Skip to main content

Crate impl_more

Crate impl_more 

Source
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 AsMut by forwarding to a field’s implementation.
forward_as_ref
Implement AsRef by forwarding to a field’s implementation.
forward_as_ref_and_mut
Implement AsRef and AsMut by forwarding to a field’s implementations.
forward_deref_and_mut
Implements Deref and DerefMut by forwarding through an inner field’s implementation.
forward_display
Implements Display for structs by forwarding to one of its field.
forward_error
Implements Error for structs and forwards the source implementation to one of its fields.
forward_from_str
Implement FromStr by forwarding to a field’s implementation.
impl_as_mut
Implement AsMut for a struct.
impl_as_ref
Implement AsRef for a struct.
impl_deref
Implement Deref for a struct.
impl_deref_and_mut
Implements Deref and DerefMut by forwarding through an inner field’s implementation.
impl_deref_mut
Implement DerefMut for a struct.
impl_display
Implements Display for structs using a format!-like string constructor.
impl_display_enum
Implements Display for enums using a static string or format args for each variant.
impl_error_enum
Implements Error for enums.
impl_from
Implement From for a struct.
impl_from_for_primitive
Implement From for a primitive.
impl_into
Implement Into for a struct.
impl_leaf_error
Implements leaf Errors.
impl_newtype_from_into
Implement From and Into for a newtype struct.