Macro impl_more::impl_deref

source ·
macro_rules! impl_deref {
    (<$($generic:ident),+> in $this:ty => $target:ty) => { ... };
    (<$($generic:ident),+> in $this:ty => $field:ident : $target:ty) => { ... };
    ($this:ty => $target:ty) => { ... };
    ($this:ty => $field:ident : $target:ty) => { ... };
}
Expand description

Implement Deref for a struct.

The first argument is that of the newtype struct to create the impl for and the second is the deref target type. The third argument is required for non-newtype structs and is the name of the field to deref to. Type parameters require special handling, see examples.

Also see impl_deref_mut, impl_deref_and_mut, and forward_deref_and_mut.

Examples

With a newtype struct:

struct Foo(String);
impl_more::impl_deref!(Foo => String);

let mut foo = Foo("bar".to_owned());
assert_eq!(foo.len(), 3);

With a named field struct and type parameter:

struct MyStruct<T> { msg: T };
impl_more::impl_deref!(<T> in MyStruct<T> => msg: T);

let foo = MyStruct { msg: "two".to_owned() };
assert_eq!(foo.len(), 3);