macro_rules! trace {
(struct $trace:ident; $($a:ty => $b:ty,)*) => { ... };
(struct $name:ident; $($a:ty => $b:ty),*) => { ... };
(struct $name:ident) => { ... };
($($a:ty => $b:ty,)*) => { ... };
($($a:ty => $b:ty),*) => { ... };
}
Expand description
Helper macro for defining a Trace
struct, and instances of From<Trace<B>>
for Trace<A>
.
§Example use
#[macro_use] extern crate stacktrace;
pub struct Error1(usize);
pub struct Error2(String);
impl From<Error1> for Error2 {
fn from(err: Error1) -> Error2 {
Error2(format!("{}", err.0))
}
}
trace!{Error1 => Error2}
fn makes_a_traced_error() -> Result<(), Trace<Error1>> {
try!(Err(Error1(1337))); // Uses generic instance of "From<Err>" for "Trace<Err>"
Ok(())
}
fn propagates_a_traced_error() -> Result<(), Trace<Error2>> {
try!(makes_a_traced_error()); // Uses the macro-generated instance of "From<Trace<Error1>>" for "Trace<Error2>"
Ok(())
}
§Advanced use
The trace
macro takes an optional initial ‘name’ parameter: trace!{struct MyTrace}
will define
a struct named Example
that behaves exactly like the default Trace
struct.
#[macro_use] extern crate stacktrace;
pub struct Error1(usize);
pub struct Error2(String);
impl From<Error1> for Error2 {
fn from(err: Error1) -> Error2 {
Error2(format!("{}", err.0))
}
}
trace!{struct MyTrace; Error1 => Error2}
fn makes_a_traced_error() -> Result<(), MyTrace<Error1>> {
try!(Err(Error1(1337))); // Uses generic instance of "From<Err>" for "MyTrace<Err>"
Ok(())
}
fn propagates_a_traced_error() -> Result<(), MyTrace<Error2>> {
try!(makes_a_traced_error()); // Uses the macro-generated instance of "From<MyTrace<Error1>>" for "MyTrace<Error2>"
Ok(())
}