mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-13 01:51:23 +00:00
feat: replace ProtocolName
with AsRef<str>
Previously, a protocol could be any sequence of bytes as long as it started with `/`. Now, we directly parse a protocol as `String` which enforces it to be valid UTF8. To notify users of this change, we delete the `ProtocolName` trait. The new requirement is that users need to provide a type that implements `AsRef<str>`. We also add a `StreamProtocol` newtype in `libp2p-swarm` which provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with `/`. `StreamProtocol` also implements `AsRef<str>`, meaning users can directly use it in their upgrades. `multistream-select` by itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implement `AsRef<str>`. Resolves: #2831. Pull-Request: #3746.
This commit is contained in:
@ -31,7 +31,7 @@ use libp2p_swarm::{
|
||||
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
|
||||
ListenUpgradeError,
|
||||
},
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, StreamProtocol,
|
||||
SubstreamProtocol,
|
||||
};
|
||||
use void::Void;
|
||||
@ -90,7 +90,7 @@ impl ConnectionHandler for Handler {
|
||||
type OutEvent = Event;
|
||||
type Error = Void;
|
||||
type InboundProtocol = DeniedUpgrade;
|
||||
type OutboundProtocol = ReadyUpgrade<&'static [u8]>;
|
||||
type OutboundProtocol = ReadyUpgrade<StreamProtocol>;
|
||||
type OutboundOpenInfo = ();
|
||||
type InboundOpenInfo = ();
|
||||
|
||||
|
@ -24,8 +24,10 @@
|
||||
|
||||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
||||
|
||||
use libp2p_swarm::StreamProtocol;
|
||||
|
||||
pub mod client;
|
||||
mod protocol;
|
||||
pub mod server;
|
||||
|
||||
pub const PROTOCOL_NAME: &[u8; 11] = b"/perf/1.0.0";
|
||||
pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/perf/1.0.0");
|
||||
|
@ -30,7 +30,7 @@ use libp2p_swarm::{
|
||||
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
|
||||
ListenUpgradeError,
|
||||
},
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, StreamProtocol,
|
||||
SubstreamProtocol,
|
||||
};
|
||||
use log::error;
|
||||
@ -67,7 +67,7 @@ impl ConnectionHandler for Handler {
|
||||
type InEvent = Void;
|
||||
type OutEvent = Event;
|
||||
type Error = Void;
|
||||
type InboundProtocol = ReadyUpgrade<&'static [u8]>;
|
||||
type InboundProtocol = ReadyUpgrade<StreamProtocol>;
|
||||
type OutboundProtocol = DeniedUpgrade;
|
||||
type OutboundOpenInfo = Void;
|
||||
type InboundOpenInfo = ();
|
||||
|
Reference in New Issue
Block a user