pub trait Chainwhere
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§
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
Sourcefn with_inner<F, Void>(self, f: F) -> Self
fn with_inner<F, Void>(self, f: F) -> Self
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.