Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Remove ProtocolName trait in favor of always using strings #2966

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::muxing::StreamMuxerEvent;
use crate::{
muxing::StreamMuxer,
transport::{ListenerId, Transport, TransportError, TransportEvent},
Multiaddr, ProtocolName,
Multiaddr,
};
use futures::{
io::{IoSlice, IoSliceMut},
Expand Down Expand Up @@ -310,20 +310,6 @@ where
}
}

#[derive(Debug, Clone)]
pub enum EitherName<A, B> {
A(A),
B(B),
}

impl<A: ProtocolName, B: ProtocolName> ProtocolName for EitherName<A, B> {
fn protocol_name(&self) -> &[u8] {
match self {
EitherName::A(a) => a.protocol_name(),
EitherName::B(b) => b.protocol_name(),
}
}
}
#[pin_project(project = EitherTransportProj)]
#[derive(Debug)]
#[must_use = "transports do nothing unless polled"]
Expand Down
48 changes: 34 additions & 14 deletions core/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mod select;
mod transfer;

use futures::future::Future;
use std::borrow::Cow;

pub use self::{
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
Expand Down Expand Up @@ -120,27 +121,46 @@ pub use multistream_select::{NegotiatedComplete, NegotiationError, ProtocolError
/// }
/// ```
///
pub trait ProtocolName {
/// The protocol name as bytes. Transmitted on the network.
///
/// **Note:** Valid protocol names must start with `/` and
/// not exceed 140 bytes in length.
fn protocol_name(&self) -> &[u8];
// pub trait ProtocolName {
// /// The protocol name as bytes. Transmitted on the network.
// ///
// /// **Note:** Valid protocol names must start with `/` and
// /// not exceed 140 bytes in length.
// fn protocol_name(&self) -> &[u8];
// }

// impl<T: AsRef<[u8]>> ProtocolName for T {
// fn protocol_name(&self) -> &[u8] {
// self.as_ref()
// }
// }

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProtocolName(Cow<'static, str>);

impl ProtocolName {
fn protocol_name(&self) -> &str {
self.0.as_ref()
}
}

impl AsRef<str> for ProtocolName {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

impl<T: AsRef<[u8]>> ProtocolName for T {
fn protocol_name(&self) -> &[u8] {
self.as_ref()
impl AsRef<[u8]> for ProtocolName {
fn as_ref(&self) -> &[u8] {
self.0.as_ref().as_bytes()
}
}

/// Common trait for upgrades that can be applied on inbound substreams, outbound substreams,
/// or both.
pub trait UpgradeInfo {
/// Opaque type representing a negotiable protocol.
type Info: ProtocolName + Clone;
/// Iterator returned by `protocol_info`.
type InfoIter: IntoIterator<Item = Self::Info>;
type InfoIter: IntoIterator<Item = ProtocolName>;

/// Returns the list of protocols that are supported. Used during the negotiation process.
fn protocol_info(&self) -> Self::InfoIter;
Expand All @@ -159,7 +179,7 @@ pub trait InboundUpgrade<C>: UpgradeInfo {
/// method is called to start the handshake.
///
/// The `info` is the identifier of the protocol, as produced by `protocol_info`.
fn upgrade_inbound(self, socket: C, info: Self::Info) -> Self::Future;
fn upgrade_inbound(self, socket: C, info: ProtocolName) -> Self::Future;
}

/// Extension trait for `InboundUpgrade`. Automatically implemented on all types that implement
Expand Down Expand Up @@ -199,7 +219,7 @@ pub trait OutboundUpgrade<C>: UpgradeInfo {
/// method is called to start the handshake.
///
/// The `info` is the identifier of the protocol, as produced by `protocol_info`.
fn upgrade_outbound(self, socket: C, info: Self::Info) -> Self::Future;
fn upgrade_outbound(self, socket: C, info: ProtocolName) -> Self::Future;
}

/// Extention trait for `OutboundUpgrade`. Automatically implemented on all types that implement
Expand Down
32 changes: 7 additions & 25 deletions core/src/upgrade/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{connection::ConnectedPoint, Negotiated};
use futures::{future::Either, prelude::*};
use log::debug;
use multistream_select::{self, DialerSelectFuture, ListenerSelectFuture};
use std::{iter, mem, pin::Pin, task::Context, task::Poll};
use std::{mem, pin::Pin, task::Context, task::Poll};

pub use multistream_select::Version;

Expand Down Expand Up @@ -53,10 +53,7 @@ where
C: AsyncRead + AsyncWrite + Unpin,
U: InboundUpgrade<Negotiated<C>>,
{
let iter = up
.protocol_info()
.into_iter()
.map(NameWrap as fn(_) -> NameWrap<_>);
let iter = up.protocol_info().into_iter();
let future = multistream_select::listener_select_proto(conn, iter);
InboundUpgradeApply {
inner: InboundUpgradeApplyState::Init {
Expand All @@ -72,10 +69,7 @@ where
C: AsyncRead + AsyncWrite + Unpin,
U: OutboundUpgrade<Negotiated<C>>,
{
let iter = up
.protocol_info()
.into_iter()
.map(NameWrap as fn(_) -> NameWrap<_>);
let iter = up.protocol_info().into_iter();
let future = multistream_select::dialer_select_proto(conn, iter, v);
OutboundUpgradeApply {
inner: OutboundUpgradeApplyState::Init {
Expand All @@ -100,7 +94,7 @@ where
U: InboundUpgrade<Negotiated<C>>,
{
Init {
future: ListenerSelectFuture<C, NameWrap<U::Info>>,
future: ListenerSelectFuture<C, ProtocolName>,
upgrade: U,
},
Upgrade {
Expand Down Expand Up @@ -138,7 +132,7 @@ where
}
};
self.inner = InboundUpgradeApplyState::Upgrade {
future: Box::pin(upgrade.upgrade_inbound(io, info.0)),
future: Box::pin(upgrade.upgrade_inbound(io, info)),
};
}
InboundUpgradeApplyState::Upgrade { mut future } => {
Expand Down Expand Up @@ -180,7 +174,7 @@ where
U: OutboundUpgrade<Negotiated<C>>,
{
Init {
future: DialerSelectFuture<C, NameWrapIter<<U::InfoIter as IntoIterator>::IntoIter>>,
future: DialerSelectFuture<C, <U::InfoIter as IntoIterator>::IntoIter>,
upgrade: U,
},
Upgrade {
Expand Down Expand Up @@ -218,7 +212,7 @@ where
}
};
self.inner = OutboundUpgradeApplyState::Upgrade {
future: Box::pin(upgrade.upgrade_outbound(connection, info.0)),
future: Box::pin(upgrade.upgrade_outbound(connection, info)),
};
}
OutboundUpgradeApplyState::Upgrade { mut future } => {
Expand All @@ -244,15 +238,3 @@ where
}
}
}

type NameWrapIter<I> = iter::Map<I, fn(<I as Iterator>::Item) -> NameWrap<<I as Iterator>::Item>>;

/// Wrapper type to expose an `AsRef<[u8]>` impl for all types implementing `ProtocolName`.
#[derive(Clone)]
struct NameWrap<N>(N);

impl<N: ProtocolName> AsRef<[u8]> for NameWrap<N> {
fn as_ref(&self) -> &[u8] {
self.0.protocol_name()
}
}
9 changes: 4 additions & 5 deletions core/src/upgrade/denied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// 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 crate::upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeInfo};
use futures::future;
use std::iter;
use void::Void;
Expand All @@ -29,8 +29,7 @@ use void::Void;
pub struct DeniedUpgrade;

impl UpgradeInfo for DeniedUpgrade {
type Info = &'static [u8];
type InfoIter = iter::Empty<Self::Info>;
type InfoIter = iter::Empty<ProtocolName>;

fn protocol_info(&self) -> Self::InfoIter {
iter::empty()
Expand All @@ -42,7 +41,7 @@ impl<C> InboundUpgrade<C> for DeniedUpgrade {
type Error = Void;
type Future = future::Pending<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, _: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, _: C, _: ProtocolName) -> Self::Future {
future::pending()
}
}
Expand All @@ -52,7 +51,7 @@ impl<C> OutboundUpgrade<C> for DeniedUpgrade {
type Error = Void;
type Future = future::Pending<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, _: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, _: C, _: ProtocolName) -> Self::Future {
future::pending()
}
}
69 changes: 14 additions & 55 deletions core/src/upgrade/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
// DEALINGS IN THE SOFTWARE.

use crate::{
either::{EitherError, EitherFuture2, EitherName, EitherOutput},
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo},
either::{EitherError, EitherFuture2, EitherOutput},
upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeInfo},
};
use either::Either;

/// A type to represent two possible upgrade types (inbound or outbound).
#[derive(Debug, Clone)]
Expand All @@ -35,16 +36,13 @@ where
A: UpgradeInfo,
B: UpgradeInfo,
{
type Info = EitherName<A::Info, B::Info>;
type InfoIter = EitherIter<
<A::InfoIter as IntoIterator>::IntoIter,
<B::InfoIter as IntoIterator>::IntoIter,
>;
type InfoIter =
Either<<A::InfoIter as IntoIterator>::IntoIter, <B::InfoIter as IntoIterator>::IntoIter>;

fn protocol_info(&self) -> Self::InfoIter {
match self {
EitherUpgrade::A(a) => EitherIter::A(a.protocol_info().into_iter()),
EitherUpgrade::B(b) => EitherIter::B(b.protocol_info().into_iter()),
EitherUpgrade::A(a) => Either::Left(a.protocol_info().into_iter()),
EitherUpgrade::B(b) => Either::Right(b.protocol_info().into_iter()),
}
}
}
Expand All @@ -58,15 +56,10 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => {
EitherFuture2::A(a.upgrade_inbound(sock, info))
}
(EitherUpgrade::B(b), EitherName::B(info)) => {
EitherFuture2::B(b.upgrade_inbound(sock, info))
}
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"),
fn upgrade_inbound(self, sock: C, info: ProtocolName) -> Self::Future {
match self {
EitherUpgrade::A(a) => EitherFuture2::A(a.upgrade_inbound(sock, info)),
EitherUpgrade::B(b) => EitherFuture2::B(b.upgrade_inbound(sock, info)),
}
}
}
Expand All @@ -80,44 +73,10 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => {
EitherFuture2::A(a.upgrade_outbound(sock, info))
}
(EitherUpgrade::B(b), EitherName::B(info)) => {
EitherFuture2::B(b.upgrade_outbound(sock, info))
}
_ => 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> Iterator for EitherIter<A, B>
where
A: Iterator,
B: Iterator,
{
type Item = EitherName<A::Item, B::Item>;

fn next(&mut self) -> Option<Self::Item> {
match self {
EitherIter::A(a) => a.next().map(EitherName::A),
EitherIter::B(b) => b.next().map(EitherName::B),
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
fn upgrade_outbound(self, sock: C, info: ProtocolName) -> Self::Future {
match self {
EitherIter::A(a) => a.size_hint(),
EitherIter::B(b) => b.size_hint(),
EitherUpgrade::A(a) => EitherFuture2::A(a.upgrade_outbound(sock, info)),
EitherUpgrade::B(b) => EitherFuture2::B(b.upgrade_outbound(sock, info)),
}
}
}
Loading