Skip to main content

impl_deref

Macro impl_deref 

Source
macro_rules! impl_deref {
    (<$($generic:ident),+> in $($rest:tt)+) => { ... };
    (<const $($rest:tt)+) => { ... };
    (<$generic:ident, $($rest:tt)+) => { ... };
    (@parse $macro:ident [$($generic:tt)*] > in $($rest:tt)+) => { ... };
    (@parse $macro:ident [$($generic:tt)*] $next:tt $($rest:tt)+) => { ... };
    (@impl [$($generic:tt)+] $this:ty => $target:ty) => { ... };
    (@impl [$($generic:tt)+] $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. Declare type and const parameters before in, as shown below.

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 type parameter and const parameter:

struct Array<T, const N: usize>([T; N]);
impl_more::impl_deref!(<T, const N: usize> in Array<T, N> => [T; N]);

let array = Array([1, 2, 3]);
assert_eq!(array.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);