https://github.com/rockneurotiko/async-await
Just some macros to emulate the Async and Await in Rust :)
https://github.com/rockneurotiko/async-await
Last synced: 2 months ago
JSON representation
Just some macros to emulate the Async and Await in Rust :)
- Host: GitHub
- URL: https://github.com/rockneurotiko/async-await
- Owner: rockneurotiko
- License: apache-2.0
- Created: 2015-09-08T15:54:58.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-05-09T08:26:31.000Z (about 7 years ago)
- Last Synced: 2025-04-01T21:03:52.661Z (3 months ago)
- Language: Rust
- Size: 13.7 KB
- Stars: 38
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
Async-Await
==========[](https://crates.io/crates/async-await)
[ Deprecated in favour to the standard RFC that just has been approved:](https://github.com/rust-lang/rfcs/blob/master/text/2394-async_await.md)
[https://github.com/rust-lang/rfcs/blob/master/text/2394-async_await.md](https://github.com/rust-lang/rfcs/blob/master/text/2394-async_await.md)
Just two macros to emulate a simple Async and Await using Futures (provided by [futures-rs](https://github.com/alexcrichton/futures-rs) thanks to @cmgriffing for port it).
## Usage
This is available in `crates.io`. Add this to your `Cargo.toml`:
```
[dependencies]
async-await = "0.2.1"
```## Example
Here is a simple example, you need to do the `#[macro_use]` and `use async_await::*;` because of the expansion of the macros :)
```rust
#[macro_use]
extern crate async_await;use async_await::*;
fn main() {
let computation = async!{"Hello world!"};
println!("{}", await!(computation));
}
```Another example using hyper, a shared client and a block in async :)
```rust
#[macro_use]
extern crate async_await;
extern crate hyper;use std::io::Read;
use std::sync::Arc;use async_await::*;
use hyper::Client;
use hyper::header::Connection;fn main() {
let client = Arc::new(Client::new());let client_comp = client.clone();
let computation = async!{{
let mut res = client_comp.get("http://rust-lang.org/")
.header(Connection::close())
.send().unwrap();
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
body
}};println!("Before await!");
println!("{}", await!{computation});
println!("After await!");
}
```You can also provide a default value in case that the computation fails:
```rust
#[macro_use]
extern crate async_await;use async_await::*;
fn main() {
let computation = async!{panic!(":()")};
assert_eq!("Recovered!", await!{computation, "Recovered!"});
}
```## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.