deferred 1.1.0

Rust crate to help perform deferred execution of code logic.
Documentation
  • Coverage
  • 82.98%
    39 out of 47 items documented15 out of 35 items with examples
  • Size
  • Source code size: 38.23 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.82 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • PsichiX/deferred
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • PsichiX

Deferred

Rust crate to help perform deferred execution of code logic.

Travis CI Docs.rs Crates.io

Idea

This crate can be used specifically for operations where you need to execute different parts of logic at unspecified time but you cannot use futures or any other asynchronous operations.

Usage

Record in Cargo.toml:

[dependencies]
deferred = "1.1"

Your crate module:

#[macro_use]
extern crate deferred;

use deferred::*;

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));
}
// IS EQUIVALENT TO:
{
  let d = foo(1);
  assert_eq!(d.consume(), 14);
}