mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-30 18:21:33 +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:
@ -20,9 +20,10 @@
|
||||
|
||||
use crate::codec;
|
||||
use crate::codec::Message;
|
||||
use libp2p_swarm::StreamProtocol;
|
||||
use void::Void;
|
||||
|
||||
const PROTOCOL_IDENT: &[u8] = b"/rendezvous/1.0.0";
|
||||
const PROTOCOL_IDENT: StreamProtocol = StreamProtocol::new("/rendezvous/1.0.0");
|
||||
|
||||
pub(crate) mod inbound;
|
||||
pub(crate) mod outbound;
|
||||
|
@ -31,7 +31,8 @@ use instant::Instant;
|
||||
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use libp2p_swarm::handler::{ConnectionEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound};
|
||||
use libp2p_swarm::{
|
||||
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, NegotiatedSubstream, SubstreamProtocol,
|
||||
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, NegotiatedSubstream, StreamProtocol,
|
||||
SubstreamProtocol,
|
||||
};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::fmt;
|
||||
@ -127,21 +128,21 @@ impl fmt::Display for OutboundSubstreamId {
|
||||
}
|
||||
|
||||
pub struct PassthroughProtocol {
|
||||
ident: Option<&'static [u8]>,
|
||||
ident: Option<StreamProtocol>,
|
||||
}
|
||||
|
||||
impl PassthroughProtocol {
|
||||
pub fn new(ident: &'static [u8]) -> Self {
|
||||
pub fn new(ident: StreamProtocol) -> Self {
|
||||
Self { ident: Some(ident) }
|
||||
}
|
||||
}
|
||||
|
||||
impl UpgradeInfo for PassthroughProtocol {
|
||||
type Info = &'static [u8];
|
||||
type Info = StreamProtocol;
|
||||
type InfoIter = std::option::IntoIter<Self::Info>;
|
||||
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
self.ident.into_iter()
|
||||
self.ident.clone().into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user