Skip to content

Commit

Permalink
WIP Issue libp2p#2831
Browse files Browse the repository at this point in the history
  • Loading branch information
efarg committed Oct 3, 2022
1 parent 1da75b2 commit 8fd23bc
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 108 deletions.
10 changes: 5 additions & 5 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,13 @@ where
}

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

impl<A: ProtocolName, B: ProtocolName> ProtocolName for EitherName<A, B> {
fn protocol_name(&self) -> &[u8] {
impl EitherName {
fn protocol_name(&self) -> &str {
match self {
EitherName::A(a) => a.protocol_name(),
EitherName::B(b) => b.protocol_name(),
Expand Down
43 changes: 29 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,41 @@ 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()
// }
// }

pub struct ProtocolName(Cow<'static, str>);

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

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



/// 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 +174,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 +214,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
6 changes: 3 additions & 3 deletions core/src/upgrade/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
U: InboundUpgrade<Negotiated<C>>,
{
Init {
future: ListenerSelectFuture<C, NameWrap<U::Info>>,
future: ListenerSelectFuture<C, NameWrap<ProtocolName>>,
upgrade: U,
},
Upgrade {
Expand Down Expand Up @@ -251,8 +251,8 @@ type NameWrapIter<I> = iter::Map<I, fn(<I as Iterator>::Item) -> NameWrap<<I as
#[derive(Clone)]
struct NameWrap<N>(N);

impl<N: ProtocolName> AsRef<[u8]> for NameWrap<N> {
impl<N> AsRef<[u8]> for NameWrap<N> {
fn as_ref(&self) -> &[u8] {
self.0.protocol_name()
self.0.protocol_name().as_bytes()
}
}
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, UpgradeInfo, ProtocolName};
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()
}
}
9 changes: 4 additions & 5 deletions core/src/upgrade/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

/// A type to represent two possible upgrade types (inbound or outbound).
Expand All @@ -35,7 +35,6 @@ 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,
Expand All @@ -58,7 +57,7 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: C, info: ProtocolName) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => {
EitherFuture2::A(a.upgrade_inbound(sock, info))
Expand All @@ -80,7 +79,7 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: C, info: EitherName) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => {
EitherFuture2::A(a.upgrade_outbound(sock, info))
Expand All @@ -105,7 +104,7 @@ where
A: Iterator,
B: Iterator,
{
type Item = EitherName<A::Item, B::Item>;
type Item = EitherName;

fn next(&mut self) -> Option<Self::Item> {
match self {
Expand Down
24 changes: 9 additions & 15 deletions core/src/upgrade/from_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ use std::iter;
/// });
/// ```
///
pub fn from_fn<P, F, C, Fut, Out, Err>(protocol_name: P, fun: F) -> FromFnUpgrade<P, F>
pub fn from_fn<F, C, Fut, Out, Err>(protocol_name: ProtocolName, fun: F) -> FromFnUpgrade<F>
where
// Note: these bounds are there in order to help the compiler infer types
P: ProtocolName + Clone,
F: FnOnce(C, Endpoint) -> Fut,
Fut: Future<Output = Result<Out, Err>>,
{
Expand All @@ -66,49 +65,44 @@ where
///
/// The upgrade consists in calling the function passed when creating this struct.
#[derive(Debug, Clone)]
pub struct FromFnUpgrade<P, F> {
protocol_name: P,
pub struct FromFnUpgrade<F> {
protocol_name: ProtocolName,
fun: F,
}

impl<P, F> UpgradeInfo for FromFnUpgrade<P, F>
where
P: ProtocolName + Clone,
impl<F> UpgradeInfo for FromFnUpgrade<F>
{
type Info = P;
type InfoIter = iter::Once<P>;
type InfoIter = iter::Once<ProtocolName>;

fn protocol_info(&self) -> Self::InfoIter {
iter::once(self.protocol_name.clone())
}
}

impl<C, P, F, Fut, Err, Out> InboundUpgrade<C> for FromFnUpgrade<P, F>
impl<C, F, Fut, Err, Out> InboundUpgrade<C> for FromFnUpgrade<F>
where
P: ProtocolName + Clone,
F: FnOnce(C, Endpoint) -> Fut,
Fut: Future<Output = Result<Out, Err>>,
{
type Output = Out;
type Error = Err;
type Future = Fut;

fn upgrade_inbound(self, sock: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: C, _: ProtocolName) -> Self::Future {
(self.fun)(sock, Endpoint::Listener)
}
}

impl<C, P, F, Fut, Err, Out> OutboundUpgrade<C> for FromFnUpgrade<P, F>
impl<C, F, Fut, Err, Out> OutboundUpgrade<C> for FromFnUpgrade<F>
where
P: ProtocolName + Clone,
F: FnOnce(C, Endpoint) -> Fut,
Fut: Future<Output = Result<Out, Err>>,
{
type Output = Out;
type Error = Err;
type Future = Fut;

fn upgrade_outbound(self, sock: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: C, _: ProtocolName) -> Self::Future {
(self.fun)(sock, Endpoint::Dialer)
}
}
22 changes: 9 additions & 13 deletions core/src/upgrade/map.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, UpgradeInfo, ProtocolName};
use futures::prelude::*;
use std::{pin::Pin, task::Context, task::Poll};

Expand All @@ -39,7 +39,6 @@ impl<U, F> UpgradeInfo for MapInboundUpgrade<U, F>
where
U: UpgradeInfo,
{
type Info = U::Info;
type InfoIter = U::InfoIter;

fn protocol_info(&self) -> Self::InfoIter {
Expand All @@ -56,7 +55,7 @@ where
type Error = U::Error;
type Future = MapFuture<U::Future, F>;

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: C, info: ProtocolName) -> Self::Future {
MapFuture {
inner: self.upgrade.upgrade_inbound(sock, info),
map: Some(self.fun),
Expand All @@ -72,7 +71,7 @@ where
type Error = U::Error;
type Future = U::Future;

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: C, info: ProtocolName) -> Self::Future {
self.upgrade.upgrade_outbound(sock, info)
}
}
Expand All @@ -94,7 +93,6 @@ impl<U, F> UpgradeInfo for MapOutboundUpgrade<U, F>
where
U: UpgradeInfo,
{
type Info = U::Info;
type InfoIter = U::InfoIter;

fn protocol_info(&self) -> Self::InfoIter {
Expand All @@ -110,7 +108,7 @@ where
type Error = U::Error;
type Future = U::Future;

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: C, info: ProtocolName) -> Self::Future {
self.upgrade.upgrade_inbound(sock, info)
}
}
Expand All @@ -124,7 +122,7 @@ where
type Error = U::Error;
type Future = MapFuture<U::Future, F>;

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: C, info: ProtocolName) -> Self::Future {
MapFuture {
inner: self.upgrade.upgrade_outbound(sock, info),
map: Some(self.fun),
Expand All @@ -149,7 +147,6 @@ impl<U, F> UpgradeInfo for MapInboundUpgradeErr<U, F>
where
U: UpgradeInfo,
{
type Info = U::Info;
type InfoIter = U::InfoIter;

fn protocol_info(&self) -> Self::InfoIter {
Expand All @@ -166,7 +163,7 @@ where
type Error = T;
type Future = MapErrFuture<U::Future, F>;

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: C, info: ProtocolName) -> Self::Future {
MapErrFuture {
fut: self.upgrade.upgrade_inbound(sock, info),
fun: Some(self.fun),
Expand All @@ -182,7 +179,7 @@ where
type Error = U::Error;
type Future = U::Future;

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: C, info: ProtocolName) -> Self::Future {
self.upgrade.upgrade_outbound(sock, info)
}
}
Expand All @@ -204,7 +201,6 @@ impl<U, F> UpgradeInfo for MapOutboundUpgradeErr<U, F>
where
U: UpgradeInfo,
{
type Info = U::Info;
type InfoIter = U::InfoIter;

fn protocol_info(&self) -> Self::InfoIter {
Expand All @@ -221,7 +217,7 @@ where
type Error = T;
type Future = MapErrFuture<U::Future, F>;

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: C, info: ProtocolName) -> Self::Future {
MapErrFuture {
fut: self.upgrade.upgrade_outbound(sock, info),
fun: Some(self.fun),
Expand All @@ -237,7 +233,7 @@ where
type Error = U::Error;
type Future = U::Future;

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: C, info: ProtocolName) -> Self::Future {
self.upgrade.upgrade_inbound(sock, info)
}
}
Expand Down
Loading

0 comments on commit 8fd23bc

Please sign in to comment.