ns_router/
future.rs

1//! Futures and streams returned from router
2//!
3use std::sync::Arc;
4
5use abstract_ns::{IpList, Address, Error};
6use futures::sync::oneshot;
7use futures::{Future, Async, Stream};
8use void::Void;
9
10use async_slot as slot;
11use config::Config;
12
13/// A future returned from `Router::resolve_host`
14#[derive(Debug)]
15#[must_use = "futures do nothing unless polled"]
16pub struct ResolveHostFuture(
17    pub(crate) oneshot::Receiver<Result<IpList, Error>>);
18
19/// A future returned from `Router::resolve`
20#[derive(Debug)]
21#[must_use = "futures do nothing unless polled"]
22pub struct ResolveFuture(pub(crate) oneshot::Receiver<Result<Address, Error>>);
23
24/// A stream returned from `Router::host_subscribe`
25#[derive(Debug)]
26#[must_use = "streams do nothing unless polled"]
27pub struct HostStream(pub(crate) slot::Receiver<IpList>);
28
29/// A stream returned from `Router::subscribe`
30#[derive(Debug)]
31#[must_use = "streams do nothing unless polled"]
32pub struct AddrStream(pub(crate) slot::Receiver<Address>);
33
34/// A sink that updates router created using `Router::updating_config`
35#[derive(Debug)]
36#[must_use = "sinks do nothing unless polled"]
37pub struct UpdateSink(pub(crate) slot::Sender<Arc<Config>>);
38
39
40impl UpdateSink {
41    /// Update a config
42    ///
43    /// Returns `true` if send worked (meaning router is still alive).
44    pub fn update(&self, config: &Arc<Config>) -> bool {
45        self.0.swap(config.clone()).is_ok()
46    }
47}
48
49impl Future for ResolveHostFuture {
50    type Item = IpList;
51    type Error = Error;
52    #[inline(always)]
53    fn poll(&mut self) -> Result<Async<IpList>, Error> {
54        match self.0.poll().map_err(|e| Error::TemporaryError(e.into()))? {
55            Async::NotReady => Ok(Async::NotReady),
56            Async::Ready(Ok(r))  => Ok(Async::Ready(r)),
57            Async::Ready(Err(e))  => Err(e),
58        }
59    }
60}
61
62impl Future for ResolveFuture {
63    type Item = Address;
64    type Error = Error;
65    #[inline(always)]
66    fn poll(&mut self) -> Result<Async<Address>, Error> {
67        match self.0.poll().map_err(|e| Error::TemporaryError(e.into()))? {
68            Async::NotReady => Ok(Async::NotReady),
69            Async::Ready(Ok(r))  => Ok(Async::Ready(r)),
70            Async::Ready(Err(e))  => Err(e),
71        }
72    }
73}
74
75impl Stream for HostStream {
76    type Item = IpList;
77    type Error = Void;
78    #[inline(always)]
79    fn poll(&mut self) -> Result<Async<Option<IpList>>, Void> {
80        match self.0.poll() {
81            Ok(r) => Ok(r),
82            Err(_) => Ok(Async::Ready(None)),
83        }
84    }
85}
86
87impl Stream for AddrStream {
88    type Item = Address;
89    type Error = Void;
90    #[inline(always)]
91    fn poll(&mut self) -> Result<Async<Option<Address>>, Void> {
92        match self.0.poll() {
93            Ok(r) => Ok(r),
94            Err(_) => Ok(Async::Ready(None)),
95        }
96    }
97}