Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/psichix/deferred
Rust crate to help perform deferred execution of code logic.
https://github.com/psichix/deferred
Last synced: 13 days ago
JSON representation
Rust crate to help perform deferred execution of code logic.
- Host: GitHub
- URL: https://github.com/psichix/deferred
- Owner: PsichiX
- License: mit
- Created: 2018-12-15T05:13:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-16T05:11:06.000Z (almost 6 years ago)
- Last Synced: 2024-10-16T05:31:35.899Z (28 days ago)
- Language: Rust
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deferred
Rust crate to help perform deferred execution of code logic.[![Travis CI](https://travis-ci.org/PsichiX/deferred.svg?branch=master)](https://travis-ci.org/PsichiX/deferred)
[![Docs.rs](https://docs.rs/deferred/badge.svg)](https://docs.rs/deferred)
[![Crates.io](https://img.shields.io/crates/v/deferred.svg)](https://crates.io/crates/deferred)# 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`:
```toml
[dependencies]
deferred = "1.1"
```Your crate module:
```rust
#[macro_use]
extern crate deferred;use deferred::*;
fn foo(v: i32) -> Deferred {
deferred!(v, [
|c| state!(c.state() + 1),
|c| foo2(c.state()).into(),
|c| state!(c.state() + 2)
])
}fn foo2(v: i32) -> Deferred {
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);
}
```