Skip to main content

forward_as_mut

Macro forward_as_mut 

Source
macro_rules! forward_as_mut {
    (<$($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 AsMut 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 AsMut implementation.

§Examples

With a newtype struct:

use impl_more::forward_as_mut;

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

let mut foo = Foo("bar".to_owned());
foo.as_mut().make_ascii_uppercase();

assert_eq!(foo.0, "BAR");

With a named field struct and type parameters:

use impl_more::forward_as_mut;

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

let mut foo = Foo { inner: vec![1, 2, 3] };
foo.as_mut().reverse();

assert_eq!(foo.inner, [3, 2, 1]);