Skip to main content

forward_as_mut

Macro forward_as_mut 

Source
macro_rules! forward_as_mut {
    (<$($generic:ident),+> in $($rest:tt)+) => { ... };
    (<const $($rest:tt)+) => { ... };
    (<$generic:ident, $($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 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.

Declare type and const parameters before in.

§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]);