mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 00:56:41 +01:00
feat: remove ChainErrorFrom and IntoChainError, add From
ChainErrorFrom and IntoChainError are not needed anymore with the `#[track_caller]` feature. Now, a proper `From<T> for ChainError<T>` can be implemented.
This commit is contained in:
parent
bdfec08228
commit
f5c8afce0d
34
src/lib.rs
34
src/lib.rs
|
@ -636,40 +636,16 @@ impl<T: 'static + Display + Debug> Debug for ChainError<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ChainErrorFrom<T>` is similar to `From<T>`
|
impl<T> From<T> for ChainError<T>
|
||||||
pub trait ChainErrorFrom<T>: Sized {
|
|
||||||
/// similar to From<T>::from()
|
|
||||||
fn chain_error_from(from: T, line_filename: Option<String>) -> ChainError<Self>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `IntoChainError<T>` is similar to `Into<T>`
|
|
||||||
pub trait IntoChainError<T>: Sized {
|
|
||||||
/// similar to Into<T>::into()
|
|
||||||
fn into_chain_error(self, line_filename: Option<String>) -> ChainError<T>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, U> IntoChainError<U> for T
|
|
||||||
where
|
where
|
||||||
U: ChainErrorFrom<T>,
|
T: 'static + Display + Debug,
|
||||||
{
|
{
|
||||||
|
#[track_caller]
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_chain_error(self, line_filename: Option<String>) -> ChainError<U> {
|
fn from(e: T) -> ChainError<T> {
|
||||||
U::chain_error_from(self, line_filename)
|
ChainError::new(e, None, Some(Location::caller().to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> ChainErrorFrom<T> for U
|
|
||||||
where
|
|
||||||
T: Into<U>,
|
|
||||||
U: 'static + Display + Debug,
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn chain_error_from(t: T, line_filename: Option<String>) -> ChainError<Self> {
|
|
||||||
let e: U = t.into();
|
|
||||||
ChainError::new(e, None, line_filename)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience macro to create a "new type" T(String) and implement Display + Debug for T
|
/// Convenience macro to create a "new type" T(String) and implement Display + Debug for T
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
|
Loading…
Reference in a new issue