Skip to main content

LensPrime

Struct LensPrime 

Source
pub struct LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible, S: 'a, A: 'a,
{ /* private fields */ }
Expand description

A concrete lens type for accessing and updating a field in a structure where types do not change. This matches PureScript’s Lens' s a.

Uses FnBrand to support capturing closures.

§Type Parameters

  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.

Implementations§

Source§

impl<'a, PointerBrand, S: 'a, A: 'a> LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source

pub fn new( to: impl 'a + Fn(S) -> (A, <FnBrand<PointerBrand> as CloneableFn>::Of<'a, A, S>), ) -> Self

Create a new monomorphic lens. This matches PureScript’s lens' constructor.

§Type Signature

forall PointerBrand S A. UnsizedCoercible PointerBrand => (S -> (A, A -> S)) -> LensPrime PointerBrand S A

§Parameters
  • to: The getter/setter pair function.
§Returns

A new instance of the type.

§Examples
use fp_library::{
	brands::{
		RcBrand,
		RcFnBrand,
	},
	classes::CloneableFn,
	types::optics::LensPrime,
};

let l: LensPrime<RcBrand, i32, i32> =
	LensPrime::new(|x| (x, <RcFnBrand as CloneableFn>::new(|s| s)));
assert_eq!(l.view(42), 42);
Source

pub fn from_view_set( view: impl 'a + Fn(S) -> A, set: impl 'a + Fn((S, A)) -> S, ) -> Self
where S: Clone,

Create a new monomorphic lens from a getter and setter.

§Type Signature

forall PointerBrand S A. UnsizedCoercible PointerBrand => (S -> A, (S, A) -> S) -> LensPrime PointerBrand S A

§Parameters
  • view: The getter function.
  • set: The setter function.
§Returns

A new LensPrime instance.

§Examples
use fp_library::{
	brands::RcBrand,
	types::optics::LensPrime,
};

let l: LensPrime<RcBrand, i32, i32> = LensPrime::from_view_set(|x: i32| x, |(_, y)| y);
assert_eq!(l.view(10), 10);
Source

pub fn view(&self, s: S) -> A

View the focus of the lens in a structure.

§Type Signature

forall PointerBrand S A. UnsizedCoercible PointerBrand => (&LensPrime PointerBrand S A, S) -> A

§Parameters
  • &self: The monomorphic lens instance.
  • s: The structure to view.
§Returns

The focus value.

§Examples
use fp_library::{
	brands::RcBrand,
	types::optics::LensPrime,
};

let l: LensPrime<RcBrand, i32, i32> = LensPrime::from_view_set(|x: i32| x, |(_, y)| y);
assert_eq!(l.view(42), 42);
Source

pub fn set(&self, s: S, a: A) -> S

Set the focus of the lens in a structure.

§Type Signature

forall PointerBrand S A. UnsizedCoercible PointerBrand => (&LensPrime PointerBrand S A, S, A) -> S

§Parameters
  • &self: The monomorphic lens instance.
  • s: The structure to update.
  • a: The new value for the focus.
§Returns

The updated structure.

§Examples
use fp_library::{
	brands::RcBrand,
	types::optics::LensPrime,
};

let l: LensPrime<RcBrand, i32, i32> = LensPrime::from_view_set(|x: i32| x, |(_, y)| y);
assert_eq!(l.set(10, 20), 20);
Source

pub fn over(&self, s: S, f: impl Fn(A) -> A) -> S

Update the focus of the lens in a structure using a function.

§Type Signature

forall PointerBrand S A. UnsizedCoercible PointerBrand => (&LensPrime PointerBrand S A, S, A -> A) -> S

§Parameters
  • &self: The monomorphic lens instance.
  • s: The structure to update.
  • f: The function to apply to the focus.
§Returns

The updated structure.

§Examples
use fp_library::{
	brands::RcBrand,
	types::optics::LensPrime,
};

let l: LensPrime<RcBrand, i32, i32> = LensPrime::from_view_set(|x: i32| x, |(_, y)| y);
assert_eq!(l.over(10, |x| x + 1), 11);

