wiwi::chain

Trait Chain

Source
pub trait Chain
where Self: Sized + Sealed + Into<Self::Inner> + AsRef<Self::Inner> + AsMut<Self::Inner>, Self::Inner: ChainInner<Chain = Self>,
{ type Inner; // Provided methods fn into_inner(self) -> Self::Inner { ... } fn from_inner(inner: Self::Inner) -> Self { ... } fn as_inner(&self) -> &Self::Inner { ... } fn as_inner_mut(&mut self) -> &mut Self::Inner { ... } fn with_inner<F, Void>(self, f: F) -> Self where F: FnOnce(&mut Self::Inner) -> Void { ... } }

Required Associated Types§

Provided Methods§

Source

fn into_inner(self) -> Self::Inner

Source

fn from_inner(inner: Self::Inner) -> Self

Source

fn as_inner(&self) -> &Self::Inner

Source

fn as_inner_mut(&mut self) -> &mut Self::Inner

Source

fn with_inner<F, Void>(self, f: F) -> Self
where F: FnOnce(&mut Self::Inner) -> Void,

Takes a closure that is called, passing in a reference to the inner value

This is useful for if there is something we have not implemented, or you otherwise need to borrow the inner struct for something, but are in the middle of some chain. It lets you do otherwise nonchainable operations inline with other chaining operations, so no need to break the chain c:

The closure passed in is allowed to return anything, but the return value is simply ignored. This makes it so you can call a function for its side effect, even if that function returns something you wouldn’t have needed anyways.

For example, MaybeUninit::write returns &mut T, but you might not need that reference, so instead of writing .with_inner(|val| { val.write(...); }), you can simply write .with_inner(|val| val.write(...)).

§Examples
let chain = VecChain::<usize>::new();

// let's pretend `push` and `reserve` don't already have chainable versions...
let chain = chain
   .with_inner(|v| v.reserve(10))
   .with_inner(|v| v.push(1))
   .with_inner(|v| v.push(2));

assert!(chain.as_inner().len() == 2);
assert!(chain.as_inner().capacity() >= 10);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> Chain for VecChain<T>

Source§

type Inner = Vec<T>

Source§

impl<T, const N: usize> Chain for ArrayChain<T, N>