Skip to main content

Identity

Struct Identity 

Source
pub struct Identity<A>(pub A);
Expand description

Wraps a value.

The Identity type represents a trivial wrapper around a value. It is the simplest possible container. It is often used as a base case for higher-kinded types or when a container is required but no additional effect is needed.

§Higher-Kinded Type Representation

The higher-kinded representation of this type constructor is IdentityBrand, which is fully polymorphic over the wrapped value type.

§Serialization

This type supports serialization and deserialization via serde when the serde feature is enabled.

§Type Parameters

  • A: The type of the wrapped value.

§Fields

  • 0: The wrapped value.

Tuple Fields§

§0: A

Implementations§

Source§

impl<A> Identity<A>

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The type of the wrapped value.
Source

pub fn map<B>(self, f: impl FnOnce(A) -> B) -> Identity<B>

Maps a function over the value in the identity.

This is the inherent version of Functor::map, accepting FnOnce instead of Fn since it consumes self.

§Type Signature

forall A B. (Identity A, A -> B) -> Identity B

§Type Parameters
  • B: The type of the result of applying the function.
§Parameters
  • self: The identity instance.
  • f: The function to apply.
§Returns

A new identity containing the result of applying the function.

§Examples
use fp_library::types::*;

let x = Identity(5);
let y = x.map(|i| i * 2);
assert_eq!(y, Identity(10));
Source

pub fn lift2<B, C>( self, other: Identity<B>, f: impl FnOnce(A, B) -> C, ) -> Identity<C>

Lifts a binary function to operate on two identities.

See Lift::lift2 for the type class version.

§Type Signature

forall A B C. (Identity A, Identity B, (A, B) -> C) -> Identity C

§Type Parameters
  • B: The type of the other identity’s value.
  • C: The return type of the function.
§Parameters
  • self: The identity instance.
  • other: The other identity.
  • f: The binary function to apply.
§Returns

A new identity containing the result of applying the function.

§Examples
use fp_library::types::*;

let x = Identity(1);
let y = Identity(2);
let z = x.lift2(y, |a, b| a + b);
assert_eq!(z, Identity(3));
Source

pub fn apply<B>(self, ff: Identity<impl FnOnce(A) -> B>) -> Identity<B>

Applies a wrapped function to a value.

See Semiapplicative::apply for the type class version.

§Type Signature

forall A B. (Identity A, Identity (A -> B)) -> Identity B

§Type Parameters
  • B: The return type of the wrapped function.
§Parameters
  • self: The identity instance.
  • ff: The identity containing the function.
§Returns

A new identity containing the result.

§Examples
use fp_library::types::*;

let f = Identity(|x: i32| x * 2);
let x = Identity(5);
let y = x.apply(f);
assert_eq!(y, Identity(10));
Source

pub fn bind<B>(self, f: impl FnOnce(A) -> Identity<B>) -> Identity<B>

Chains identity computations.

This is the inherent version of Semimonad::bind, accepting FnOnce instead of Fn since it consumes self.

§Type Signature

forall A B. (Identity A, A -> Identity B) -> Identity B

§Type Parameters
  • B: The type of the result of the chained computation.
§Parameters
  • self: The identity instance.
  • f: The function to apply to the value inside the identity.
§Returns

The result of applying f to the value.

§Examples
use fp_library::types::*;

let x = Identity(5);
let y = x.bind(|i| Identity(i * 2));
assert_eq!(y, Identity(10));
Source

pub fn fold_right<B>(self, f: impl FnOnce(A, B) -> B, initial: B) -> B

Folds the identity from the right.

See Foldable::fold_right for the type class version.

§Type Signature

forall A B. (Identity A, (A, B) -> B, B) -> B

§Type Parameters
  • B: The type of the accumulator.
§Parameters
  • self: The identity instance.
  • f: The function to apply to the element and the accumulator.
  • initial: The initial value of the accumulator.
§Returns

The final accumulator value.

§Examples
use fp_library::types::*;

let x = Identity(5);
let y = x.fold_right(|a, b| a + b, 10);
assert_eq!(y, 15);
Source

pub fn fold_left<B>(self, f: impl FnOnce(B, A) -> B, initial: B) -> B

Folds the identity from the left.

See Foldable::fold_left for the type class version.

