mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 09:04:14 +01:00
deny more lints and add more docs
This commit is contained in:
parent
15f983ce55
commit
490eaa474b
97
src/lib.rs
97
src/lib.rs
|
@ -26,7 +26,7 @@
|
||||||
//!
|
//!
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
//! ~~~rust
|
//! ```rust
|
||||||
//! use chainerror::*;
|
//! use chainerror::*;
|
||||||
//! use std::error::Error;
|
//! use std::error::Error;
|
||||||
//! use std::io;
|
//! use std::io;
|
||||||
|
@ -65,10 +65,10 @@
|
||||||
//! # unreachable!();
|
//! # unreachable!();
|
||||||
//! # }
|
//! # }
|
||||||
//! }
|
//! }
|
||||||
//! ~~~
|
//! ```
|
||||||
//!
|
//!
|
||||||
//!
|
//!
|
||||||
//! ~~~rust
|
//! ```rust
|
||||||
//! use chainerror::*;
|
//! use chainerror::*;
|
||||||
//! use std::error::Error;
|
//! use std::error::Error;
|
||||||
//! use std::io;
|
//! use std::io;
|
||||||
|
@ -165,7 +165,32 @@
|
||||||
//! # unreachable!();
|
//! # unreachable!();
|
||||||
//! # }
|
//! # }
|
||||||
//! }
|
//! }
|
||||||
//! ~~~
|
//! ```
|
||||||
|
|
||||||
|
#![deny(
|
||||||
|
warnings,
|
||||||
|
absolute_paths_not_starting_with_crate,
|
||||||
|
deprecated_in_future,
|
||||||
|
keyword_idents,
|
||||||
|
macro_use_extern_crate,
|
||||||
|
missing_debug_implementations,
|
||||||
|
missing_docs,
|
||||||
|
trivial_casts,
|
||||||
|
trivial_numeric_casts,
|
||||||
|
unused_extern_crates,
|
||||||
|
unused_import_braces,
|
||||||
|
unused_qualifications,
|
||||||
|
unused_results,
|
||||||
|
unused_labels,
|
||||||
|
unused_lifetimes,
|
||||||
|
unstable_features,
|
||||||
|
unreachable_pub,
|
||||||
|
future_incompatible,
|
||||||
|
missing_copy_implementations,
|
||||||
|
missing_doc_code_examples,
|
||||||
|
rust_2018_idioms,
|
||||||
|
rust_2018_compatibility
|
||||||
|
)]
|
||||||
|
|
||||||
use std::any::TypeId;
|
use std::any::TypeId;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
@ -216,7 +241,7 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use crate::chainerror::*;
|
/// # use crate::chainerror::*;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
|
@ -258,7 +283,7 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
/// # unreachable!();
|
/// # unreachable!();
|
||||||
/// # }
|
/// # }
|
||||||
/// }
|
/// }
|
||||||
/// ~~~
|
/// ```
|
||||||
pub fn find_cause<U: Error + 'static>(&self) -> Option<&U> {
|
pub fn find_cause<U: Error + 'static>(&self) -> Option<&U> {
|
||||||
self.iter().filter_map(Error::downcast_ref::<U>).next()
|
self.iter().filter_map(Error::downcast_ref::<U>).next()
|
||||||
}
|
}
|
||||||
|
@ -269,13 +294,16 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust,ignore
|
/// ```rust
|
||||||
|
/// # use chainerror::{ChainError, derive_str_cherr};
|
||||||
|
/// # derive_str_cherr!(FooError);
|
||||||
|
/// # let err = ChainError::new(String::new(), None, None);
|
||||||
/// // Instead of writing
|
/// // Instead of writing
|
||||||
/// err.find_cause::<ChainError<FooError>>();
|
/// err.find_cause::<ChainError<FooError>>();
|
||||||
///
|
///
|
||||||
/// // leave out the ChainError<FooError> implementation detail
|
/// // leave out the ChainError<FooError> implementation detail
|
||||||
/// err.find_chain_cause::<FooError>();
|
/// err.find_chain_cause::<FooError>();
|
||||||
/// ~~~
|
/// ```
|
||||||
pub fn find_chain_cause<U: Error + 'static>(&self) -> Option<&ChainError<U>> {
|
pub fn find_chain_cause<U: Error + 'static>(&self) -> Option<&ChainError<U>> {
|
||||||
self.iter()
|
self.iter()
|
||||||
.filter_map(Error::downcast_ref::<ChainError<U>>)
|
.filter_map(Error::downcast_ref::<ChainError<U>>)
|
||||||
|
@ -288,7 +316,10 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust,ignore
|
/// ```rust
|
||||||
|
/// # use chainerror::{ChainError, derive_str_cherr};
|
||||||
|
/// # derive_str_cherr!(FooErrorKind);
|
||||||
|
/// # let err = ChainError::new(String::new(), None, None);
|
||||||
/// // Instead of writing
|
/// // Instead of writing
|
||||||
/// err.find_cause::<ChainError<FooErrorKind>>();
|
/// err.find_cause::<ChainError<FooErrorKind>>();
|
||||||
/// // and/or
|
/// // and/or
|
||||||
|
@ -297,8 +328,8 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
/// err.find_cause::<FooErrorKind>();
|
/// err.find_cause::<FooErrorKind>();
|
||||||
///
|
///
|
||||||
/// // leave out the ChainError<FooErrorKind> implementation detail
|
/// // leave out the ChainError<FooErrorKind> implementation detail
|
||||||
/// err.find_chain_or_kind::<FooErrorKind>();
|
/// err.find_kind_or_cause::<FooErrorKind>();
|
||||||
/// ~~~
|
/// ```
|
||||||
pub fn find_kind_or_cause<U: Error + 'static>(&self) -> Option<&U> {
|
pub fn find_kind_or_cause<U: Error + 'static>(&self) -> Option<&U> {
|
||||||
self.iter()
|
self.iter()
|
||||||
.filter_map(|e| {
|
.filter_map(|e| {
|
||||||
|
@ -313,7 +344,7 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use crate::chainerror::*;
|
/// # use crate::chainerror::*;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
|
@ -365,7 +396,7 @@ impl<T: 'static + Display + Debug> ChainError<T> {
|
||||||
/// # unreachable!();
|
/// # unreachable!();
|
||||||
/// # }
|
/// # }
|
||||||
/// }
|
/// }
|
||||||
/// ~~~
|
/// ```
|
||||||
pub fn kind(&self) -> &T {
|
pub fn kind(&self) -> &T {
|
||||||
&self.kind
|
&self.kind
|
||||||
}
|
}
|
||||||
|
@ -423,6 +454,7 @@ impl<U: 'static + Display + Debug> ChainErrorDown for ChainError<U> {
|
||||||
if self.is_chain::<T>() {
|
if self.is_chain::<T>() {
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
unsafe {
|
unsafe {
|
||||||
|
#[allow(trivial_casts)]
|
||||||
Some(&*(self as *const dyn Error as *const &ChainError<T>))
|
Some(&*(self as *const dyn Error as *const &ChainError<T>))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -434,6 +466,7 @@ impl<U: 'static + Display + Debug> ChainErrorDown for ChainError<U> {
|
||||||
if self.is_chain::<T>() {
|
if self.is_chain::<T>() {
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
unsafe {
|
unsafe {
|
||||||
|
#[allow(trivial_casts)]
|
||||||
Some(&mut *(self as *mut dyn Error as *mut &mut ChainError<T>))
|
Some(&mut *(self as *mut dyn Error as *mut &mut ChainError<T>))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -503,7 +536,7 @@ impl<T: 'static + Display + Debug> Error for &mut ChainError<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static + Display + Debug> Display for ChainError<T> {
|
impl<T: 'static + Display + Debug> Display for ChainError<T> {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
write!(f, "{}", self.kind)?;
|
write!(f, "{}", self.kind)?;
|
||||||
|
|
||||||
#[cfg(feature = "display-cause")]
|
#[cfg(feature = "display-cause")]
|
||||||
|
@ -518,7 +551,7 @@ impl<T: 'static + Display + Debug> Display for ChainError<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static + Display + Debug> Debug for ChainError<T> {
|
impl<T: 'static + Display + Debug> Debug for ChainError<T> {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
#[cfg(not(feature = "no-fileline"))]
|
#[cfg(not(feature = "no-fileline"))]
|
||||||
{
|
{
|
||||||
if let Some(o) = self.occurrence {
|
if let Some(o) = self.occurrence {
|
||||||
|
@ -543,11 +576,15 @@ impl<T: 'static + Display + Debug> Debug for ChainError<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `ChainErrorFrom<T>` is similar to `From<T>`
|
||||||
pub trait ChainErrorFrom<T>: Sized {
|
pub trait ChainErrorFrom<T>: Sized {
|
||||||
|
/// similar to From<T>::from()
|
||||||
fn chain_error_from(_: T, line_filename: Option<(u32, &'static str)>) -> ChainError<Self>;
|
fn chain_error_from(_: T, line_filename: Option<(u32, &'static str)>) -> ChainError<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `IntoChainError<T>` is similar to `Into<T>`
|
||||||
pub trait IntoChainError<T>: Sized {
|
pub trait IntoChainError<T>: Sized {
|
||||||
|
/// similar to Into<T>::into()
|
||||||
fn into_chain_error(self, line_filename: Option<(u32, &'static str)>) -> ChainError<T>;
|
fn into_chain_error(self, line_filename: Option<(u32, &'static str)>) -> ChainError<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -571,6 +608,9 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// map into `ChainError<T>` with `IntoChainError`
|
||||||
|
///
|
||||||
|
/// adds `line!()` and `file!()` information
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! minto_cherr {
|
macro_rules! minto_cherr {
|
||||||
( ) => {
|
( ) => {
|
||||||
|
@ -578,6 +618,9 @@ macro_rules! minto_cherr {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// into `ChainError<T>` with `IntoChainError`
|
||||||
|
///
|
||||||
|
/// adds `line!()` and `file!()` information
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! into_cherr {
|
macro_rules! into_cherr {
|
||||||
( $t:expr ) => {
|
( $t:expr ) => {
|
||||||
|
@ -590,7 +633,7 @@ macro_rules! into_cherr {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// Create a new ChainError<FooError>, where `FooError` must implement `Display` and `Debug`.
|
/// Create a new ChainError<FooError>, where `FooError` must implement `Display` and `Debug`.
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use chainerror::*;
|
/// # use chainerror::*;
|
||||||
/// #
|
/// #
|
||||||
/// # #[derive(Debug)]
|
/// # #[derive(Debug)]
|
||||||
|
@ -627,11 +670,11 @@ macro_rules! into_cherr {
|
||||||
/// # _ => panic!(),
|
/// # _ => panic!(),
|
||||||
/// # }
|
/// # }
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~
|
/// ```
|
||||||
///
|
///
|
||||||
/// Additionally an error cause can be added.
|
/// Additionally an error cause can be added.
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use chainerror::*;
|
/// # use chainerror::*;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
|
@ -669,7 +712,7 @@ macro_rules! into_cherr {
|
||||||
/// # _ => panic!(),
|
/// # _ => panic!(),
|
||||||
/// # }
|
/// # }
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! cherr {
|
macro_rules! cherr {
|
||||||
( $k:expr ) => ({
|
( $k:expr ) => ({
|
||||||
|
@ -697,7 +740,7 @@ macro_rules! cherr {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use crate::chainerror::*;
|
/// # use crate::chainerror::*;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
|
@ -735,12 +778,12 @@ macro_rules! cherr {
|
||||||
/// # unreachable!();
|
/// # unreachable!();
|
||||||
/// # }
|
/// # }
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~
|
/// ```
|
||||||
///
|
///
|
||||||
/// `mstrerr!()` can also be used to map a new `ChainError<T>`, where T was defined with
|
/// `mstrerr!()` can also be used to map a new `ChainError<T>`, where T was defined with
|
||||||
/// `derive_str_cherr!(T)`
|
/// `derive_str_cherr!(T)`
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use crate::chainerror::*;
|
/// # use crate::chainerror::*;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
|
@ -778,7 +821,7 @@ macro_rules! cherr {
|
||||||
/// # unreachable!();
|
/// # unreachable!();
|
||||||
/// # }
|
/// # }
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! mstrerr {
|
macro_rules! mstrerr {
|
||||||
( $t:path, $msg:expr ) => ({
|
( $t:path, $msg:expr ) => ({
|
||||||
|
@ -805,7 +848,7 @@ macro_rules! mstrerr {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use crate::chainerror::*;
|
/// # use crate::chainerror::*;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::result::Result;
|
/// # use std::result::Result;
|
||||||
|
@ -836,7 +879,7 @@ macro_rules! mstrerr {
|
||||||
/// # unreachable!();
|
/// # unreachable!();
|
||||||
/// # }
|
/// # }
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! strerr {
|
macro_rules! strerr {
|
||||||
( $t:path, $msg:expr ) => ({
|
( $t:path, $msg:expr ) => ({
|
||||||
|
@ -863,7 +906,7 @@ macro_rules! strerr {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ~~~rust
|
/// ```rust
|
||||||
/// # use crate::chainerror::*;
|
/// # use crate::chainerror::*;
|
||||||
/// # use std::error::Error;
|
/// # use std::error::Error;
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
|
@ -901,7 +944,7 @@ macro_rules! strerr {
|
||||||
/// # unreachable!();
|
/// # unreachable!();
|
||||||
/// # }
|
/// # }
|
||||||
/// # }
|
/// # }
|
||||||
/// ~~~
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! derive_str_cherr {
|
macro_rules! derive_str_cherr {
|
||||||
($e:ident) => {
|
($e:ident) => {
|
||||||
|
|
Loading…
Reference in a new issue