Skip to main content

Prism

Struct Prism 

Source
pub struct Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn, S: 'a, T: 'a, A: 'a, B: 'a,
{ pub preview: <FnBrand<PointerBrand> as Kind_266801a817966495>::Of<'a, S, Result<A, T>>, pub review: <FnBrand<PointerBrand> as Kind_266801a817966495>::Of<'a, B, T>, }
Expand description

A polymorphic prism for sum types where types can change. This matches PureScript’s Prism s t a b.

A prism focuses on a value that may not be present (like a particular variant of an enum). Uses FnBrand to support capturing closures.

§Type Parameters

  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.

Fields§

§preview: <FnBrand<PointerBrand> as Kind_266801a817966495>::Of<'a, S, Result<A, T>>

Preview function: tries to extract the focus, returning the target structure T on failure.

§review: <FnBrand<PointerBrand> as Kind_266801a817966495>::Of<'a, B, T>

Review function: constructs the structure from a focus value.

Implementations§

Source§

impl<'a, PointerBrand, S: 'a, T: 'a, A: 'a, B: 'a> Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source

pub fn new( preview: impl 'a + Fn(S) -> Result<A, T>, review: impl 'a + Fn(B) -> T, ) -> Self

Create a new polymorphic prism.

§Type Signature

forall PointerBrand S T A B. ToDynCloneFn PointerBrand => (S -> Result A T, B -> T) -> Prism PointerBrand S T A B

§Parameters
  • preview: The preview function.
  • review: The review function.
§Returns

A new instance of the type.

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<f64>, i32, f64> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |f| Some(f));
assert_eq!(ok_prism.preview(Some(42)), Ok(42));
Source

pub fn preview(&self, s: S) -> Result<A, T>

Preview the focus of the prism in a structure.

§Type Signature

forall PointerBrand S T A B. ToDynCloneFn PointerBrand => (&Prism PointerBrand S T A B, S) -> Result A T

§Parameters
  • &self: The prism instance.
  • s: The structure to preview.
§Returns

A result containing the focus value if it exists, or the original structure (possibly with changed type) if not.

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<f64>, i32, f64> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |f| Some(f));
assert_eq!(ok_prism.preview(Some(42)), Ok(42));
Source

pub fn review(&self, b: B) -> T

Review the focus into the structure.

§Type Signature

forall PointerBrand S T A B. ToDynCloneFn PointerBrand => (&Prism PointerBrand S T A B, B) -> T

§Parameters
  • &self: The prism instance.
  • b: The focus value.
§Returns

The structure containing the focus value.

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<f64>, i32, f64> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |f| Some(f));
assert_eq!(ok_prism.review(42.0), Some(42.0));

Trait Implementations§

Source§

impl<'a, PointerBrand, S: 'a, T: 'a, A: 'a, B: 'a> AffineTraversalOptic<'a, S, T, A, B> for Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

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

§Type Signature

forall PointerBrand S T A B Q. (Strong Q, Choice Q, ToDynCloneFn PointerBrand) => (&Prism PointerBrand S T A B, Q A B) -> Q S T

§Type Parameters
  • Q: The profunctor type.
§Parameters
  • &self: The prism 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 ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn(Option<i32>) -> Option<i32>> =
	AffineTraversalOptic::evaluate::<RcFnBrand>(&ok_prism, f);
assert_eq!(modifier(Some(21)), Some(42));
Source§

impl<'a, PointerBrand, S, T, A, B> Clone for Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn, S: 'a, T: 'a, A: 'a, B: 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

fn clone(&self) -> Self

§Type Signature

forall PointerBrand S T A B. ToDynCloneFn PointerBrand => &Prism PointerBrand S T A B -> Prism PointerBrand S T A B

