Step

Enum Step 

Source
pub enum Step<Y, D> {
    Yielded(Y),
    Complete(D),
}
Expand description

Result of a computation step, either yielding a value to continue or completing with a final value.

Step is the return type for coroutine computations, similar to how Option represents optional values and Result represents fallible operations.

§Examples

use sans::Step;

let continuing: Step<i32, String> = Step::Yielded(42);
let completed: Step<i32, String> = Step::Complete("finished".to_string());

// Using combinators
let doubled = continuing.map_yielded(|x| x * 2);
assert_eq!(doubled, Step::Yielded(84));

Variants§

§

Yielded(Y)

Continue computation with an intermediate yield value

§

Complete(D)

Complete computation with a final value

Implementations§

Source§

impl<Y, D> Step<Y, D>

Source

pub const fn is_yielded(&self) -> bool

Returns true if the step is Yielded.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert!(x.is_yielded());

let y: Step<i32, &str> = Step::Complete("complete");
assert!(!y.is_yielded());
Source

pub const fn is_complete(&self) -> bool

Returns true if the step is Complete.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert!(x.is_complete());

let y: Step<i32, &str> = Step::Yielded(42);
assert!(!y.is_complete());
Source

pub fn yielded_value(self) -> Option<Y>

Converts from Step<Y, D> to Option<Y>.

Converts self into an Option<Y>, consuming self, and discarding the complete value, if any.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.yielded_value(), Some(42));

let y: Step<i32, &str> = Step::Complete("complete");
assert_eq!(y.yielded_value(), None);
Source

pub fn complete_value(self) -> Option<D>

Converts from Step<Y, D> to Option<D>.

Converts self into an Option<D>, consuming self, and discarding the yield value, if any.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert_eq!(x.complete_value(), Some("complete"));

let y: Step<i32, &str> = Step::Yielded(42);
assert_eq!(y.complete_value(), None);
Source

pub fn map_complete<D2, F>(self, f: F) -> Step<Y, D2>
where F: FnOnce(D) -> D2,

Maps a Step<Y, D> to Step<Y, D2> by applying a function to the complete value.

§Examples
use sans::Step;

let x: Step<i32, i32> = Step::Complete(5);
assert_eq!(x.map_complete(|v| v * 2), Step::Complete(10));

let y: Step<i32, i32> = Step::Yielded(3);
assert_eq!(y.map_complete(|v| v * 2), Step::Yielded(3));
Source

pub fn map_yielded<Y2, F>(self, f: F) -> Step<Y2, D>
where F: FnOnce(Y) -> Y2,

Maps a Step<Y, D> to Step<Y2, D> by applying a function to the yielded value.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.map_yielded(|v| v * 2), Step::Yielded(84));

let y: Step<i32, &str> = Step::Complete("complete");
assert_eq!(y.map_yielded(|v: i32| v * 2), Step::Complete("complete"));
Source

pub fn map<Y2, D2, FY, FD>(self, fy: FY, fd: FD) -> Step<Y2, D2>
where FY: FnOnce(Y) -> Y2, FD: FnOnce(D) -> D2,

Maps a Step<Y, D> to Step<Y2, D2> by applying functions to both values.

§Examples
use sans::Step;

let x: Step<i32, i32> = Step::Yielded(42);
assert_eq!(x.map(|y| y * 2, |d| d + 1), Step::Yielded(84));

let y: Step<i32, i32> = Step::Complete(10);
assert_eq!(y.map(|y| y * 2, |d| d + 1), Step::Complete(11));
Source

pub fn yielded_or(self, default: Y) -> Y

Returns the yielded value or a default.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.yielded_or(0), 42);

let y: Step<i32, &str> = Step::Complete("complete");
assert_eq!(y.yielded_or(0), 0);
Source

pub fn yielded_or_else<F>(self, f: F) -> Y
where F: FnOnce() -> Y,

Returns the yielded value or computes it from a closure.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.yielded_or_else(|| 0), 42);

let y: Step<i32, &str> = Step::Complete("complete");
assert_eq!(y.yielded_or_else(|| 0), 0);
Source

pub fn complete_or(self, default: D) -> D

Returns the complete value or a default.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert_eq!(x.complete_or("default"), "complete");

