pub enum Step<A, B> {
Loop(A),
Done(B),
}Expand description
Represents the result of a single step in a tail-recursive computation.
This type is fundamental to stack-safe recursion via MonadRec.
§Higher-Kinded Type Representation
This type has multiple higher-kinded representations:
StepBrand: fully polymorphic over both loop and done types (bifunctor).StepWithLoopBrand<LoopType>: the loop type is fixed, polymorphic over the done type (functor over done).StepWithDoneBrand<DoneType>: the done type is fixed, polymorphic over the loop type (functor over loop).
§Serialization
This type supports serialization and deserialization via serde when the serde feature is enabled.
A: The “loop” type - when we returnLoop(a), we continue witha.B: The “done” type - when we returnDone(b), we’re finished.
§Variants
Loop(A): Continue the loop with a new value.Done(B): Finish the computation with a final value.
§Examples
use fp_library::types::*;
let loop_step: Step<i32, i32> = Step::Loop(10);
let done_step: Step<i32, i32> = Step::Done(20);Variants§
Implementations§
Source§impl<A, B> Step<A, B>
§Type Parameters
A: The “loop” type - when we return Loop(a), we continue with a.
B: The “done” type - when we return Done(b), we’re finished.
impl<A, B> Step<A, B>
§Type Parameters
A: The “loop” type - when we returnLoop(a), we continue witha.B: The “done” type - when we returnDone(b), we’re finished.
Sourcepub fn map_loop<C>(self, f: impl FnOnce(A) -> C) -> Step<C, B>
pub fn map_loop<C>(self, f: impl FnOnce(A) -> C) -> Step<C, B>
Maps a function over the Loop variant.
§Type Signature
forall A B C. (Step A B, A -> C) -> Step C B
§Type Parameters
C: The new loop type.
§Parameters
self: The step value.f: The function to apply to the loop value.
§Returns
A new Step with the loop value transformed.
§Examples
use fp_library::types::*;
let step: Step<i32, i32> = Step::Loop(1);
let mapped = step.map_loop(|x| x + 1);
assert_eq!(mapped, Step::Loop(2));Sourcepub fn map_done<C>(self, f: impl FnOnce(B) -> C) -> Step<A, C>
pub fn map_done<C>(self, f: impl FnOnce(B) -> C) -> Step<A, C>
Maps a function over the Done variant.
§Type Signature
forall A B C. (Step A B, B -> C) -> Step A C
§Type Parameters
C: The new done type.
§Parameters
self: The step value.f: The function to apply to the done value.
§Returns
A new Step with the done value transformed.
§Examples
use fp_library::types::*;
let step: Step<i32, i32> = Step::Done(1);
let mapped = step.map_done(|x| x + 1);
assert_eq!(mapped, Step::Done(2));Sourcepub fn bimap<C, D>(
self,
f: impl FnOnce(A) -> C,
g: impl FnOnce(B) -> D,
) -> Step<C, D>
pub fn bimap<C, D>( self, f: impl FnOnce(A) -> C, g: impl FnOnce(B) -> D, ) -> Step<C, D>
Applies functions to both variants (bifunctor map).
§Type Signature
forall A B C D. (Step A B, A -> C, B -> D) -> Step C D
§Type Parameters
C: The new loop type.D: The new done type.
§Parameters
self: The step value.f: The function to apply to the loop value.g: The function to apply to the done value.
§Returns
A new Step with both values transformed.
§Examples
use fp_library::types::*;
let step: Step<i32, i32> = Step::Loop(1);
let mapped = step.bimap(|x| x + 1, |x| x * 2);
assert_eq!(mapped, Step::Loop(2));Trait Implementations§
Source§impl<'de, A, B> Deserialize<'de> for Step<A, B>where
A: Deserialize<'de>,
B: Deserialize<'de>,
impl<'de, A, B> Deserialize<'de> for Step<A, B>where
A: Deserialize<'de>,
B: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl<A: Copy, B: Copy> Copy for Step<A, B>
impl<A: Eq, B: Eq> Eq for Step<A, B>
impl<A, B> StructuralPartialEq for Step<A, B>
Auto Trait Implementations§
impl<A, B> Freeze for Step<A, B>
impl<A, B> RefUnwindSafe for Step<A, B>where
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<A, B> Send for Step<A, B>
impl<A, B> Sync for Step<A, B>
impl<A, B> Unpin for Step<A, B>
impl<A, B> UnwindSafe for Step<A, B>where
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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