Last active
July 29, 2018 01:49
-
-
Save FrankHassanabad/91c742cb262c930e1463d2371f9d3e52 to your computer and use it in GitHub Desktop.
Rust futures examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
use futures::{future, Future}; | |
use std::*; | |
/// Returns an empty future, empty error | |
fn return_empty_result() -> impl Future<Item = (), Error = ()> { | |
future::lazy(|| future::ok::<(), ()>(())) | |
} | |
/// Returns an i32 future, empty error | |
fn return_i32() -> impl Future<Item = i32, Error = ()> { | |
future::lazy(|| future::ok::<i32, ()>(42)) | |
} | |
/// Returns whatever future and error and echos out a passed in parameter | |
fn return_whatever<T, U>(some_value: T) -> impl Future<Item = T, Error = U> { | |
future::lazy(|| future::ok::<T, U>(some_value)) | |
} | |
/// Returns whatever future and error as a string for the error | |
fn return_whatever_with_error_a_string<'a, T>( | |
some_value: T, | |
) -> impl Future<Item = T, Error = &'a str> { | |
future::lazy(|| future::ok::<T, &str>(some_value)) | |
} | |
/// Returns an error called "My inner error" which is a string based error | |
fn return_future_error<'a>() -> impl Future<Error = &'a str> { | |
future::lazy(|| future::err::<(), &str>("My inner error")) | |
} | |
/// Returns a specific error (format error for example) | |
fn return_future_fmt_error() -> impl Future<Item = (), Error = fmt::Error> { | |
future::lazy(|| { | |
let err = fmt::Error; | |
future::err::<(), fmt::Error>(err) | |
}) | |
} | |
/// Maps a fmt::Error to a &str | |
fn return_future_error_with_map<'a>() -> impl Future<Item = (), Error = &'a str> { | |
future::lazy(|| { | |
let err = fmt::Error; | |
future::err::<(), fmt::Error>(err) | |
}).map_err(|_| "Some string") | |
} | |
/// Some simple examples with futures | |
/// | |
/// Prints out: | |
/// Empty future returned: Ready(()) | |
/// Empty future returned: Ready(()) | |
/// My number returned is:42 | |
/// My string returned is:Yolo brolo | |
/// My string returned is:Yolo molo colo | |
/// My number returned is:42 | |
/// Future with the number but not mapped: Ready(42) | |
fn main() { | |
// Prints: Empty future returned: Ready(()) | |
let mut future = return_empty_result(); | |
let result = future.poll(); | |
println!("Empty future returned: {:?}", result.expect("Empty result")); | |
// Same as above but I combine everything | |
// Prints: Empty future returned: Ready(()) | |
println!( | |
"Empty future returned: {:?}", | |
return_empty_result().poll().expect("Empty result") | |
); | |
// Prints my_number of 42 out | |
return_i32() | |
.poll() | |
.expect("To get a value") | |
.map(|my_number| { | |
println!("My number returned is:{}", my_number); | |
}); | |
// My string returned is:Yolo brolo | |
return_whatever::<&str, &str>("Yolo brolo") | |
.poll() | |
.expect("To get a value") | |
.map(|my_number| { | |
println!("My string returned is:{}", my_number); | |
}); | |
// My string returned is:Yolo brolo | |
return_whatever_with_error_a_string("Yolo molo colo") | |
.poll() | |
.expect("To get a value") | |
.map(|my_number| { | |
println!("My string returned is:{}", my_number); | |
}); | |
// My number returned is: 42 | |
return_whatever_with_error_a_string(42) | |
.poll() | |
.expect("To get a value") | |
.map(|my_number| { | |
println!("My number returned is:{}", my_number); | |
}); | |
// This is my error I expect, inner is: "My inner error" | |
match return_future_error().poll() { | |
Ok(_) => println!("Reality violation, I am in an Ok that should not be possible"), | |
Err(err) => println!("This is my error I expect, inner is: {:?}", err), | |
}; | |
// This is my error I expect, inner is: an error occurred when formatting an argument | |
match return_future_fmt_error().poll() { | |
Ok(_) => println!("Reality violation, I am in an Ok that should not be possible"), | |
Err(err) => println!("This is my error I expect, inner is: {}", err), | |
}; | |
// This is my error I expect, inner is: Some string | |
match return_future_error_with_map().poll() { | |
Ok(_) => println!("Reality violation, I am in an Ok that should not be possible"), | |
Err(err) => println!("This is my error I expect, inner is: {}", err), | |
}; | |
//Future with the number but not mapped: Ready(42) | |
println!( | |
"Future with the number but not mapped: {:?}", | |
return_i32().poll().expect("A number") | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints out: