future_utils/
infallible.rs

1use std::marker::PhantomData;
2use futures::{Async, Future, Stream};
3use void::{self, Void};
4
5/// Wraps a future or stream which can't fail (ie. has error type `Void`) and casts the error type
6/// to some inferred type.
7pub struct Infallible<F, E> {
8    inner: F,
9    _ph: PhantomData<E>,
10}
11
12impl<F, E> Infallible<F, E> {
13    pub fn new(inner: F) -> Infallible<F, E> {
14        Infallible {
15            inner: inner,
16            _ph: PhantomData,
17        }
18    }
19}
20
21impl<F, E> Future for Infallible<F, E>
22where
23    F: Future<Error=Void>
24{
25    type Item = F::Item;
26    type Error = E;
27
28    fn poll(&mut self) -> Result<Async<F::Item>, E> {
29        self.inner.poll().map_err(|e| void::unreachable(e))
30    }
31}
32
33impl<F, E> Stream for Infallible<F, E>
34where
35    F: Stream<Error=Void>
36{
37    type Item = F::Item;
38    type Error = E;
39
40    fn poll(&mut self) -> Result<Async<Option<F::Item>>, E> {
41        self.inner.poll().map_err(|e| void::unreachable(e))
42    }
43}
44