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 -> BbecomesB -> A, T -> S(for review) - A simple optic
S <-> AbecomesA <-> S(for getter/fold)
§Type Parameters
'a: The lifetime of the values.PointerBrand: The cloneable function pointer brand used by theReverseprofunctor.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.
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>
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.
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>
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.
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>
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.
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>
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>where
O: RefUnwindSafe,
PointerBrand: RefUnwindSafe,
S: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<'a, PointerBrand, S, T, A, B, O> Send for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
impl<'a, PointerBrand, S, T, A, B, O> Sync for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
impl<'a, PointerBrand, S, T, A, B, O> Unpin for ReversedOptic<'a, PointerBrand, S, T, A, B, O>
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> 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> 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