mapinto/
lib.rs

1use futures::{Future, Map, MapErr, AndThen, IntoFuture};
2
3pub trait ResultMapInto<U> {
4    type Output;
5    fn map_into(self) -> Self::Output;
6}
7
8pub trait ResultMapErrInto<U> {
9    type Output;
10    fn map_err_into(self) -> Self::Output;
11}
12
13impl<T, E, U: From<T>> ResultMapInto<U> for Result<T, E> {
14    type Output = Result<U, E>;
15    fn map_into(self) -> Self::Output {
16        self.map(Into::into)
17    }
18}
19
20impl<T, E, U: From<E>> ResultMapErrInto<U> for Result<T, E> {
21    type Output = Result<T, U>;
22    fn map_err_into(self) -> Self::Output {
23        self.map_err(Into::into)
24    }
25}
26
27pub trait FutureMapInto<U> {
28    type Output: Future<Item=U>;
29    fn map_into(self) -> Self::Output;
30}
31
32pub trait FutureMapErrInto<U> {
33    type Output: Future<Error=U>;
34    fn map_err_into(self) -> Self::Output;
35}
36
37impl<F, U> FutureMapInto<U> for F
38    where F: Future,
39          U: From<F::Item>
40{
41    type Output = Map<F, fn(F::Item) -> U>;
42    fn map_into(self) -> Self::Output {
43        self.map(Into::into)
44    }
45}
46
47impl<F, U> FutureMapErrInto<U> for F
48    where F: Future,
49          U: From<F::Error>
50{
51    type Output = MapErr<F, fn(F::Error) -> U>;
52    fn map_err_into(self) -> Self::Output {
53        self.map_err(Into::into)
54    }
55}
56
57pub trait FutureFlatMapErrInto {
58    type Output;
59    fn flat_map_err_into(self) -> Self::Output;
60}
61
62impl<F, E> FutureFlatMapErrInto for F
63    where F: Future,
64          F::Item: IntoFuture<Error=E>,
65          F::Error: From<E>
66{
67    type Output = AndThen<
68        F,
69        <<F::Item as IntoFuture>::Future as FutureMapErrInto<F::Error>>::Output,
70        fn(F::Item) -> <<F::Item as IntoFuture>::Future as FutureMapErrInto<F::Error>>::Output
71    >;
72    fn flat_map_err_into(self) -> Self::Output {
73        self.and_then(|f| f.into_future().map_err_into())
74    }
75}