mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-21 22:01: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:
@ -6,7 +6,7 @@ use futures::prelude::*;
|
||||
|
||||
use libp2p::{
|
||||
core::{
|
||||
upgrade::{read_length_prefixed, write_length_prefixed, ProtocolName},
|
||||
upgrade::{read_length_prefixed, write_length_prefixed},
|
||||
Multiaddr,
|
||||
},
|
||||
identity,
|
||||
@ -21,6 +21,7 @@ use libp2p::{
|
||||
};
|
||||
|
||||
use libp2p::core::upgrade::Version;
|
||||
use libp2p::StreamProtocol;
|
||||
use std::collections::{hash_map, HashMap, HashSet};
|
||||
use std::error::Error;
|
||||
use std::iter;
|
||||
@ -61,7 +62,10 @@ pub(crate) async fn new(
|
||||
kademlia: Kademlia::new(peer_id, MemoryStore::new(peer_id)),
|
||||
request_response: request_response::Behaviour::new(
|
||||
FileExchangeCodec(),
|
||||
iter::once((FileExchangeProtocol(), ProtocolSupport::Full)),
|
||||
iter::once((
|
||||
StreamProtocol::new("/file-exchange/1"),
|
||||
ProtocolSupport::Full,
|
||||
)),
|
||||
Default::default(),
|
||||
),
|
||||
},
|
||||
@ -468,31 +472,20 @@ pub(crate) enum Event {
|
||||
|
||||
// Simple file exchange protocol
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct FileExchangeProtocol();
|
||||
#[derive(Clone)]
|
||||
struct FileExchangeCodec();
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct FileRequest(String);
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct FileResponse(Vec<u8>);
|
||||
impl ProtocolName for FileExchangeProtocol {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
"/file-exchange/1".as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl request_response::Codec for FileExchangeCodec {
|
||||
type Protocol = FileExchangeProtocol;
|
||||
type Protocol = StreamProtocol;
|
||||
type Request = FileRequest;
|
||||
type Response = FileResponse;
|
||||
|
||||
async fn read_request<T>(
|
||||
&mut self,
|
||||
_: &FileExchangeProtocol,
|
||||
io: &mut T,
|
||||
) -> io::Result<Self::Request>
|
||||
async fn read_request<T>(&mut self, _: &StreamProtocol, io: &mut T) -> io::Result<Self::Request>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
@ -507,7 +500,7 @@ impl request_response::Codec for FileExchangeCodec {
|
||||
|
||||
async fn read_response<T>(
|
||||
&mut self,
|
||||
_: &FileExchangeProtocol,
|
||||
_: &StreamProtocol,
|
||||
io: &mut T,
|
||||
) -> io::Result<Self::Response>
|
||||
where
|
||||
@ -524,7 +517,7 @@ impl request_response::Codec for FileExchangeCodec {
|
||||
|
||||
async fn write_request<T>(
|
||||
&mut self,
|
||||
_: &FileExchangeProtocol,
|
||||
_: &StreamProtocol,
|
||||
io: &mut T,
|
||||
FileRequest(data): FileRequest,
|
||||
) -> io::Result<()>
|
||||
@ -539,7 +532,7 @@ impl request_response::Codec for FileExchangeCodec {
|
||||
|
||||
async fn write_response<T>(
|
||||
&mut self,
|
||||
_: &FileExchangeProtocol,
|
||||
_: &StreamProtocol,
|
||||
io: &mut T,
|
||||
FileResponse(data): FileResponse,
|
||||
) -> io::Result<()>
|
||||
|
Reference in New Issue
Block a user