Created
February 6, 2021 22:34
-
-
Save tesaguri/33fb7022a6fcd62edb31b44902ca024c to your computer and use it in GitHub Desktop.
An example code for `type_alias_impl_trait` feature
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
#![feature(type_alias_impl_trait)] | |
use std::error; | |
use std::io; | |
use futures_core::TryFuture; | |
use futures_util::future::{MapErr, TryFutureExt}; | |
#[allow(type_alias_bounds)] // The bound seems not to be removable. | |
pub type IoFuture<F: TryFuture> = MapErr<F, impl Fn(<F as TryFuture>::Error) -> io::Error>; | |
pub fn io_future<F>(f: F) -> IoFuture<F> | |
where | |
F: TryFuture, | |
F::Error: Into<Box<dyn error::Error + Send + Sync>>, | |
{ | |
f.map_err(into_io_error) | |
} | |
fn into_io_error<E>(e: E) -> io::Error | |
where | |
E: Into<Box<dyn error::Error + Send + Sync>>, | |
{ | |
io::Error::new(io::ErrorKind::Other, e) | |
} | |
#[cfg(test)] | |
mod tests { | |
use std::future; | |
use std::mem; | |
use super::*; | |
type F = future::Ready<Result<(), &'static str>>; | |
type FnIoFuture<F> = MapErr<F, fn(<F as TryFuture>::Error) -> io::Error>; | |
#[test] | |
fn size() { | |
assert!(mem::size_of::<IoFuture<F>>() < mem::size_of::<FnIoFuture<F>>()); | |
assert_eq!(mem::size_of::<IoFuture<F>>(), mem::size_of::<F>()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment