Skip to main content

forward_as_ref

Macro forward_as_ref 

Source
macro_rules! forward_as_ref {
    (<$($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 AsRef by forwarding to a field’s implementation.

The first argument is the struct to create the impl for and the second is the target type produced by the field’s AsRef implementation.

§Examples

With a newtype struct:

use impl_more::forward_as_ref;

struct Foo(String);
forward_as_ref!(Foo => str);

let foo = Foo("bar".to_owned());
assert_eq!(foo.as_ref(), "bar");

With a named field struct and type parameters:

use impl_more::forward_as_ref;

struct Foo<T> { inner: Vec<T> }
forward_as_ref!(<T> in Foo<T> => inner: [T]);

let foo = Foo { inner: vec![1, 2, 3] };
assert_eq!(foo.as_ref(), &[1, 2, 3]);