Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 21, 2019 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/9c2bf510fba80e5652788c72ec116d63 to your computer and use it in GitHub Desktop.
Save rust-play/9c2bf510fba80e5652788c72ec116d63 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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