Skip to content

Commit

Permalink
feat: Add IntoStreamProtocol, replacing StreamProtocolRef (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Jun 18, 2024
1 parent af52b33 commit 3e8ca35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- refactor: Simplify bitswap WantSession. [PR 234](https://github.com/dariusc93/rust-ipfs/pull/234)
- chore: Use default handler in bitswap behaviour. [PR 235](https://github.com/dariusc93/rust-ipfs/pull/235)
- feat: Add AddPeerOpt::{set_keepalive, keepalive, can_keep_alive). [PR XXX](https://github.com/dariusc93/rust-ipfs/pull/XXX)
- feat: Add IntoStreamProtocol, replacing StreamProtocolRef. [PR 244](https://github.com/dariusc93/rust-ipfs/pull/244)

# 0.11.20
- feat: Add Ipfs::{add,remove}_external_address.
Expand All @@ -14,7 +15,7 @@

# 0.11.18
- chore: Use simple_x509 for certificate generation. [PR 219](https://github.com/dariusc93/rust-ipfs/pull/219)
- feat: Use tls along side noise. [PR 212](https://github.com/dariusc93/rust-ipfs/pull/212
- feat: Use tls along side noise. [PR 212](https://github.com/dariusc93/rust-ipfs/pull/212)
- feat: Add webtransport support for wasm target. [PR 220](https://github.com/dariusc93/rust-ipfs/pull/220)

# 0.11.17
Expand Down
46 changes: 15 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,9 +1630,9 @@ impl Ipfs {
#[cfg(feature = "experimental_stream")]
pub async fn new_stream(
&self,
protocol: impl Into<StreamProtocolRef>,
protocol: impl IntoStreamProtocol,
) -> Result<libp2p_stream::IncomingStreams, Error> {
let protocol: StreamProtocol = protocol.into().try_into()?;
let protocol: StreamProtocol = protocol.into_protocol()?;
async move {
let (tx, rx) = oneshot_channel();

Expand All @@ -1651,9 +1651,9 @@ impl Ipfs {
pub async fn open_stream(
&self,
peer_id: PeerId,
protocol: impl Into<StreamProtocolRef>,
protocol: impl IntoStreamProtocol,
) -> Result<libp2p::Stream, Error> {
let protocol: StreamProtocol = protocol.into().try_into()?;
let protocol: StreamProtocol = protocol.into_protocol()?;
async move {
let mut control = self.stream_control().await?;
let stream = control
Expand Down Expand Up @@ -2357,41 +2357,25 @@ impl Ipfs {
}
}

pub enum StreamProtocolRef {
Static(&'static str),
Owned(String),
Direct(StreamProtocol),
pub trait IntoStreamProtocol {
fn into_protocol(self) -> std::io::Result<StreamProtocol>;
}

impl From<&'static str> for StreamProtocolRef {
fn from(protocol: &'static str) -> Self {
StreamProtocolRef::Static(protocol)
}
}

impl From<String> for StreamProtocolRef {
fn from(protocol: String) -> Self {
StreamProtocolRef::Owned(protocol)
impl IntoStreamProtocol for StreamProtocol {
fn into_protocol(self) -> std::io::Result<StreamProtocol> {
Ok(self)
}
}

impl From<StreamProtocol> for StreamProtocolRef {
fn from(protocol: StreamProtocol) -> Self {
StreamProtocolRef::Direct(protocol)
impl IntoStreamProtocol for String {
fn into_protocol(self) -> std::io::Result<StreamProtocol> {
StreamProtocol::try_from_owned(self).map_err(std::io::Error::other)
}
}

impl TryFrom<StreamProtocolRef> for StreamProtocol {
type Error = std::io::Error;
fn try_from(protocl_ref: StreamProtocolRef) -> Result<Self, Self::Error> {
let protocol = match protocl_ref {
StreamProtocolRef::Direct(protocol) => protocol,
StreamProtocolRef::Owned(protocol) => StreamProtocol::try_from_owned(protocol)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
StreamProtocolRef::Static(protocol) => StreamProtocol::new(protocol),
};

Ok(protocol)
impl IntoStreamProtocol for &'static str {
fn into_protocol(self) -> std::io::Result<StreamProtocol> {
Ok(StreamProtocol::new(self))
}
}

Expand Down

0 comments on commit 3e8ca35

Please sign in to comment.