mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-03 11:41:34 +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 crate::handler::{
|
||||
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, UpgradeInfoSend};
|
||||
use crate::NegotiatedSubstream;
|
||||
use futures::{future::BoxFuture, prelude::*};
|
||||
use libp2p_core::upgrade::{NegotiationError, ProtocolError, ProtocolName, UpgradeError};
|
||||
use libp2p_core::upgrade::{NegotiationError, ProtocolError, UpgradeError};
|
||||
use libp2p_core::ConnectedPoint;
|
||||
use libp2p_identity::PeerId;
|
||||
use rand::Rng;
|
||||
@ -463,9 +463,9 @@ where
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IndexedProtoName<H>(usize, H);
|
||||
|
||||
impl<H: ProtocolName> ProtocolName for IndexedProtoName<H> {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
self.1.protocol_name()
|
||||
impl<H: AsRef<str>> AsRef<str> for IndexedProtoName<H> {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.1.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
@ -586,7 +586,7 @@ where
|
||||
let mut set = HashSet::new();
|
||||
for infos in iter {
|
||||
for i in infos.protocol_info() {
|
||||
let v = Vec::from(i.protocol_name());
|
||||
let v = Vec::from(i.as_ref());
|
||||
if set.contains(&v) {
|
||||
return Err(DuplicateProtonameError(v));
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user