Trait PureExt

Source
pub trait PureExt: Sized {
    // Provided methods
    fn to_pure<P>(&self) -> P::Output<Self>
       where P: Pure,
             Self: Clone { ... }
    fn to_pure_owned<P>(self) -> P::Output<Self>
       where P: Pure,
             Self: Clone { ... }
    fn pair_with<P, U>(&self, other: &U) -> P::Output<(Self, U)>
       where P: Pure,
             Self: Clone,
             U: Clone { ... }
    fn lift_other<P, U>(&self, other: &U) -> P::Output<U>
       where P: Pure,
             U: Clone { ... }
    fn combine_with<P, U, V>(
        &self,
        other: &U,
        f: impl Fn(&Self, &U) -> V,
    ) -> P::Output<V>
       where P: Pure,
             Self: Clone,
             U: Clone,
             V: Clone { ... }
}
Expand description

Extension trait providing a more ergonomic way to use Pure.

This trait allows calling methods like to_pure directly on values, making it more convenient to lift values into higher-kinded contexts and work with them.

§Implemented For

This trait is implemented for all types that implement Clone, which means it can be used with any cloneable value.

§Examples

Using to_pure to lift a value into Option:

use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};

// Instead of: <Option<i32> as Pure>::pure(&42)
// We can write:
let value: i32 = 42;
let option: Option<i32> = value.to_pure::<Option<i32>>();
assert_eq!(option, Some(42));

Chaining operations with the Validated type:

use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};
use rustica::traits::functor::Functor;
use rustica::datatypes::validated::Validated;

let value: i32 = 42;

// Lift into Validated context and transform
let validated: Validated<&str, i32> = value.to_pure::<Validated<&str, i32>>();
let doubled: Validated<&str, i32> = validated.fmap(|x: &i32| x * 2);

assert!(matches!(doubled, Validated::Valid(84)));

Provided Methods§

Source

fn to_pure<P>(&self) -> P::Output<Self>
where P: Pure, Self: Clone,

Lift a value into a context.

This method provides a more ergonomic way to use Pure::pure, allowing you to call it directly on values.

§Type Parameters
  • P: The higher-kinded type to lift into, implementing Pure
§Returns

The value wrapped in the higher-kinded context

§Examples
use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};
use rustica::datatypes::validated::Validated;

let x: i32 = 42;

// Lift into Option
let option: Option<i32> = x.to_pure::<Option<i32>>();
assert_eq!(option, Some(42));

// Lift into Validated
let validated: Validated<&str, i32> = x.to_pure::<Validated<&str, i32>>();
assert!(matches!(validated, Validated::Valid(42)));
Source

fn to_pure_owned<P>(self) -> P::Output<Self>
where P: Pure, Self: Clone,

Lift a value into a context, consuming the value.

This method provides a more efficient version of to_pure that consumes the value instead of cloning it.

§Type Parameters
  • P: The higher-kinded type to lift into, implementing Pure
§Returns

The value wrapped in the higher-kinded context

§Examples
use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};

// Using to_pure_owned (consumes the value)
let option: Option<i32> = 42.to_pure_owned::<Option<i32>>();
assert_eq!(option, Some(42));
Source

fn pair_with<P, U>(&self, other: &U) -> P::Output<(Self, U)>
where P: Pure, Self: Clone, U: Clone,

Lift a pair of values into a context.

This method combines two values into a tuple and lifts the result into a higher-kinded context.

§Type Parameters
  • P: The higher-kinded type to lift into, implementing Pure
  • U: The type of the second value
§Parameters
  • other: A reference to the second value to pair with this one
§Returns

A pair of values in the higher-kinded context

§Examples
use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};
use rustica::datatypes::validated::Validated;

let a: i32 = 42;
let b: &str = "hello";

// Create a pair in Option context
let option_pair: Option<(i32, &str)> = a.pair_with::<Option<(i32, &str)>, &str>(&b);
assert_eq!(option_pair, Some((42, "hello")));

// Create a pair in Validated context
let validated_pair: Validated<&str, (i32, &str)> = a.pair_with::<Validated<&str, (i32, &str)>, &str>(&b);
assert!(matches!(validated_pair, Validated::Valid((42, "hello"))));
Source

fn lift_other<P, U>(&self, other: &U) -> P::Output<U>
where P: Pure, U: Clone,

Lift another value into a context.

This method lifts a different value into the same context type. It’s useful for working with applicative functors.

§Type Parameters
  • P: The higher-kinded type to lift into, implementing Pure
  • U: The type of the other value
§Parameters
  • other: A reference to the value to lift
§Returns

The other value lifted into the context

§Examples
use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};

let a: i32 = 42;
let b: &str = "hello";

// Lift b into the same context as would be used for a
let option_b: Option<&str> = a.lift_other::<Option<&str>, &str>(&b);
assert_eq!(option_b, Some("hello"));
Source

fn combine_with<P, U, V>( &self, other: &U, f: impl Fn(&Self, &U) -> V, ) -> P::Output<V>
where P: Pure, Self: Clone, U: Clone, V: Clone,

Combine two values into a new value and lift it into a context.

This method applies a function to two values and lifts the result into a higher-kinded context. It’s particularly useful for applicative-style programming.

§Type Parameters
  • P: The higher-kinded type to lift into, implementing Pure
  • U: The type of the second value
  • V: The result type after applying the function
§Parameters
  • other: A reference to the second value to combine with
  • f: The function to apply to both values
§Returns

The result of applying the function to both values, lifted into the context

§Examples
use rustica::traits::hkt::HKT;
use rustica::traits::pure::{Pure, PureExt};

let a: i32 = 42;
let b: i32 = 10;

// Combine values with a function
let result: Option<i32> = a.combine_with::<Option<i32>, i32, i32>(&b, |x, y| x * y);
assert_eq!(result, Some(420));

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: Clone> PureExt for T