let y: Step<i32, &str> = Step::Yielded(42);
assert_eq!(y.complete_or("default"), "default");
Source

pub fn complete_or_else<F>(self, f: F) -> D
where F: FnOnce() -> D,

Returns the complete value or computes it from a closure.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert_eq!(x.complete_or_else(|| "default"), "complete");

let y: Step<i32, &str> = Step::Yielded(42);
assert_eq!(y.complete_or_else(|| "default"), "default");
Source

pub const fn as_ref(&self) -> Step<&Y, &D>

Converts from &Step<Y, D> to Step<&Y, &D>.

§Examples
use sans::Step;

let x: Step<i32, String> = Step::Yielded(42);
assert_eq!(x.as_ref(), Step::Yielded(&42));

let y: Step<i32, String> = Step::Complete("complete".to_string());
assert_eq!(y.as_ref(), Step::Complete(&"complete".to_string()));
Source

pub fn as_mut(&mut self) -> Step<&mut Y, &mut D>

Converts from &mut Step<Y, D> to Step<&mut Y, &mut D>.

§Examples
use sans::Step;

let mut x: Step<i32, String> = Step::Yielded(42);
if let Step::Yielded(y) = x.as_mut() {
    *y = 100;
}
assert_eq!(x, Step::Yielded(100));
Source

pub fn flip(self) -> Step<D, Y>

Converts from Step<Y, D> to Step<D, Y> by swapping variants.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.flip(), Step::Complete(42));

let y: Step<i32, &str> = Step::Complete("complete");
assert_eq!(y.flip(), Step::Yielded("complete"));
Source

pub fn contains_yielded<U>(&self, y: &U) -> bool
where U: PartialEq<Y>,

Returns true if the step is a Yielded value containing the given value.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert!(x.contains_yielded(&42));
assert!(!x.contains_yielded(&100));

let y: Step<i32, &str> = Step::Complete("complete");
assert!(!y.contains_yielded(&42));
Source

pub fn contains_complete<U>(&self, d: &U) -> bool
where U: PartialEq<D>,

Returns true if the step is a Complete value containing the given value.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert!(x.contains_complete(&"complete"));
assert!(!x.contains_complete(&"other"));

let y: Step<i32, &str> = Step::Yielded(42);
assert!(!y.contains_complete(&"complete"));
Source

pub fn expect_yielded(self, msg: &str) -> Y

Returns the contained Yielded value, consuming the self value.

§Panics

Panics if the value is a Complete with a custom panic message provided by msg.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.expect_yielded("was complete"), 42);
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
x.expect_yielded("the world is ending"); // panics with "the world is ending"
Source

pub fn expect_complete(self, msg: &str) -> D

Returns the contained Complete value, consuming the self value.

§Panics

Panics if the value is a Yielded with a custom panic message provided by msg.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert_eq!(x.expect_complete("was yielding"), "complete");
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
x.expect_complete("the world is ending"); // panics with "the world is ending"
Source

pub fn unwrap_yielded(self) -> Y

Returns the contained Yielded value, consuming the self value.

§Panics

Panics if the value is a Complete.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
assert_eq!(x.unwrap_yielded(), 42);
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
x.unwrap_yielded(); // panics
Source

pub fn unwrap_complete(self) -> D

Returns the contained Complete value, consuming the self value.

§Panics

Panics if the value is a Yielded.

§Examples
use sans::Step;

let x: Step<i32, &str> = Step::Complete("complete");
assert_eq!(x.unwrap_complete(), "complete");
use sans::Step;

let x: Step<i32, &str> = Step::Yielded(42);
x.unwrap_complete(); // panics

Trait Implementations§

Source§

impl<Y: Clone, D: Clone> Clone for Step<Y, D>

Source§

fn clone(&self) -> Step<Y, D>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Y: Debug, D: Debug> Debug for Step<Y, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Y: Hash, D: Hash> Hash for Step<Y, D>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<I, O, S> InitSans<I, O> for Step<(O, S), S::Return>
where S: Sans<I, O>,

Source§

type Next = S

Source§

fn init(self) -> Step<(O, S), S::Return>

Execute the first coroutine. Read more
Source§

