Struct deferred::deferred::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§

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);

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);

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));

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));

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);

Alias for consume() method.

Trait Implementations§

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

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.