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);
struct Items<T>(Vec<T>);
impl_more::forward_into_iterator!(<T> in Items<T> => Vec<T>; owned, ref, ref_mut);
impl_more::forward_from_iterator!(<T> in Items<T> => Vec<T>);
impl_more::forward_extend!(<T> in Items<T> => Vec<T>);
struct Mask(u32);
impl_more::forward_debug!(Mask);
impl_more::forward_lower_hex!(Mask);
impl_more::forward_binary!(Mask);
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);Declare type and const parameters before in:
struct Array<T, const N: usize>([T; N]);
impl_more::impl_as_ref!(<T, const N: usize> in Array<T, N> => [T; N]);
impl_more::impl_from!(<T, const N: usize> in [T; N] => Array<T, N>);
impl_more::forward_into_iterator!(<T, const N: usize> in Array<T, N> => [T; N]; owned);
let value = Array::from([1, 2, 3]);
assert_eq!(value.as_ref(), &[1, 2, 3]);
assert_eq!(value.into_iter().sum::<u32>(), 6);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_
binary - Implement
Binaryby forwarding to a field. - forward_
debug - Implement
Debugby forwarding to a field. - 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_
extend - Implement
Extendby forwarding to a field. - forward_
from_ iterator - Implement
FromIteratorfor a single-field wrapper. - forward_
from_ str - Implement
FromStrby forwarding to a field’s implementation. - forward_
into_ iterator - Implement
IntoIteratorby forwarding to a field. - forward_
lower_ exp - Implement
LowerExpby forwarding to a field. - forward_
lower_ hex - Implement
LowerHexby forwarding to a field. - forward_
octal - Implement
Octalby forwarding to a field. - forward_
pointer - Implement
Pointerby forwarding to a field. - forward_
upper_ exp - Implement
UpperExpby forwarding to a field. - forward_
upper_ hex - Implement
UpperHexby forwarding to a field. - 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_
enum_ from - Implement
Fromfor an enum variant with one named or unnamed field. - 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.