Skip to main content

ReversedOptic

Struct ReversedOptic 

Source
pub struct ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where PointerBrand: ToDynCloneFn, S: 'a, T: 'a, A: 'a, B: 'a,
{ /* private fields */ }
Expand description

A reversed optic, produced by the reverse combinator.

ReversedOptic wraps an inner optic and implements reversed optic traits by evaluating the inner optic with ReverseBrand<ConcreteP> as the profunctor.

Corresponds to PureScript’s re :: Optic (Re p a b) s t a b -> Optic p b a t s.

The reversed optic swaps the roles of source/target and focus types:

  • An optic S -> T, A -> B becomes B -> A, T -> S (for review)
  • A simple optic S <-> A becomes A <-> S (for getter/fold)

§Type Parameters

  • 'a: The lifetime of the values.
  • PointerBrand: The cloneable function pointer brand used by the Reverse profunctor.
  • S: The source type of the original optic.
  • T: The target type of the original optic.
  • A: The focus source type of the original optic.
  • B: The focus target type of the original optic.
  • O: The inner optic type.

Trait Implementations§

Source§

impl<'a, PointerBrand, S, A, O> FoldOptic<'a, A, S> for ReversedOptic<'a, PointerBrand, S, S, A, A, O>
where PointerBrand: ToDynCloneFn + 'static, O: PrismOptic<'a, S, S, A, A>, S: 'a + 'static, A: 'a + 'static,

FoldOptic for ReversedOptic - reversing prism-like optics to folds.

Same as the GetterOptic implementation but with the additional R: Monoid + Clone bound required by FoldOptic.

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The cloneable function pointer brand.
  • S: The source type of the original (simple) optic.
  • A: The focus type of the original (simple) optic.
  • O: The inner optic type.
Source§

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

Evaluates the reversed optic with Forget for a monoidal fold in the reverse direction.

§Type Signature

forall PointerBrand S A O R Q. (Monoid R, ToDynCloneFn Q, ToDynCloneFn PointerBrand, PrismOptic O) => (&ReversedOptic PointerBrand S A O, Forget Q R S S) -> Forget Q R A A

§Type Parameters
  • R: The monoid type.
  • Q: The reference-counted pointer type.
§Parameters
  • &self: The reversed optic instance.
  • pab: The forget profunctor value.
§Returns

The transformed forget profunctor value.

§Examples
use fp_library::{
	brands::{
		optics::*,
		*,
	},
	classes::optics::FoldOptic,
	types::optics::{
		Forget,
		PrismPrime,
		reverse,
	},
};

// reverse(prism) as a fold with String monoid
let some_prism: PrismPrime<RcBrand, Option<i32>, i32> = PrismPrime::from_option(|o| o, Some);
let reversed = reverse::<RcBrand, _, _, _, _, _>(some_prism);
let forget = Forget::<RcBrand, String, Option<i32>, Option<i32>>::new(|o: Option<i32>| {
	format!("{:?}", o)
});
let result = FoldOptic::evaluate::<String, RcBrand>(&reversed, forget);
assert_eq!(result.run(42), "Some(42)");
Source§

impl<'a, PointerBrand, S, A, O> GetterOptic<'a, A, S> for ReversedOptic<'a, PointerBrand, S, S, A, A, O>
where PointerBrand: ToDynCloneFn + 'static, O: PrismOptic<'a, S, S, A, A>, S: 'a + 'static, A: 'a + 'static,

GetterOptic for ReversedOptic - reversing prism-like optics to getters.

ReverseBrand<ForgetBrand<Q, R>> has Choice (from ForgetBrand: Cochoice) but NOT Strong (since ForgetBrand is not Costrong), so only PrismOptic’s P: Choice bound can be satisfied.

This means reverse(prism) and reverse(iso) produce getters, but reverse(lens) does not. This matches PureScript semantics.

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The cloneable function pointer brand.
  • S: The source type of the original (simple) optic.
  • A: The focus type of the original (simple) optic.
  • O: The inner optic type.
Source§

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

Evaluates the reversed optic with Forget, producing a getter in the reverse direction.

§Type Signature

forall PointerBrand S A O R Q. (ToDynCloneFn Q, ToDynCloneFn PointerBrand, PrismOptic O) => (&ReversedOptic PointerBrand S A O, Forget Q R S S) -> Forget Q R A A

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

The transformed forget profunctor value.

§Examples
use fp_library::{
	brands::{
		optics::*,
		*,
	},
	classes::optics::GetterOptic,
	types::optics::{
		Forget,
		PrismPrime,
		reverse,
	},
};

// reverse(prism) as a getter: given A, extract S via the prism's review
let some_prism: PrismPrime<RcBrand, Option<i32>, i32> = PrismPrime::from_option(|o| o, Some);
let reversed = reverse::<RcBrand, _, _, _, _, _>(some_prism);
let forget = Forget::<RcBrand, Option<i32>, Option<i32>, Option<i32>>::new(|o| o);
let result = GetterOptic::evaluate::<Option<i32>, RcBrand>(&reversed, forget);
assert_eq!(result.run(42), Some(42));
Source§

