mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-02 19:21:37 +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:
@ -21,7 +21,7 @@
|
||||
use crate::codec::MAX_FRAME_SIZE;
|
||||
use std::cmp;
|
||||
|
||||
pub(crate) const DEFAULT_MPLEX_PROTOCOL_NAME: &[u8] = b"/mplex/6.7.0";
|
||||
pub(crate) const DEFAULT_MPLEX_PROTOCOL_NAME: &str = "/mplex/6.7.0";
|
||||
|
||||
/// Configuration for the multiplexer.
|
||||
#[derive(Debug, Clone)]
|
||||
@ -36,7 +36,7 @@ pub struct MplexConfig {
|
||||
/// (max 1MByte, as per the Mplex spec).
|
||||
pub(crate) split_send_size: usize,
|
||||
/// Protocol name, defaults to b"/mplex/6.7.0"
|
||||
pub(crate) protocol_name: &'static [u8],
|
||||
pub(crate) protocol_name: &'static str,
|
||||
}
|
||||
|
||||
impl MplexConfig {
|
||||
@ -94,9 +94,9 @@ impl MplexConfig {
|
||||
/// ```rust
|
||||
/// use libp2p_mplex::MplexConfig;
|
||||
/// let mut muxer_config = MplexConfig::new();
|
||||
/// muxer_config.set_protocol_name(b"/mplex/6.7.0");
|
||||
/// muxer_config.set_protocol_name("/mplex/6.7.0");
|
||||
/// ```
|
||||
pub fn set_protocol_name(&mut self, protocol_name: &'static [u8]) -> &mut Self {
|
||||
pub fn set_protocol_name(&mut self, protocol_name: &'static str) -> &mut Self {
|
||||
self.protocol_name = protocol_name;
|
||||
self
|
||||
}
|
||||
|
Reference in New Issue
Block a user