diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index c377d042106..dd6dbecc26b 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -6,6 +6,8 @@ - Change `StreamMuxer` interface to be entirely poll-based. All functions on `StreamMuxer` now require a `Context` and return `Poll`. This gives callers fine-grained control over what they would like to make progress on. See [PR 2724] and [PR 2797]. +- Remove `EitherName` in favor of `Either` from the `either` crate. See [PR 2798]. +- Replace blanket `ProtocolName` impl with specific ones on `&'static [u8]` and `Cow<'static, [u8]>`. See [PR 2798]. [PR 2724]: https://github.com/libp2p/rust-libp2p/pull/2724 [PR 2762]: https://github.com/libp2p/rust-libp2p/pull/2762 @@ -13,6 +15,7 @@ [PR 2776]: https://github.com/libp2p/rust-libp2p/pull/2776 [PR 2765]: https://github.com/libp2p/rust-libp2p/pull/2765 [PR 2797]: https://github.com/libp2p/rust-libp2p/pull/2797 +[PR 2798]: https://github.com/libp2p/rust-libp2p/pull/2798 # 0.34.0 diff --git a/core/Cargo.toml b/core/Cargo.toml index f0197b675d0..a35a65ee6a8 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous"] asn1_der = "0.7.4" bs58 = "0.4.0" ed25519-dalek = "1.0.1" -either = "1.5" +either = "1.7" fnv = "1.0" futures = { version = "0.3.1", features = ["executor", "thread-pool"] } futures-timer = "3" diff --git a/core/src/either.rs b/core/src/either.rs index a34552bf28f..d031bd2d09c 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -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}, @@ -310,20 +310,6 @@ where } } -#[derive(Debug, Clone)] -pub enum EitherName { - A(A), - B(B), -} - -impl ProtocolName for EitherName { - 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"] diff --git a/core/src/upgrade.rs b/core/src/upgrade.rs index 34a27cdf77a..ac1e6e31c28 100644 --- a/core/src/upgrade.rs +++ b/core/src/upgrade.rs @@ -68,7 +68,9 @@ mod pending; mod select; mod transfer; +use ::either::Either; use futures::future::Future; +use std::borrow::Cow; pub use self::{ apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply}, @@ -126,12 +128,28 @@ pub trait ProtocolName { fn protocol_name(&self) -> &[u8]; } -impl> ProtocolName for T { +impl ProtocolName for &'static [u8] { + fn protocol_name(&self) -> &[u8] { + self + } +} + +impl ProtocolName for Cow<'static, [u8]> { fn protocol_name(&self) -> &[u8] { self.as_ref() } } +impl ProtocolName for Either +where + A: ProtocolName, + B: ProtocolName, +{ + fn protocol_name(&self) -> &[u8] { + ::either::for_both!(self, inner => inner.protocol_name()) + } +} + /// Common trait for upgrades that can be applied on inbound substreams, outbound substreams, /// or both. pub trait UpgradeInfo { diff --git a/core/src/upgrade/either.rs b/core/src/upgrade/either.rs index 8b5c7f71422..5c9c61b8a13 100644 --- a/core/src/upgrade/either.rs +++ b/core/src/upgrade/either.rs @@ -19,9 +19,10 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - either::{EitherError, EitherFuture2, EitherName, EitherOutput}, + either::{EitherError, EitherFuture2, EitherOutput}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, }; +use either::Either; /// A type to represent two possible upgrade types (inbound or outbound). #[derive(Debug, Clone)] @@ -35,7 +36,7 @@ where A: UpgradeInfo, B: UpgradeInfo, { - type Info = EitherName; + type Info = Either; type InfoIter = EitherIter< ::IntoIter, ::IntoIter, @@ -60,10 +61,10 @@ where fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { match (self, info) { - (EitherUpgrade::A(a), EitherName::A(info)) => { + (EitherUpgrade::A(a), Either::Left(info)) => { EitherFuture2::A(a.upgrade_inbound(sock, info)) } - (EitherUpgrade::B(b), EitherName::B(info)) => { + (EitherUpgrade::B(b), Either::Right(info)) => { EitherFuture2::B(b.upgrade_inbound(sock, info)) } _ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"), @@ -82,10 +83,10 @@ where fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { match (self, info) { - (EitherUpgrade::A(a), EitherName::A(info)) => { + (EitherUpgrade::A(a), Either::Left(info)) => { EitherFuture2::A(a.upgrade_outbound(sock, info)) } - (EitherUpgrade::B(b), EitherName::B(info)) => { + (EitherUpgrade::B(b), Either::Right(info)) => { EitherFuture2::B(b.upgrade_outbound(sock, info)) } _ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound"), @@ -105,12 +106,12 @@ where A: Iterator, B: Iterator, { - type Item = EitherName; + type Item = Either; fn next(&mut self) -> Option { match self { - EitherIter::A(a) => a.next().map(EitherName::A), - EitherIter::B(b) => b.next().map(EitherName::B), + EitherIter::A(a) => a.next().map(Either::Left), + EitherIter::B(b) => b.next().map(Either::Right), } } diff --git a/core/src/upgrade/from_fn.rs b/core/src/upgrade/from_fn.rs index 97bbc2eb292..76d1fdc9be9 100644 --- a/core/src/upgrade/from_fn.rs +++ b/core/src/upgrade/from_fn.rs @@ -37,7 +37,7 @@ use std::iter; /// # use futures::AsyncWriteExt; /// let _transport = MemoryTransport::default() /// .and_then(move |out, cp| { -/// upgrade::apply(out, upgrade::from_fn("/foo/1", move |mut sock: Negotiated>>, endpoint| async move { +/// upgrade::apply(out, upgrade::from_fn(b"/foo/1", move |mut sock: Negotiated>>, endpoint| async move { /// if endpoint.is_dialer() { /// upgrade::write_length_prefixed(&mut sock, "some handshake data").await?; /// sock.close().await?; diff --git a/core/src/upgrade/select.rs b/core/src/upgrade/select.rs index d1a8cabca2f..6787da6716b 100644 --- a/core/src/upgrade/select.rs +++ b/core/src/upgrade/select.rs @@ -19,9 +19,10 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - either::{EitherError, EitherFuture2, EitherName, EitherOutput}, + either::{EitherError, EitherFuture2, EitherOutput}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, }; +use either::Either; /// Upgrade that combines two upgrades into one. Supports all the protocols supported by either /// sub-upgrade. @@ -44,7 +45,7 @@ where A: UpgradeInfo, B: UpgradeInfo, { - type Info = EitherName; + type Info = Either; type InfoIter = InfoIterChain< ::IntoIter, ::IntoIter, @@ -69,8 +70,8 @@ where fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { match info { - EitherName::A(info) => EitherFuture2::A(self.0.upgrade_inbound(sock, info)), - EitherName::B(info) => EitherFuture2::B(self.1.upgrade_inbound(sock, info)), + Either::Left(info) => EitherFuture2::A(self.0.upgrade_inbound(sock, info)), + Either::Right(info) => EitherFuture2::B(self.1.upgrade_inbound(sock, info)), } } } @@ -86,8 +87,8 @@ where fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { match info { - EitherName::A(info) => EitherFuture2::A(self.0.upgrade_outbound(sock, info)), - EitherName::B(info) => EitherFuture2::B(self.1.upgrade_outbound(sock, info)), + Either::Left(info) => EitherFuture2::A(self.0.upgrade_outbound(sock, info)), + Either::Right(info) => EitherFuture2::B(self.1.upgrade_outbound(sock, info)), } } } @@ -101,14 +102,14 @@ where A: Iterator, B: Iterator, { - type Item = EitherName; + type Item = Either; fn next(&mut self) -> Option { if let Some(info) = self.0.next() { - return Some(EitherName::A(info)); + return Some(Either::Left(info)); } if let Some(info) = self.1.next() { - return Some(EitherName::B(info)); + return Some(Either::Right(info)); } None } diff --git a/core/tests/transport_upgrade.rs b/core/tests/transport_upgrade.rs index 723a04b0780..4a80d777f54 100644 --- a/core/tests/transport_upgrade.rs +++ b/core/tests/transport_upgrade.rs @@ -32,11 +32,11 @@ use std::{io, pin::Pin}; struct HelloUpgrade {} impl UpgradeInfo for HelloUpgrade { - type Info = &'static str; + type Info = &'static [u8]; type InfoIter = std::iter::Once; fn protocol_info(&self) -> Self::InfoIter { - std::iter::once("/hello/1") + std::iter::once(b"/hello/1") } } diff --git a/src/simple.rs b/src/simple.rs index fb4d3b735d2..338feb69746 100644 --- a/src/simple.rs +++ b/src/simple.rs @@ -19,14 +19,13 @@ // DEALINGS IN THE SOFTWARE. use crate::core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; -use bytes::Bytes; use futures::prelude::*; use std::{iter, sync::Arc}; /// Implementation of `ConnectionUpgrade`. Convenient to use with small protocols. #[derive(Debug)] pub struct SimpleProtocol { - info: Bytes, + info: &'static [u8], // Note: we put the closure `F` in an `Arc` because Rust closures aren't automatically clonable // yet. upgrade: Arc, @@ -34,12 +33,9 @@ pub struct SimpleProtocol { impl SimpleProtocol { /// Builds a `SimpleProtocol`. - pub fn new(info: N, upgrade: F) -> SimpleProtocol - where - N: Into, - { + pub fn new(info: &'static [u8], upgrade: F) -> SimpleProtocol { SimpleProtocol { - info: info.into(), + info, upgrade: Arc::new(upgrade), } } @@ -55,7 +51,7 @@ impl Clone for SimpleProtocol { } impl UpgradeInfo for SimpleProtocol { - type Info = Bytes; + type Info = &'static [u8]; type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { diff --git a/swarm/src/connection/handler_wrapper.rs b/swarm/src/connection/handler_wrapper.rs index 03d09b3fbc1..a12b7fcd6d1 100644 --- a/swarm/src/connection/handler_wrapper.rs +++ b/swarm/src/connection/handler_wrapper.rs @@ -458,7 +458,7 @@ mod tests { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), }, - PendingConnectionHandler::new("test".to_string()), + PendingConnectionHandler::new(b"test"), None, max_negotiating_inbound_streams, ); diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index 04c1696515c..47490d7f019 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -34,11 +34,11 @@ use void::Void; /// Implementation of [`ConnectionHandler`] that returns a pending upgrade. #[derive(Clone, Debug)] pub struct PendingConnectionHandler { - protocol_name: String, + protocol_name: &'static [u8], } impl PendingConnectionHandler { - pub fn new(protocol_name: String) -> Self { + pub fn new(protocol_name: &'static [u8]) -> Self { PendingConnectionHandler { protocol_name } } } @@ -47,13 +47,13 @@ impl ConnectionHandler for PendingConnectionHandler { type InEvent = Void; type OutEvent = Void; type Error = Void; - type InboundProtocol = PendingUpgrade; - type OutboundProtocol = PendingUpgrade; + type InboundProtocol = PendingUpgrade<&'static [u8]>; + type OutboundProtocol = PendingUpgrade<&'static [u8]>; type OutboundOpenInfo = Void; type InboundOpenInfo = (); fn listen_protocol(&self) -> SubstreamProtocol { - SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ()) + SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name), ()) } fn inject_fully_negotiated_inbound(