Trait Implementations§

Source§

impl<'a, PointerBrand, S: 'a, A: 'a> AffineTraversalOptic<'a, S, S, A, A> for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate<Q: Strong + Choice>( &self, pab: <Q as Kind_266801a817966495>::Of<'a, A, A>, ) -> <Q as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall PointerBrand S A Q. (Strong Q, Choice Q, UnsizedCoercible PointerBrand) => (&LensPrime PointerBrand S A, Q A A) -> Q S S

§Type Parameters
  • Q: The profunctor type.
§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use {
	fp_library::{
		brands::{
			optics::*,
			*,
		},
		classes::optics::*,
		functions::*,
		types::optics::*,
	},
	std::rc::Rc,
};

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn((i32, String)) -> (i32, String)> =
	AffineTraversalOptic::evaluate::<RcFnBrand>(&l, f);
assert_eq!(modifier((21, "hello".to_string())), (42, "hello".to_string()));
Source§

impl<'a, PointerBrand, S, A> Clone for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible, S: 'a, A: 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn clone(&self) -> Self

§Type Signature

forall PointerBrand S A. UnsizedCoercible PointerBrand => &LensPrime PointerBrand S A -> LensPrime PointerBrand S A

§Returns

A new LensPrime instance that is a copy of the original.

§Examples
use fp_library::{
	brands::RcBrand,
	types::optics::LensPrime,
};

let l: LensPrime<RcBrand, i32, i32> = LensPrime::from_view_set(|x: i32| x, |(_, y)| y);
let cloned = l.clone();
assert_eq!(cloned.view(42), 42);
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, PointerBrand, S: 'a, A: 'a> FoldOptic<'a, S, A> for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate<R: 'a + Monoid + 'static, Q: UnsizedCoercible + 'static>( &self, pab: <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, A, A>, ) -> <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall PointerBrand S A R Q. (Monoid R, UnsizedCoercible Q, UnsizedCoercible PointerBrand) => (&LensPrime PointerBrand S A, Forget Q R A A) -> Forget Q R S S

§Type Parameters
  • R: The monoid type.
  • Q: The reference-counted pointer type.
§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

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

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = Forget::<RcBrand, String, i32, i32>::new(|x| x.to_string());
let folded = FoldOptic::evaluate(&l, f);
assert_eq!(folded.run((42, "hi".to_string())), "42".to_string());
Source§

impl<'a, PointerBrand, S: 'a, A: 'a> GetterOptic<'a, S, A> for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate<R: 'a + 'static, Q: UnsizedCoercible + 'static>( &self, pab: <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, A, A>, ) -> <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall PointerBrand S A R Q. (UnsizedCoercible Q, UnsizedCoercible PointerBrand) => (&LensPrime PointerBrand S A, Forget Q R A A) -> Forget Q R S S

§Type Parameters
  • R: The return type of the forget profunctor.
  • Q: The reference-counted pointer type.
§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

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

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = Forget::<RcBrand, i32, i32, i32>::new(|x| x);
let folded = GetterOptic::evaluate(&l, f);
assert_eq!(folded.run((42, "hi".to_string())), 42);
Source§

impl<'a, PointerBrand, S: 'a, A: 'a> LensOptic<'a, S, S, A, A> for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate<Q: Strong>( &self, pab: <Q as Kind_266801a817966495>::Of<'a, A, A>, ) -> <Q as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall PointerBrand S A Q. (Strong Q, UnsizedCoercible PointerBrand) => (&LensPrime PointerBrand S A, Q A A) -> Q S S

§Type Parameters
  • Q: The profunctor type.
§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use {
	fp_library::{
		brands::{
			optics::*,
			*,
		},
		classes::optics::*,
		functions::*,
		types::optics::*,
	},
	std::rc::Rc,
};

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn((i32, String)) -> (i32, String)> =
	LensOptic::evaluate::<RcFnBrand>(&l, f);
assert_eq!(modifier((21, "hello".to_string())), (42, "hello".to_string()));
Source§

impl<'a, Q, PointerBrand, S, A> Optic<'a, Q, S, S, A, A> for LensPrime<'a, PointerBrand, S, A>
where Q: Strong, PointerBrand: UnsizedCoercible, S: 'a, A: 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • Q: The profunctor type.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate( &self, pab: <Q as Kind_266801a817966495>::Of<'a, A, A>, ) -> <Q as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall Q PointerBrand S A. (Strong Q, UnsizedCoercible PointerBrand) => (&LensPrime Q PointerBrand S A, Q A A) -> Q S S

§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use {
	fp_library::{
		brands::{
			optics::*,
			*,
		},
		classes::optics::*,
		functions::*,
		types::optics::*,
	},
	std::rc::Rc,
};

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn((i32, String)) -> (i32, String)> =
	Optic::<RcFnBrand, _, _, _, _>::evaluate(&l, f);
assert_eq!(modifier((21, "hello".to_string())), (42, "hello".to_string()));
Source§

impl<'a, Q, PointerBrand, S: 'a, A: 'a> SetterOptic<'a, Q, S, S, A, A> for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible, Q: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • Q: The reference-counted pointer type for the setter brand.
  • PointerBrand: The reference-counted pointer type for the lens.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate( &self, pab: <FnBrand<Q> as Kind_266801a817966495>::Of<'a, A, A>, ) -> <FnBrand<Q> as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall Q PointerBrand S A. (UnsizedCoercible PointerBrand, UnsizedCoercible Q) => (&LensPrime Q PointerBrand S A, Fn Q A A) -> Fn Q S S

§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use {
	fp_library::{
		brands::{
			optics::*,
			*,
		},
		classes::optics::*,
		functions::*,
		types::optics::*,
	},
	std::rc::Rc,
};

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn((i32, String)) -> (i32, String)> =
	SetterOptic::<RcBrand, _, _, _, _>::evaluate(&l, f);
assert_eq!(modifier((21, "hello".to_string())), (42, "hello".to_string()));
Source§

impl<'a, PointerBrand, S: 'a, A: 'a> TraversalOptic<'a, S, S, A, A> for LensPrime<'a, PointerBrand, S, A>
where PointerBrand: UnsizedCoercible,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The type of the structure.
  • A: The type of the focus.
Source§

fn evaluate<Q: Wander>( &self, pab: <Q as Kind_266801a817966495>::Of<'a, A, A>, ) -> <Q as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall PointerBrand S A Q. (Wander Q, UnsizedCoercible PointerBrand) => (&LensPrime PointerBrand S A, Q A A) -> Q S S

§Type Parameters
  • Q: The profunctor type.
§Parameters
  • &self: The monomorphic lens instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use {
	fp_library::{
		brands::{
			optics::*,
			*,
		},
		classes::optics::*,
		functions::*,
		types::optics::*,
	},
	std::rc::Rc,
};

let l: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));

let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn((i32, String)) -> (i32, String)> =
	TraversalOptic::evaluate::<RcFnBrand>(&l, f);
assert_eq!(modifier((21, "hello".to_string())), (42, "hello".to_string()));

Auto Trait Implementations§

§

impl<'a, PointerBrand, S, A> Freeze for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + 'a>: Freeze,

§

impl<'a, PointerBrand, S, A> RefUnwindSafe for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + 'a>: RefUnwindSafe,

§

impl<'a, PointerBrand, S, A> Send for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + 'a>: Send,

§

impl<'a, PointerBrand, S, A> Sync for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + 'a>: Sync,

§

impl<'a, PointerBrand, S, A> Unpin for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + 'a>: Unpin,

§

impl<'a, PointerBrand, S, A> UnsafeUnpin for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + 'a>: UnsafeUnpin,

§

impl<'a, PointerBrand, S, A> UnwindSafe for LensPrime<'a, PointerBrand, S, A>
where <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(S) -> (A, <PointerBrand as RefCountedPointer>::CloneableOf<'a, dyn Fn(A) -> S + 'a>) + '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.