BoxWitness

Struct BoxWitness 

Source
pub struct BoxWitness;
Expand description

BoxWitness is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for the Box<T> type constructor. It allows Box to be used with generic functional programming traits like Functor, Applicative, Foldable, and Monad.

By implementing HKT for BoxWitness, we can write generic functions that operate on any type that has the “shape” of Box, without knowing the inner type T.

Trait Implementations§

Source§

impl Applicative<BoxWitness> for BoxWitness

Source§

fn pure<T>(value: T) -> <BoxWitness as HKT>::Type<T>

Lifts a pure value into a Box.

§Arguments
  • value: The value to wrap in a Box.
§Returns

Box::new(value).

Source§

fn apply<A, B, Func>( f_ab: <BoxWitness as HKT>::Type<Func>, f_a: <BoxWitness as HKT>::Type<A>, ) -> <BoxWitness as HKT>::Type<B>
where Func: FnMut(A) -> B, A: Clone,

Applies a function wrapped in a Box (f_ab) to a value wrapped in a Box (f_a).

The function is applied to the value, and the result is wrapped in a new Box.

§Arguments
  • f_ab: A Box containing the function.
  • f_a: A Box containing the argument.
§Returns

A Box containing the result of the application.

Source§

impl CoMonad<BoxWitness> for BoxWitness

Source§

fn extract<A>(fa: &<Self as HKT>::Type<A>) -> A
where A: Clone,

Extracts the value at the current focus of the comonadic context. Read more
Source§

fn extend<A, B, Func>( fa: &<Self as HKT>::Type<A>, f: Func, ) -> <Self as HKT>::Type<B>
where Func: FnMut(&<Self as HKT>::Type<A>) -> B,

Extends the comonadic context by applying a function to its observed state. Read more
Source§

impl Foldable<BoxWitness> for BoxWitness

Source§

fn fold<A, B, Func>(fa: Box<A>, init: B, f: Func) -> B
where Func: FnMut(B, A) -> B,

Folds (reduces) a Box into a single value.

The function f is applied with the initial accumulator and the value inside the Box.

§Arguments
  • fa: The Box to fold.
  • init: The initial accumulator value.
  • f: The folding function.
§Returns

The accumulated result.

Source§

impl Functor<BoxWitness> for BoxWitness

Source§

fn fmap<A, B, Func>( m_a: <BoxWitness as HKT>::Type<A>, f: Func, ) -> <BoxWitness as HKT>::Type<B>
where Func: FnOnce(A) -> B,

Implements the fmap operation for Box<T>.

If the Box contains a value, the function f is applied to value, and the result is wrapped in a new Box.

§Arguments
  • m_a: The Box to map over.
  • f: The function to apply to the value inside the Box.
§Returns

A new Box with the function applied to its content.

Source§

impl HKT for BoxWitness

Source§

type Type<T> = Box<T>

Specifies that BoxWitness represents the Box<T> type constructor.

Source§

impl Monad<BoxWitness> for BoxWitness

Source§

fn bind<A, B, Func>( m_a: <BoxWitness as HKT>::Type<A>, f: Func, ) -> <BoxWitness as HKT>::Type<B>
where Func: FnMut(A) -> <BoxWitness as HKT>::Type<B>,

Implements the bind operation for Box<T>.

The function f is applied to the value inside the Box, which itself returns a new Box.

§Arguments
  • m_a: The initial Box.
  • f: A function that takes the inner value of m_a and returns a new Box.
§Returns

A new Box representing the chained computation.

Auto Trait Implementations§

Blanket Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.