mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-27 16:51:34 +00:00
Add EitherUpgrade
and generalise OrUpgrade
. (#662)
This commit is contained in:
109
core/src/upgrade/either.rs
Normal file
109
core/src/upgrade/either.rs
Normal file
@ -0,0 +1,109 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::future::Either;
|
||||
use crate::{
|
||||
either::{EitherOutput, EitherError, EitherFuture2},
|
||||
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}
|
||||
};
|
||||
|
||||
/// A type to represent two possible upgrade types (inbound or outbound).
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum EitherUpgrade<A, B> { A(A), B(B) }
|
||||
|
||||
impl<A, B> UpgradeInfo for EitherUpgrade<A, B>
|
||||
where
|
||||
A: UpgradeInfo,
|
||||
B: UpgradeInfo
|
||||
{
|
||||
type UpgradeId = Either<A::UpgradeId, B::UpgradeId>;
|
||||
type NamesIter = EitherIter<A::NamesIter, B::NamesIter>;
|
||||
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
match self {
|
||||
EitherUpgrade::A(a) => EitherIter::A(a.protocol_names()),
|
||||
EitherUpgrade::B(b) => EitherIter::B(b.protocol_names())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for EitherUpgrade<A, B>
|
||||
where
|
||||
A: InboundUpgrade<C, Output = TA, Error = EA>,
|
||||
B: InboundUpgrade<C, Output = TB, Error = EB>,
|
||||
{
|
||||
type Output = EitherOutput<TA, TB>;
|
||||
type Error = EitherError<EA, EB>;
|
||||
type Future = EitherFuture2<A::Future, B::Future>;
|
||||
|
||||
fn upgrade_inbound(self, sock: C, id: Self::UpgradeId) -> Self::Future {
|
||||
match (self, id) {
|
||||
(EitherUpgrade::A(a), Either::A(id)) => EitherFuture2::A(a.upgrade_inbound(sock, id)),
|
||||
(EitherUpgrade::B(b), Either::B(id)) => EitherFuture2::B(b.upgrade_inbound(sock, id)),
|
||||
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for EitherUpgrade<A, B>
|
||||
where
|
||||
A: OutboundUpgrade<C, Output = TA, Error = EA>,
|
||||
B: OutboundUpgrade<C, Output = TB, Error = EB>,
|
||||
{
|
||||
type Output = EitherOutput<TA, TB>;
|
||||
type Error = EitherError<EA, EB>;
|
||||
type Future = EitherFuture2<A::Future, B::Future>;
|
||||
|
||||
fn upgrade_outbound(self, sock: C, id: Self::UpgradeId) -> Self::Future {
|
||||
match (self, id) {
|
||||
(EitherUpgrade::A(a), Either::A(id)) => EitherFuture2::A(a.upgrade_outbound(sock, id)),
|
||||
(EitherUpgrade::B(b), Either::B(id)) => EitherFuture2::B(b.upgrade_outbound(sock, id)),
|
||||
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A type to represent two possible `Iterator` types.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum EitherIter<A, B> { A(A), B(B) }
|
||||
|
||||
impl<A, B, AId, BId> Iterator for EitherIter<A, B>
|
||||
where
|
||||
A: Iterator<Item = (Bytes, AId)>,
|
||||
B: Iterator<Item = (Bytes, BId)>,
|
||||
{
|
||||
type Item = (Bytes, Either<AId, BId>);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self {
|
||||
EitherIter::A(a) => a.next().map(|(name, id)| (name, Either::A(id))),
|
||||
EitherIter::B(b) => b.next().map(|(name, id)| (name, Either::B(id)))
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
match self {
|
||||
EitherIter::A(a) => a.size_hint(),
|
||||
EitherIter::B(b) => b.size_hint()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,10 +59,10 @@
|
||||
|
||||
mod apply;
|
||||
mod denied;
|
||||
mod either;
|
||||
mod error;
|
||||
mod map;
|
||||
mod or;
|
||||
mod toggleable;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::future::Future;
|
||||
@ -70,10 +70,10 @@ use futures::future::Future;
|
||||
pub use self::{
|
||||
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
||||
denied::DeniedUpgrade,
|
||||
either::EitherUpgrade,
|
||||
error::UpgradeError,
|
||||
map::{MapInboundUpgrade, MapOutboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgradeErr},
|
||||
or::OrUpgrade,
|
||||
toggleable::{toggleable, Toggleable}
|
||||
};
|
||||
|
||||
/// Common trait for upgrades that can be applied on inbound substreams, outbound substreams,
|
||||
@ -132,7 +132,7 @@ pub trait InboundUpgradeExt<C>: InboundUpgrade<C> {
|
||||
fn or_inbound<U>(self, upgrade: U) -> OrUpgrade<Self, U>
|
||||
where
|
||||
Self: Sized,
|
||||
U: InboundUpgrade<C, Output = Self::Output, Error = Self::Error>
|
||||
U: InboundUpgrade<C>
|
||||
{
|
||||
OrUpgrade::new(self, upgrade)
|
||||
}
|
||||
@ -182,7 +182,7 @@ pub trait OutboundUpgradeExt<C>: OutboundUpgrade<C> {
|
||||
fn or_outbound<U>(self, upgrade: U) -> OrUpgrade<Self, U>
|
||||
where
|
||||
Self: Sized,
|
||||
U: OutboundUpgrade<C, Output = Self::Output, Error = Self::Error>
|
||||
U: OutboundUpgrade<C>
|
||||
{
|
||||
OrUpgrade::new(self, upgrade)
|
||||
}
|
||||
|
@ -20,7 +20,10 @@
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::future::Either;
|
||||
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use crate::{
|
||||
either::{EitherOutput, EitherError, EitherFuture2},
|
||||
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}
|
||||
};
|
||||
|
||||
/// Upgrade that combines two upgrades into one. Supports all the protocols supported by either
|
||||
/// sub-upgrade.
|
||||
@ -51,36 +54,36 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, A, B, T, E> InboundUpgrade<C> for OrUpgrade<A, B>
|
||||
impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for OrUpgrade<A, B>
|
||||
where
|
||||
A: InboundUpgrade<C, Output = T, Error = E>,
|
||||
B: InboundUpgrade<C, Output = T, Error = E>,
|
||||
A: InboundUpgrade<C, Output = TA, Error = EA>,
|
||||
B: InboundUpgrade<C, Output = TB, Error = EB>,
|
||||
{
|
||||
type Output = T; // TODO: different output types
|
||||
type Error = E; // TODO: different error types
|
||||
type Future = Either<A::Future, B::Future>;
|
||||
type Output = EitherOutput<TA, TB>;
|
||||
type Error = EitherError<EA, EB>;
|
||||
type Future = EitherFuture2<A::Future, B::Future>;
|
||||
|
||||
fn upgrade_inbound(self, sock: C, id: Self::UpgradeId) -> Self::Future {
|
||||
match id {
|
||||
Either::A(id) => Either::A(self.0.upgrade_inbound(sock, id)),
|
||||
Either::B(id) => Either::B(self.1.upgrade_inbound(sock, id))
|
||||
Either::A(id) => EitherFuture2::A(self.0.upgrade_inbound(sock, id)),
|
||||
Either::B(id) => EitherFuture2::B(self.1.upgrade_inbound(sock, id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, A, B, T, E> OutboundUpgrade<C> for OrUpgrade<A, B>
|
||||
impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for OrUpgrade<A, B>
|
||||
where
|
||||
A: OutboundUpgrade<C, Output = T, Error = E>,
|
||||
B: OutboundUpgrade<C, Output = T, Error = E>,
|
||||
A: OutboundUpgrade<C, Output = TA, Error = EA>,
|
||||
B: OutboundUpgrade<C, Output = TB, Error = EB>,
|
||||
{
|
||||
type Output = T; // TODO: different output types
|
||||
type Error = E; // TODO: different error types
|
||||
type Future = Either<A::Future, B::Future>;
|
||||
type Output = EitherOutput<TA, TB>;
|
||||
type Error = EitherError<EA, EB>;
|
||||
type Future = EitherFuture2<A::Future, B::Future>;
|
||||
|
||||
fn upgrade_outbound(self, sock: C, id: Self::UpgradeId) -> Self::Future {
|
||||
match id {
|
||||
Either::A(id) => Either::A(self.0.upgrade_outbound(sock, id)),
|
||||
Either::B(id) => Either::B(self.1.upgrade_outbound(sock, id))
|
||||
Either::A(id) => EitherFuture2::A(self.0.upgrade_outbound(sock, id)),
|
||||
Either::B(id) => EitherFuture2::B(self.1.upgrade_outbound(sock, id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,153 +0,0 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use futures::future;
|
||||
|
||||
/// Wraps around a `InboundUpgrade` or `OutboundUpgrade` and makes it possible
|
||||
/// to enable or disable the upgrade.
|
||||
#[inline]
|
||||
pub fn toggleable<U>(upgrade: U) -> Toggleable<U> {
|
||||
Toggleable {
|
||||
inner: upgrade,
|
||||
enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// See `toggleable`.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Toggleable<U> {
|
||||
inner: U,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl<U> Toggleable<U> {
|
||||
/// Toggles the upgrade.
|
||||
#[inline]
|
||||
pub fn toggle(&mut self) {
|
||||
self.enabled = !self.enabled;
|
||||
}
|
||||
|
||||
/// Returns true if the upgrade is enabled.
|
||||
#[inline]
|
||||
pub fn enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
|
||||
/// Enables the upgrade.
|
||||
#[inline]
|
||||
pub fn enable(&mut self) {
|
||||
self.enabled = true;
|
||||
}
|
||||
|
||||
/// Disables the upgrade.
|
||||
#[inline]
|
||||
pub fn disable(&mut self) {
|
||||
self.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl<U> UpgradeInfo for Toggleable<U>
|
||||
where
|
||||
U: UpgradeInfo
|
||||
{
|
||||
type UpgradeId = U::UpgradeId;
|
||||
type NamesIter = ToggleableIter<U::NamesIter>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
ToggleableIter {
|
||||
inner: self.inner.protocol_names(),
|
||||
enabled: self.enabled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, U> InboundUpgrade<C> for Toggleable<U>
|
||||
where
|
||||
U: InboundUpgrade<C>
|
||||
{
|
||||
type Output = U::Output;
|
||||
type Error = U::Error;
|
||||
type Future = future::Either<future::Empty<Self::Output, Self::Error>, U::Future>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_inbound(self, socket: C, id: Self::UpgradeId) -> Self::Future {
|
||||
if self.enabled {
|
||||
future::Either::B(self.inner.upgrade_inbound(socket, id))
|
||||
} else {
|
||||
future::Either::A(future::empty())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, U> OutboundUpgrade<C> for Toggleable<U>
|
||||
where
|
||||
U: OutboundUpgrade<C>
|
||||
{
|
||||
type Output = U::Output;
|
||||
type Error = U::Error;
|
||||
type Future = future::Either<future::Empty<Self::Output, Self::Error>, U::Future>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: C, id: Self::UpgradeId) -> Self::Future {
|
||||
if self.enabled {
|
||||
future::Either::B(self.inner.upgrade_outbound(socket, id))
|
||||
} else {
|
||||
future::Either::A(future::empty())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator that is toggleable.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ToggleableIter<I> {
|
||||
inner: I,
|
||||
// It is expected that `enabled` doesn't change once the iterator has been created.
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl<I> Iterator for ToggleableIter<I>
|
||||
where I: Iterator
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.enabled {
|
||||
self.inner.next()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
if self.enabled {
|
||||
self.inner.size_hint()
|
||||
} else {
|
||||
(0, Some(0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for ToggleableIter<I>
|
||||
where
|
||||
I: ExactSizeIterator
|
||||
{}
|
||||
|
Reference in New Issue
Block a user