Add upgrade::apply for convenience. (#653)

This commit is contained in:
Toralf Wittner
2018-11-19 15:19:07 +01:00
committed by GitHub
parent 1d1970c772
commit e7bffd009f
2 changed files with 18 additions and 3 deletions

View File

@ -19,12 +19,27 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use bytes::Bytes; use bytes::Bytes;
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError}; use crate::{nodes::ConnectedPoint, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError}};
use futures::prelude::*; use futures::{future::Either, prelude::*};
use multistream_select::{self, DialerSelectFuture, ListenerSelectFuture}; use multistream_select::{self, DialerSelectFuture, ListenerSelectFuture};
use std::mem; use std::mem;
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
/// Applies an upgrade to the inbound and outbound direction of a connection or substream.
pub fn apply<C, U>(conn: C, up: U, cp: ConnectedPoint)
-> Either<InboundUpgradeApply<C, U>, OutboundUpgradeApply<C, U>>
where
C: AsyncRead + AsyncWrite,
U: InboundUpgrade<C> + OutboundUpgrade<C>,
U::NamesIter: Clone
{
if cp.is_listener() {
Either::A(apply_inbound(conn, up))
} else {
Either::B(apply_outbound(conn, up))
}
}
/// Tries to perform an upgrade on an inbound connection or substream. /// Tries to perform an upgrade on an inbound connection or substream.
pub fn apply_inbound<C, U>(conn: C, up: U) -> InboundUpgradeApply<C, U> pub fn apply_inbound<C, U>(conn: C, up: U) -> InboundUpgradeApply<C, U>
where where

View File

@ -68,7 +68,7 @@ use bytes::Bytes;
use futures::future::Future; use futures::future::Future;
pub use self::{ pub use self::{
apply::{apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply}, apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
denied::DeniedUpgrade, denied::DeniedUpgrade,
error::UpgradeError, error::UpgradeError,
map::{MapInboundUpgrade, MapOutboundUpgrade, MapUpgradeErr}, map::{MapInboundUpgrade, MapOutboundUpgrade, MapUpgradeErr},