Created
September 21, 2019 11:30
-
-
Save rust-play/9c2bf510fba80e5652788c72ec116d63 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
use futures; | |
use std::char; | |
use std::num; | |
use futures::{Future, future}; | |
#[derive(PartialEq, Debug)] | |
pub enum MyError { | |
Utf16Error(char::DecodeUtf16Error), | |
ParseError(num::ParseIntError) | |
} | |
impl From<char::DecodeUtf16Error> for MyError { | |
fn from(e: char::DecodeUtf16Error) -> Self { | |
MyError::Utf16Error(e) | |
} | |
} | |
impl From<num::ParseIntError> for MyError { | |
fn from(e: num::ParseIntError) -> Self { | |
MyError::ParseError(e) | |
} | |
} | |
#[test] | |
fn main() { | |
// We define our base futures | |
let future1 = future::ok::<(), char::DecodeUtf16Error>(()); | |
let future2 = future::ok::<u8, num::ParseIntError>(3); | |
// We then cast them | |
let future1_casted = future1.from_err::<MyError>(); | |
let future2_casted = future2.from_err::<MyError>(); | |
let combined = future2_casted.join(future1_casted); | |
assert_eq!(combined.wait(), Ok((3, ()))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment