pub struct Const<'a, R, A>(pub R, pub PhantomData<&'a A>);Expand description
The Const functor.
Const<R, A> stores a value of type R and ignores the type A.
§Type Parameters
'a: The lifetime of the values.R: The stored type.A: The ignored type.
Tuple Fields§
§0: R§1: PhantomData<&'a A>Implementations§
Source§impl<'a, R, A> Const<'a, R, A>
§Type Parameters
'a: The lifetime of the values.
R: The stored type.
A: The ignored type.
impl<'a, R, A> Const<'a, R, A>
§Type Parameters
'a: The lifetime of the values.R: The stored type.A: The ignored type.
Sourcepub fn map<B>(self, _f: impl FnOnce(A) -> B) -> Const<'a, R, B>
pub fn map<B>(self, _f: impl FnOnce(A) -> B) -> Const<'a, R, B>
Maps over the phantom type parameter, preserving the stored value.
Since Const ignores its second type parameter, the function is never called.
This is the inherent method form of Functor::map.
§Type Signature
forall R A B. (Const R A, A -> B) -> Const R B
§Type Parameters
B: The new phantom type.
§Parameters
self: TheConstinstance._f: The function to map (ignored).
§Returns
A new Const instance with the same stored value but a different phantom type.
§Examples
use fp_library::types::const_val::Const;
let c: Const<i32, String> = Const::new(42);
let mapped: Const<i32, bool> = c.map(|s: String| s.is_empty());
assert_eq!(mapped.0, 42);Sourcepub fn lift2<B, C>(
self,
other: Const<'a, R, B>,
_f: impl FnOnce(A, B) -> C,
) -> Const<'a, R, C>where
R: Semigroup,
pub fn lift2<B, C>(
self,
other: Const<'a, R, B>,
_f: impl FnOnce(A, B) -> C,
) -> Const<'a, R, C>where
R: Semigroup,
Combines two Const values by appending their stored values, discarding the phantom types.
This is the inherent method form of Lift::lift2.
§Type Signature
forall R A B C. Semigroup R => (Const R A, Const R B, (A, B) -> C) -> Const R C
§Type Parameters
B: The second phantom type.C: The result phantom type.
§Parameters
self: TheConstinstance.other: The otherConstinstance._f: The function to lift (ignored).
§Returns
A new Const instance with the appended stored values.
§Examples
use fp_library::types::const_val::Const;
let c1: Const<String, i32> = Const::new("Hello".to_string());
let c2: Const<String, i32> = Const::new(" World".to_string());
let lifted = c1.lift2(c2, |a: i32, b: i32| a + b);
assert_eq!(lifted.0, "Hello World");Sourcepub fn apply_first<B>(self, other: Const<'a, R, B>) -> Const<'a, R, A>where
R: Semigroup,
pub fn apply_first<B>(self, other: Const<'a, R, B>) -> Const<'a, R, A>where
R: Semigroup,
Combines the stored values of two Const instances, keeping the phantom type of the first.
This is the inherent method form of ApplyFirst::apply_first.
§Type Signature
forall R A B. Semigroup R => (Const R A, Const R B) -> Const R A
§Type Parameters
B: The phantom type of the secondConstinstance.
§Parameters
self: TheConstinstance.other: The secondConstinstance.
§Returns
A new Const instance with the appended stored values.
§Examples
use fp_library::types::const_val::Const;
let c1: Const<String, i32> = Const::new("Hello".to_string());
let c2: Const<String, bool> = Const::new(" World".to_string());
let result = c1.apply_first(c2);
assert_eq!(result.0, "Hello World");Sourcepub fn apply_second<B>(self, other: Const<'a, R, B>) -> Const<'a, R, B>where
R: Semigroup,
pub fn apply_second<B>(self, other: Const<'a, R, B>) -> Const<'a, R, B>where
R: Semigroup,
Combines the stored values of two Const instances, keeping the phantom type of the second.
This is the inherent method form of ApplySecond::apply_second.
§Type Signature
forall R A B. Semigroup R => (Const R A, Const R B) -> Const R B
§Type Parameters
B: The phantom type of the secondConstinstance.
§Parameters
self: TheConstinstance.other: The secondConstinstance.
§Returns
A new Const instance with the appended stored values.
§Examples
use fp_library::types::const_val::Const;
let c1: Const<String, i32> = Const::new("Hello".to_string());
let c2: Const<String, bool> = Const::new(" World".to_string());
let result = c1.apply_second(c2);
assert_eq!(result.0, "Hello World");Sourcepub fn pure(_a: A) -> Selfwhere
R: Monoid,
pub fn pure(_a: A) -> Selfwhere
R: Monoid,
Creates a Const with the monoidal identity, ignoring the given value.
This is the inherent method form of Pointed::pure.
§Type Signature
forall R A. Monoid R => A -> Const R A
§Parameters
_a: The value to wrap (ignored).
§Returns
A new Const instance with the empty value of the stored type.
§Examples
use fp_library::types::const_val::Const;
let c: Const<String, i32> = Const::pure(42);
assert_eq!(c.0, "".to_string());Trait Implementations§
Source§impl<'a, R: Ord, A: Ord> Ord for Const<'a, R, A>
impl<'a, R: Ord, A: Ord> Ord for Const<'a, R, A>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a, R: PartialOrd, A: PartialOrd> PartialOrd for Const<'a, R, A>
impl<'a, R: PartialOrd, A: PartialOrd> PartialOrd for Const<'a, R, A>
impl<'a, R: Copy, A> Copy for Const<'a, R, A>
impl<'a, R: Eq, A: Eq> Eq for Const<'a, R, A>
impl<'a, R, A> StructuralPartialEq for Const<'a, R, A>
Auto Trait Implementations§
impl<'a, R, A> Freeze for Const<'a, R, A>where
R: Freeze,
impl<'a, R, A> RefUnwindSafe for Const<'a, R, A>where
R: RefUnwindSafe,
A: RefUnwindSafe,
impl<'a, R, A> Send for Const<'a, R, A>
impl<'a, R, A> Sync for Const<'a, R, A>
impl<'a, R, A> Unpin for Const<'a, R, A>where
R: Unpin,
impl<'a, R, A> UnsafeUnpin for Const<'a, R, A>where
R: UnsafeUnpin,
impl<'a, R, A> UnwindSafe for Const<'a, R, A>where
R: UnwindSafe,
A: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more