use std::sync::Arc;
use abstract_ns::{IpList, Address, Error};
use futures::sync::oneshot;
use futures::{Future, Async, Stream};
use void::Void;
use async_slot as slot;
use config::Config;
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct ResolveHostFuture(
pub(crate) oneshot::Receiver<Result<IpList, Error>>);
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct ResolveFuture(pub(crate) oneshot::Receiver<Result<Address, Error>>);
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct HostStream(pub(crate) slot::Receiver<IpList>);
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct AddrStream(pub(crate) slot::Receiver<Address>);
#[derive(Debug)]
#[must_use = "sinks do nothing unless polled"]
pub struct UpdateSink(pub(crate) slot::Sender<Arc<Config>>);
impl UpdateSink {
pub fn update(&self, config: &Arc<Config>) -> bool {
self.0.swap(config.clone()).is_ok()
}
}
impl Future for ResolveHostFuture {
type Item = IpList;
type Error = Error;
#[inline(always)]
fn poll(&mut self) -> Result<Async<IpList>, Error> {
match self.0.poll().map_err(|e| Error::TemporaryError(e.into()))? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(Ok(r)) => Ok(Async::Ready(r)),
Async::Ready(Err(e)) => Err(e),
}
}
}
impl Future for ResolveFuture {
type Item = Address;
type Error = Error;
#[inline(always)]
fn poll(&mut self) -> Result<Async<Address>, Error> {
match self.0.poll().map_err(|e| Error::TemporaryError(e.into()))? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(Ok(r)) => Ok(Async::Ready(r)),
Async::Ready(Err(e)) => Err(e),
}
}
}
impl Stream for HostStream {
type Item = IpList;
type Error = Void;
#[inline(always)]
fn poll(&mut self) -> Result<Async<Option<IpList>>, Void> {
match self.0.poll() {
Ok(r) => Ok(r),
Err(_) => Ok(Async::Ready(None)),
}
}
}
impl Stream for AddrStream {
type Item = Address;
type Error = Void;
#[inline(always)]
fn poll(&mut self) -> Result<Async<Option<Address>>, Void> {
match self.0.poll() {
Ok(r) => Ok(r),
Err(_) => Ok(Async::Ready(None)),
}
}
}