wiwi::chain

Struct GenericChain

Source
pub struct GenericChain<T> { /* private fields */ }
Expand description

Generic (works with all types) chaining wrapper

This wrapper’s API is very very simple, consisting of just 4 functions:

  • new creates a new generic chainer (obviously :p)
  • map takes a closure that gets ownership of the inner value, and returns a new value, possibly of a different type
  • with_inner takes a closure that can operate on a passed-in mutable reference to the inner value
  • into_inner will unwrap the struct and return the (possibly modified) contained value.

The idea is to provide a generic type that can give very basic and generic chaining abilities, useful for types that don’t have their own dedicated chainer.

This type also does not implement any traits, not even the chain ones provided by this module. The API really is just those 4 functions.

§Examples

Let’s pretend VecChain doesn’t exist just for a moment…

// create the chain ...
let numbers = GenericChain::new(Vec::<i32>::new())
   // ... chain push some numbers ...
   .with_inner(|v| v.push(1))
   .with_inner(|v| v.push(2))
   .with_inner(|v| v.push(3))
   .with_inner(|v| v.push(4))
   .with_inner(|v| v.push(5))
   // ... get back the inner vec, now with the pushed elements
   .into_inner();

assert_eq!(&*numbers, &[1, 2, 3, 4, 5]);

Implementations§

Source§

impl<T> GenericChain<T>

Source

pub fn new(value: T) -> Self

Source

pub fn map<T2>(self, f: impl FnOnce(T) -> T2) -> GenericChain<T2>

Source

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

Source

pub fn into_inner(self) -> T

Auto Trait Implementations§

§

impl<T> Freeze for GenericChain<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for GenericChain<T>
where T: RefUnwindSafe,

§

impl<T> Send for GenericChain<T>
where T: Send,

§

impl<T> Sync for GenericChain<T>
where T: Sync,

§

impl<T> Unpin for GenericChain<T>
where T: Unpin,

§

impl<T> UnwindSafe for GenericChain<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> AcceptDefault<T> for T

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Encode for T

Source§

fn encode<E>(self) -> <E as Encoding>::EncodeOutput
where T: Encodable<E>, E: Encoding,

Source§

fn decode<E>(self) -> <E as Encoding>::DecodeOutput
where T: Decodable<E>, E: Encoding,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GenericChainConversion for T

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.