§Returns

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

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<f64>, i32, f64> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |f| Some(f));
let cloned = ok_prism.clone();
assert_eq!(cloned.preview(Some(42)), Ok(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 Prism<'a, PointerBrand, S, S, A, A>
where PointerBrand: ToDynCloneFn,

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

fn evaluate<R: 'a + Monoid + 'static, Q: ToDynCloneFn + '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, ToDynCloneFn Q, ToDynCloneFn PointerBrand) => (&Prism 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 for the Forget brand.
§Parameters
  • &self: The prism instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

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

impl<'a, Q, PointerBrand, S, T, A, B> Optic<'a, Q, S, T, A, B> for Prism<'a, PointerBrand, S, T, A, B>
where Q: Choice, PointerBrand: ToDynCloneFn, S: 'a, T: 'a, A: 'a, B: 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • Q: The profunctor type.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

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

§Type Signature

forall Q PointerBrand S T A B. (Choice Q, ToDynCloneFn PointerBrand) => (&Prism Q PointerBrand S T A B, Q A B) -> Q S T

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

The transformed profunctor value.

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier = Optic::<RcFnBrand, _, _, _, _>::evaluate(&ok_prism, f);
assert_eq!(modifier(Some(21)), Some(42));
assert_eq!(modifier(None), None);
Source§

impl<'a, PointerBrand, S: 'a, T: 'a, A: 'a, B: 'a> PrismOptic<'a, S, T, A, B> for Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

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

§Type Signature

forall PointerBrand S T A B Q. (Choice Q, ToDynCloneFn PointerBrand) => (&Prism PointerBrand S T A B, Q A B) -> Q S T

§Type Parameters
  • Q: The profunctor type.
§Parameters
  • &self: The prism 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 ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn(Option<i32>) -> Option<i32>> =
	PrismOptic::evaluate::<RcFnBrand>(&ok_prism, f);
assert_eq!(modifier(Some(21)), Some(42));
Source§

impl<'a, PointerBrand, S: 'a, T: 'a, A: 'a, B: 'a> ReviewOptic<'a, S, T, A, B> for Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

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

§Type Signature

forall PointerBrand S T A B. ToDynCloneFn PointerBrand => (&Prism PointerBrand S T A B, Tagged A B) -> Tagged S T

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

The transformed profunctor value.

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

let ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

let f = Tagged::new(42);
let reviewed = ReviewOptic::evaluate(&ok_prism, f);
assert_eq!(reviewed.0, Some(42));
Source§

impl<'a, Q, PointerBrand, S: 'a, T: 'a, A: 'a, B: 'a> SetterOptic<'a, Q, S, T, A, B> for Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn, Q: ToDynCloneFn,

§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 prism.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

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

§Type Signature

forall Q PointerBrand S T A B. (ToDynCloneFn PointerBrand, ToDynCloneFn Q) => (&Prism Q PointerBrand S T A B, Fn Q A B) -> Fn Q S T

§Parameters
  • &self: The prism 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 ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn(Option<i32>) -> Option<i32>> =
	SetterOptic::<RcBrand, _, _, _, _>::evaluate(&ok_prism, f);
assert_eq!(modifier(Some(21)), Some(42));
Source§

impl<'a, PointerBrand, S: 'a, T: 'a, A: 'a, B: 'a> TraversalOptic<'a, S, T, A, B> for Prism<'a, PointerBrand, S, T, A, B>
where PointerBrand: ToDynCloneFn,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • S: The source type of the structure.
  • T: The target type of the structure after an update.
  • A: The source type of the focus.
  • B: The target type of the focus after an update.
Source§

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

§Type Signature

forall PointerBrand S T A B Q. (Wander Q, ToDynCloneFn PointerBrand) => (&Prism PointerBrand S T A B, Q A B) -> Q S T

§Type Parameters
  • Q: The profunctor type.
§Parameters
  • &self: The prism 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 ok_prism: Prism<RcBrand, Option<i32>, Option<i32>, i32, i32> =
	Prism::new(|o: Option<i32>| o.ok_or(None), |x| Some(x));

let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
let modifier: Rc<dyn Fn(Option<i32>) -> Option<i32>> =
	TraversalOptic::evaluate::<RcFnBrand>(&ok_prism, f);
assert_eq!(modifier(Some(21)), Some(42));

Auto Trait Implementations§

§

impl<'a, PointerBrand, S, T, A, B> Freeze for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: Freeze, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + 'a>: Freeze,

§

impl<'a, PointerBrand, S, T, A, B> RefUnwindSafe for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: RefUnwindSafe, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + 'a>: RefUnwindSafe,

§

impl<'a, PointerBrand, S, T, A, B> Send for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: Send, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + 'a>: Send,

§

impl<'a, PointerBrand, S, T, A, B> Sync for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: Sync, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + 'a>: Sync,

§

impl<'a, PointerBrand, S, T, A, B> Unpin for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: Unpin, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + 'a>: Unpin,

§

impl<'a, PointerBrand, S, T, A, B> UnsafeUnpin for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: UnsafeUnpin, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + 'a>: UnsafeUnpin,

§

impl<'a, PointerBrand, S, T, A, B> UnwindSafe for Prism<'a, PointerBrand, S, T, A, B>
where <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(S) -> Result<A, T> + 'a>: UnwindSafe, <PointerBrand as RefCountedPointer>::Of<'a, dyn Fn(B) -> T + '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.