Deferred

Struct Deferred 

Source
pub struct Deferred<S> { /* private fields */ }
Expand description

Struct that holds parts and state of deferred logic to execute whenever you want to.

§Note

Everytime when you want to resume execution, you consume deferred context and produce new one so keep in mind to restore it before resume() and store it again after resume().

Implementations§

Source§

impl<S> Deferred<S>

Source

pub fn new(state: S, parts: Vec<Part<S>>) -> Self

Creates new deferred execution.

§Arguments
  • state - context initial state.
  • parts - vector of logic parts.
§Example
fn foo(v: i32) -> Deferred<i32> {
    deferred!(v, [
        |c| state!(c.state() + 1),
        |c| state!(c.state() + 2)
    ])
}

assert_eq!(foo(1).consume(), 4);
Source

pub fn can_resume(&self) -> bool

Tells if deferred execution can be resumed.

§Example
fn foo(v: i32) -> Deferred<i32> {
    deferred!(v, [
        |c| state!(c.state() + 1),
        |c| state!(c.state() + 2)
    ])
}

let d = foo(1);
assert_eq!(d.can_resume(), true);
let d = d.resume().unwrap();
assert_eq!(d.can_resume(), true);
let d = d.resume().unwrap();
assert_eq!(d.can_resume(), false);
Source

pub fn state(&self) -> Option<&S>

Gets reference to current state stored in context.

§Example
fn foo(v: i32) -> Deferred<i32> {
    deferred!(v, [
        |c| state!(c.state() + 1),
        |c| state!(c.state() + 2)
    ])
}

let d = foo(1);
assert_eq!(d.state(), Some(&1));
let d = d.resume().unwrap();
assert_eq!(d.state(), Some(&2));
let d = d.resume().unwrap();
assert_eq!(d.state(), Some(&4));
Source

pub fn resume(self) -> Option<Self>

Resumes deferred execution, which means we execute next logic part and store its state.

§Note

While you resume execution, you consume it and return new one so keep in mind that you need to store it again or replace with old one after calling resume().

§Example
fn foo(v: i32) -> Deferred<i32> {
    deferred!(v, [
        |c| state!(c.state() + 1),
        |c| foo2(c.state()).into(),
        |c| state!(c.state() + 2)
    ])
}

fn foo2(v: i32) -> Deferred<i32> {
    deferred!(v, [
        |c| state!(c.state() * 2),
        |c| state!(c.state() * 3)
    ])
}

let d = foo(1);
assert!(d.can_resume());
assert_eq!(d.state(), Some(&1));

let d = d.resume().unwrap();
assert!(d.can_resume());
assert_eq!(d.state(), Some(&2));

let d = d.resume().unwrap();
assert!(d.can_resume());
assert_eq!(d.state(), Some(&4));

let d = d.resume().unwrap();
assert!(d.can_resume());
assert_eq!(d.state(), Some(&12));

let d = d.resume().unwrap();
assert!(!d.can_resume());
assert_eq!(d.state(), Some(&14));
Source

pub fn consume(self) -> S

Consumes deferred execution, which means we execute all remaining logic parts and returns final state.

§Example
fn foo(v: i32) -> Deferred<i32> {
    deferred!(v, [
        |c| state!(c.state() + 1),
        |c| state!(c.state() + 2)
    ])
}

assert_eq!(foo(1).consume(), 4);
Source

pub fn unwrap(self) -> S

Alias for consume() method.

Trait Implementations§

Source§

impl<S> Into<Context<S>> for Deferred<S>

Source§

fn into(self) -> Context<S>

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

§

impl<S> Freeze for Deferred<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Deferred<S>
where S: RefUnwindSafe,

§

impl<S> Send for Deferred<S>
where S: Send,

§

impl<S> Sync for Deferred<S>
where S: Sync,

§

impl<S> Unpin for Deferred<S>
where S: Unpin,

§

impl<S> UnwindSafe for Deferred<S>
where S: 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, 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.