§Type Signature

forall A B. (Identity A, (B, A) -> B, B) -> B

§Type Parameters
  • B: The type of the accumulator.
§Parameters
  • self: The identity instance.
  • f: The function to apply to the accumulator and the element.
  • initial: The initial value of the accumulator.
§Returns

The final accumulator value.

§Examples
use fp_library::types::*;

let x = Identity(5);
let y = x.fold_left(|b, a| b + a, 10);
assert_eq!(y, 15);
Source

pub fn fold_map<M>(self, f: impl FnOnce(A) -> M) -> M

Maps the value to a monoid and returns it.

See Foldable::fold_map for the type class version.

§Type Signature

forall A M. (Identity A, A -> M) -> M

§Type Parameters
  • M: The monoid type.
§Parameters
  • self: The identity instance.
  • f: The mapping function.
§Returns

The monoid value.

§Examples
use fp_library::types::*;

let x = Identity(5);
let y = x.fold_map(|a: i32| a.to_string());
assert_eq!(y, "5".to_string());
Source§

impl<'a, A: 'a> Identity<A>

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The type of the wrapped value.
Source

pub fn traverse<B: 'a + Clone, F: Applicative>( self, f: impl Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Identity<B>>
where Identity<B>: Clone,

Traverses the identity with an applicative function.

See Traversable::traverse for the type class version.

§Type Signature

forall A B F. Applicative F => (Identity A, A -> F B) -> F (Identity B)

§Type Parameters
  • B: The type of the elements in the resulting identity.
  • F: The applicative context.
§Parameters
  • self: The identity instance.
  • f: The function to apply, returning a value in an applicative context.
§Returns

The identity wrapped in the applicative context.

§Examples
use fp_library::{
	brands::*,
	types::*,
};

let x = Identity(5);
let y = x.traverse::<_, OptionBrand>(|a| Some(a * 2));
assert_eq!(y, Some(Identity(10)));
Source

pub fn sequence<InnerA: 'a + Clone, F: Applicative>( self, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Identity<InnerA>>
where A: Into<<F as Kind_cdc7cd43dac7585f>::Of<'a, InnerA>>, Identity<InnerA>: Clone,

Sequences an identity containing an applicative value.

See Traversable::sequence for the type class version.

§Type Signature

forall A InnerA F. (Applicative F, Into A) => Identity A -> F (Identity InnerA)

§Type Parameters
  • InnerA: The inner type wrapped in the applicative context.
  • F: The applicative context.
§Returns

The identity wrapped in the applicative context.

§Examples
use fp_library::{
	brands::*,
	types::*,
};

let x = Identity(Some(5));
let y: Option<Identity<i32>> = x.sequence::<i32, OptionBrand>();
assert_eq!(y, Some(Identity(5)));

Trait Implementations§

Source§

impl<A: Clone> Clone for Identity<A>

Source§

fn clone(&self) -> Identity<A>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug> Debug for Identity<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: Default> Default for Identity<A>

Source§

fn default() -> Identity<A>

Returns the “default value” for a type. Read more
Source§

impl<'de, A> Deserialize<'de> for Identity<A>
where A: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<A: Hash> Hash for Identity<A>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A: Ord> Ord for Identity<A>

Source§

fn cmp(&self, other: &Identity<A>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<A: PartialEq> PartialEq for Identity<A>

Source§

fn eq(&self, other: &Identity<A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: PartialOrd> PartialOrd for Identity<A>

Source§

fn partial_cmp(&self, other: &Identity<A>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<A> Serialize for Identity<A>
where A: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<A: Copy> Copy for Identity<A>

Source§

impl<A: Eq> Eq for Identity<A>

Source§

impl<A> StructuralPartialEq for Identity<A>

Auto Trait Implementations§

§

impl<A> Freeze for Identity<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for Identity<A>
where A: RefUnwindSafe,

§

impl<A> Send for Identity<A>
where A: Send,

§

impl<A> Sync for Identity<A>
where A: Sync,

§

impl<A> Unpin for Identity<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for Identity<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for Identity<A>
where A: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pipe for T

Source§

fn pipe<B>(self, f: impl FnOnce(Self) -> B) -> B

Pipes self into a function, enabling left-to-right composition. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,