Expand description
Concise, declarative trait implementation macros.
§#[no_std]
Where possible, these macros emit #[no_std]-compatible code.
§Usage
struct MyNewTypeStruct(String);
impl_more::impl_as_ref!(MyNewTypeStruct => String);
impl_more::impl_as_mut!(MyNewTypeStruct => String);
impl_more::forward_as_ref!(MyNewTypeStruct => str);
impl_more::forward_as_mut!(MyNewTypeStruct => str);
impl_more::impl_deref!(MyNewTypeStruct => String);
impl_more::impl_deref_mut!(MyNewTypeStruct);
// or, to deref through String too:
// impl_more::forward_deref_and_mut!(MyNewTypeStruct => ref str);
impl_more::impl_from!(String => MyNewTypeStruct);
impl_more::impl_into!(MyNewTypeStruct => String);
impl_more::forward_display!(MyNewTypeStruct);
impl_more::forward_from_str!(MyNewTypeStruct => String);
enum MyEnum {
Bar,
Qux,
}
impl_more::impl_display_enum!(MyEnum: Bar => "bar", Qux => "qux");
enum Coords {
Xy(i64, i64),
Xyz(i64, i64, i64),
Uv(i64, i64),
}
impl_more::impl_display_enum!(
Coords:
Xy(x, y) => "{x}, {y}",
Xyz(x, y, z) => "{x}, {y}, {z}",
Uv(u, _) => "{u}",
);
#[derive(Debug)]
struct MyError(eyre::Report);
impl_more::forward_display!(MyError);
impl_more::forward_error!(MyError);
let err = MyError(eyre::eyre!("something went wrong"));
assert_eq!(err.source().unwrap().to_string(), "something went wrong");
#[derive(Debug)]
struct MyFieldError {
cause: eyre::Report,
}
impl_more::forward_display!(MyFieldError => cause);
impl_more::forward_error!(MyFieldError => cause);
let err = MyFieldError {
cause: eyre::eyre!("something else went wrong"),
};
assert_eq!(
err.source().unwrap().to_string(),
"something else went wrong"
);
#[derive(Debug)]
enum Err {
Io(std::io::Error),
Generic(String),
}
impl_more::impl_display_enum! {
Err:
Io(err) => "{err}",
Generic(msg) => "{msg}",
}
impl_more::impl_error_enum!(Err: Io(err) => err);Macros§
- forward_
as_ mut - Implement
AsMutby forwarding to a field’s implementation. - forward_
as_ ref - Implement
AsRefby forwarding to a field’s implementation. - forward_
as_ ref_ and_ mut - Implement
AsRefandAsMutby forwarding to a field’s implementations. - forward_
deref_ and_ mut - Implements
DerefandDerefMutby forwarding through an inner field’s implementation. - forward_
display - Implements
Displayfor structs by forwarding to one of its field. - forward_
error - Implements
Errorfor structs and forwards thesourceimplementation to one of its fields. - forward_
from_ str - Implement
FromStrby forwarding to a field’s implementation. - impl_
as_ mut - Implement
AsMutfor a struct. - impl_
as_ ref - Implement
AsReffor a struct. - impl_
deref - Implement
Dereffor a struct. - impl_
deref_ and_ mut - Implements
DerefandDerefMutby forwarding through an inner field’s implementation. - impl_
deref_ mut - Implement
DerefMutfor a struct. - impl_
display - Implements
Displayfor structs using aformat!-like string constructor. - impl_
display_ enum - Implements
Displayfor enums using a static string or format args for each variant. - impl_
error_ enum - Implements
Errorfor enums. - impl_
from - Implement
Fromfor a struct. - impl_
from_ for_ primitive - Implement
Fromfor a primitive. - impl_
into - Implement
Intofor a struct. - impl_
leaf_ error - Implements leaf
Errors. - impl_
newtype_ from_ into - Implement
FromandIntofor a newtype struct.