Remove some boxed futures. (#718)

This commit is contained in:
Toralf Wittner
2018-12-04 11:24:59 +01:00
committed by GitHub
parent a5766fdfac
commit acfa1c9c79
6 changed files with 321 additions and 162 deletions

View File

@ -18,11 +18,19 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use futures::prelude::*;
use futures::{future::Either, prelude::*};
use multiaddr::Multiaddr;
use crate::{
transport::Transport,
upgrade::{OutboundUpgrade, InboundUpgrade, UpgradeInfo, apply_inbound, apply_outbound, UpgradeError}
upgrade::{
OutboundUpgrade,
InboundUpgrade,
apply_inbound,
apply_outbound,
UpgradeError,
OutboundUpgradeApply,
InboundUpgradeApply
}
};
use tokio_io::{AsyncRead, AsyncWrite};
@ -38,53 +46,32 @@ impl<T, U> Upgrade<T, U> {
impl<D, U, O, E> Transport for Upgrade<D, U>
where
D: Transport,
D::Dial: Send + 'static,
D::Listener: Send + 'static,
D::ListenerUpgrade: Send + 'static,
D::Output: AsyncRead + AsyncWrite + Send + 'static,
D::Output: AsyncRead + AsyncWrite,
U: InboundUpgrade<D::Output, Output = O, Error = E>,
U: OutboundUpgrade<D::Output, Output = O, Error = E> + Send + Clone + 'static,
<U as UpgradeInfo>::NamesIter: Send,
<U as UpgradeInfo>::UpgradeId: Send,
<U as InboundUpgrade<D::Output>>::Future: Send,
<U as OutboundUpgrade<D::Output>>::Future: Send,
U: OutboundUpgrade<D::Output, Output = O, Error = E> + Clone,
E: std::error::Error + Send + Sync + 'static
{
type Output = O;
type Listener = Box<Stream<Item = (Self::ListenerUpgrade, Multiaddr), Error = std::io::Error> + Send>;
type ListenerUpgrade = Box<Future<Item = Self::Output, Error = std::io::Error> + Send>;
type Dial = Box<Future<Item = Self::Output, Error = std::io::Error> + Send>;
type Listener = ListenerStream<D::Listener, U>;
type ListenerUpgrade = ListenerUpgradeFuture<D::ListenerUpgrade, U>;
type Dial = DialUpgradeFuture<D::Dial, U>;
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
let upgrade = self.upgrade;
match self.inner.dial(addr.clone()) {
Ok(outbound) => {
let future = outbound
.and_then(move |x| {
apply_outbound(x, upgrade).map_err(UpgradeError::into_io_error)
});
Ok(Box::new(future))
}
Err((dialer, addr)) => Err((Upgrade::new(dialer, upgrade), addr))
Ok(outbound) => Ok(DialUpgradeFuture {
future: outbound,
upgrade: Either::A(Some(self.upgrade))
}),
Err((dialer, addr)) => Err((Upgrade::new(dialer, self.upgrade), addr))
}
}
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
let upgrade = self.upgrade;
match self.inner.listen_on(addr) {
Ok((inbound, addr)) => {
let stream = inbound
.map(move |(future, addr)| {
let upgrade = upgrade.clone();
let future = future
.and_then(move |x| {
apply_inbound(x, upgrade).map_err(UpgradeError::into_io_error)
});
(Box::new(future) as Box<_>, addr)
});
Ok((Box::new(stream), addr))
}
Err((listener, addr)) => Err((Upgrade::new(listener, upgrade), addr)),
Ok((inbound, addr)) =>
Ok((ListenerStream { stream: inbound, upgrade: self.upgrade }, addr)),
Err((listener, addr)) =>
Err((Upgrade::new(listener, self.upgrade), addr))
}
}
@ -92,3 +79,103 @@ where
self.inner.nat_traversal(server, observed)
}
}
pub struct DialUpgradeFuture<T, U>
where
T: Future,
T::Item: AsyncRead + AsyncWrite,
U: OutboundUpgrade<T::Item>
{
future: T,
upgrade: Either<Option<U>, OutboundUpgradeApply<T::Item, U>>
}
impl<T, U> Future for DialUpgradeFuture<T, U>
where
T: Future<Error = std::io::Error>,
T::Item: AsyncRead + AsyncWrite,
U: OutboundUpgrade<T::Item>,
U::Error: std::error::Error + Send + Sync + 'static
{
type Item = U::Output;
type Error = std::io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
let next = match self.upgrade {
Either::A(ref mut up) => {
let x = try_ready!(self.future.poll());
let u = up.take().expect("DialUpgradeFuture is constructed with Either::A(Some).");
Either::B(apply_outbound(x, u))
}
Either::B(ref mut up) => return up.poll().map_err(UpgradeError::into_io_error)
};
self.upgrade = next
}
}
}
pub struct ListenerStream<T, U> {
stream: T,
upgrade: U
}
impl<T, U, F> Stream for ListenerStream<T, U>
where
T: Stream<Item = (F, Multiaddr)>,
F: Future,
F::Item: AsyncRead + AsyncWrite,
U: InboundUpgrade<F::Item> + Clone
{
type Item = (ListenerUpgradeFuture<F, U>, Multiaddr);
type Error = T::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match try_ready!(self.stream.poll()) {
Some((x, a)) => {
let f = ListenerUpgradeFuture {
future: x,
upgrade: Either::A(Some(self.upgrade.clone()))
};
Ok(Async::Ready(Some((f, a))))
}
None => Ok(Async::Ready(None))
}
}
}
pub struct ListenerUpgradeFuture<T, U>
where
T: Future,
T::Item: AsyncRead + AsyncWrite,
U: InboundUpgrade<T::Item>
{
future: T,
upgrade: Either<Option<U>, InboundUpgradeApply<T::Item, U>>
}
impl<T, U> Future for ListenerUpgradeFuture<T, U>
where
T: Future<Error = std::io::Error>,
T::Item: AsyncRead + AsyncWrite,
U: InboundUpgrade<T::Item>,
U::Error: std::error::Error + Send + Sync + 'static
{
type Item = U::Output;
type Error = std::io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
let next = match self.upgrade {
Either::A(ref mut up) => {
let x = try_ready!(self.future.poll());
let u = up.take().expect("ListenerUpgradeFuture is constructed with Either::A(Some).");
Either::B(apply_inbound(x, u))
}
Either::B(ref mut up) => return up.poll().map_err(UpgradeError::into_io_error)
};
self.upgrade = next
}
}
}