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>
impl<S> Deferred<S>
sourcepub fn can_resume(&self) -> bool
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);
sourcepub fn state(&self) -> Option<&S>
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));
sourcepub fn resume(self) -> Option<Self>
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));