fn chain<R>(self, r: R) -> Chain<Self, R>
where Self: Sized, Self::Next: Sans<I, O, Return = I>, R: Sans<I, O>,

Chain with a coroutine.
Source§

fn chain_once<F>(self, f: F) -> Chain<Self, Once<F>>
where Self: Sized, Self::Next: Sans<I, O, Return = I>, F: FnOnce(<Self::Next as Sans<I, O>>::Return) -> O,

Chain with a function that executes once.
Source§

fn chain_repeat<F>(self, f: F) -> Chain<Self, Repeat<F>>
where Self: Sized, Self::Next: Sans<I, O, Return = I>, F: FnMut(<Self::Next as Sans<I, O>>::Return) -> O,

Chain with a function that repeats indefinitely.
Source§

fn map_input<I2, F>(self, f: F) -> MapInput<Self, F>
where Self: Sized, F: FnMut(I2) -> I,

Transform inputs before they reach the underlying coroutine.
Source§

fn map_yield<O2, F>(self, f: F) -> MapYield<Self, F, I, O>
where Self: Sized, F: FnMut(O) -> O2,

Transform yielded values before returning them.
Source§

fn map_done<D2, F>(self, f: F) -> MapReturn<Self, F>
where Self: Sized, F: FnMut(<Self::Next as Sans<I, O>>::Return) -> D2,

Transform the final result when completing.
Source§

fn into_iter(self) -> InitSansIter<O, Self>
where Self: Sized + InitSans<(), O>,

Convert to an iterator.
Source§

impl<Y: Ord, D: Ord> Ord for Step<Y, D>

Source§

fn cmp(&self, other: &Step<Y, D>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<Y: PartialEq, D: PartialEq> PartialEq for Step<Y, D>

Source§

fn eq(&self, other: &Step<Y, D>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Y: PartialOrd, D: PartialOrd> PartialOrd for Step<Y, D>

Source§

fn partial_cmp(&self, other: &Step<Y, D>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<Y: Copy, D: Copy> Copy for Step<Y, D>

Source§

impl<Y: Eq, D: Eq> Eq for Step<Y, D>

Source§

impl<Y, D> StructuralPartialEq for Step<Y, D>

Auto Trait Implementations§

§

impl<Y, D> Freeze for Step<Y, D>
where Y: Freeze, D: Freeze,

§

impl<Y, D> RefUnwindSafe for Step<Y, D>

§

impl<Y, D> Send for Step<Y, D>
where Y: Send, D: Send,

§

impl<Y, D> Sync for Step<Y, D>
where Y: Sync, D: Sync,

§

impl<Y, D> Unpin for Step<Y, D>
where Y: Unpin, D: Unpin,

§

impl<Y, D> UnwindSafe for Step<Y, D>
where Y: UnwindSafe, D: 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> 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<I, O, S> TryInitSans<I, O> for S
where S: InitSans<I, O>,

Source§

fn ok_map<P, E, T, F>(self, f: F) -> OkMap<Self, T::Next, F>
where Self: InitSans<I, O>, Self::Next: Sans<I, O, Return = Result<P, E>>, T: InitSans<I, O>, T::Next: Sans<I, O>, F: FnOnce(P) -> T,

Maps Ok return values through a function that produces an InitSans.
Source§

fn ok_and_then<P, Q, E, T, F>(self, f: F) -> OkAndThen<Self, T::Next, F>
where Self: InitSans<I, O>, Self::Next: Sans<I, O, Return = Result<P, E>>, T: InitSans<I, O>, T::Next: Sans<I, O, Return = Result<Q, E>>, F: FnOnce(P) -> T,

Chains through a function that produces an InitSans with a Result return type.
Source§

fn ok_chain<E, R>(self, next: R) -> OkChain<Self, R>
where Self: InitSans<I, O>, Self::Next: Sans<I, O, Return = Result<I, E>>, R: Sans<I, O>,

Chains to another coroutine only if the first returns Ok.
Source§

fn flatten<T, E>(self) -> Flatten<Self>
where Self: InitSans<I, O>, Self::Next: Sans<I, O, Return = Result<Result<T, E>, E>>,

Flattens nested Result types in the return value.
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.