mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 01:31: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:
@ -52,7 +52,7 @@ pub fn dialer_select_proto<R, I>(
|
||||
where
|
||||
R: AsyncRead + AsyncWrite,
|
||||
I: IntoIterator,
|
||||
I::Item: AsRef<[u8]>,
|
||||
I::Item: AsRef<str>,
|
||||
{
|
||||
let protocols = protocols.into_iter().peekable();
|
||||
DialerSelectFuture {
|
||||
@ -88,7 +88,7 @@ where
|
||||
// It also makes the implementation considerably easier to write.
|
||||
R: AsyncRead + AsyncWrite + Unpin,
|
||||
I: Iterator,
|
||||
I::Item: AsRef<[u8]>,
|
||||
I::Item: AsRef<str>,
|
||||
{
|
||||
type Output = Result<(I::Item, Negotiated<R>), NegotiationError>;
|
||||
|
||||
@ -187,7 +187,7 @@ where
|
||||
Message::NotAvailable => {
|
||||
log::debug!(
|
||||
"Dialer: Received rejection of protocol: {}",
|
||||
String::from_utf8_lossy(protocol.as_ref())
|
||||
protocol.as_ref()
|
||||
);
|
||||
let protocol = this.protocols.next().ok_or(NegotiationError::Failed)?;
|
||||
*this.state = State::SendProtocol { io, protocol }
|
||||
|
Reference in New Issue
Block a user