impl<'a, PointerBrand, S, T, A, B, O> IsoOptic<'a, B, A, T, S> for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where PointerBrand: ToDynCloneFn + 'static, O: IsoOptic<'a, S, T, A, B>, S: 'a + 'static, T: 'a + 'static, A: 'a + 'static, B: 'a + 'static,

IsoOptic for ReversedOptic - reversing an iso to an iso.

ReverseBrand<P> has Profunctor whenever P: Profunctor, which is all that IsoOptic::evaluate requires. This means reverse(iso) is itself an iso, matching the PureScript semantics where re :: Iso s t a b -> Iso b a t s.

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The cloneable function pointer brand.
  • S: The source type of the original optic.
  • T: The target type of the original optic.
  • A: The focus source type of the original optic.
  • B: The focus target type of the original optic.
  • O: The inner optic type.
Source§

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

Evaluates the reversed optic with any profunctor, producing an iso in the reverse direction.

§Type Signature

forall PointerBrand S T A B O P. (Profunctor P, ToDynCloneFn PointerBrand, IsoOptic O) => (&ReversedOptic PointerBrand S T A B O, P T S) -> P B A

§Type Parameters
  • P: The profunctor type.
§Parameters
  • &self: The reversed optic instance.
  • pab: The profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use fp_library::{
	brands::{
		optics::*,
		*,
	},
	classes::optics::IsoOptic,
	functions::lift_fn_new,
	types::optics::{
		IsoPrime,
		reverse,
	},
};

// An iso between (i32,) and i32
let iso: IsoPrime<RcBrand, (i32,), i32> = IsoPrime::new(|(x,)| x, |x| (x,));
let reversed = reverse::<RcBrand, _, _, _, _, _>(iso);
// reverse(iso) is itself an iso from i32 to (i32,)
let f = lift_fn_new::<RcFnBrand, _, _>(|(x,): (i32,)| (x * 2,));
let modifier = IsoOptic::evaluate::<RcFnBrand>(&reversed, f);
assert_eq!(modifier(21), 42);
Source§

impl<'a, PointerBrand, S, T, A, B, O> ReviewOptic<'a, B, A, T, S> for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where PointerBrand: ToDynCloneFn + 'static, O: LensOptic<'a, S, T, A, B>, S: 'a + 'static, T: 'a + 'static, A: 'a + 'static, B: 'a + 'static,

ReviewOptic for ReversedOptic - reversing any optic >= Lens.

ReverseBrand<TaggedBrand> has Strong (from TaggedBrand: Costrong), satisfying the P: Strong bound required by LensOptic::evaluate.

This covers Iso and Lens, matching the PureScript semantics where Re Tagged has Strong (from Tagged: Costrong) but not Choice (since Tagged does not implement Cochoice).

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The cloneable function pointer brand.
  • S: The source type of the original optic.
  • T: The target type of the original optic.
  • A: The focus source type of the original optic.
  • B: The focus target type of the original optic.
  • O: The inner optic type.
Source§

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

Evaluates the reversed optic with Tagged, producing a review in the reverse direction.

§Type Signature

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

§Parameters
  • &self: The reversed optic instance.
  • pab: The tagged profunctor value.
§Returns

The transformed tagged profunctor value.

§Examples
use fp_library::{
	brands::{
		optics::*,
		*,
	},
	classes::optics::ReviewOptic,
	types::optics::{
		LensPrime,
		Tagged,
		reverse,
	},
};

// reverse(lens) as a review: given the source, extracts the focus.
let lens: LensPrime<RcBrand, (i32, String), i32> =
	LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
let reversed = reverse::<RcBrand, _, _, _, _, _>(lens);
let result = ReviewOptic::evaluate(&reversed, Tagged::new((42, "hello".to_string())));
assert_eq!(result.0, 42);

Auto Trait Implementations§

§

impl<'a, PointerBrand, S, T, A, B, O> Freeze for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where O: Freeze,

§

impl<'a, PointerBrand, S, T, A, B, O> RefUnwindSafe for ReversedOptic<'a, PointerBrand, S, T, A, B, O>

§

impl<'a, PointerBrand, S, T, A, B, O> Send for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where O: Send, PointerBrand: Send, S: Send, T: Send, A: Send, B: Send,

§

impl<'a, PointerBrand, S, T, A, B, O> Sync for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where O: Sync, PointerBrand: Sync, S: Sync, T: Sync, A: Sync, B: Sync,

§

impl<'a, PointerBrand, S, T, A, B, O> Unpin for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where O: Unpin, PointerBrand: Unpin, S: Unpin, T: Unpin, A: Unpin, B: Unpin,

§

impl<'a, PointerBrand, S, T, A, B, O> UnsafeUnpin for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where O: UnsafeUnpin,

§

impl<'a, PointerBrand, S, T, A, B, O> UnwindSafe for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
where O: UnwindSafe, PointerBrand: UnwindSafe, S: UnwindSafe, T: UnwindSafe, A: UnwindSafe, B: 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> 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, 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.