From 5e20702ce6cde1ec31ef7e097d88eb6993d261e3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 3 Jun 2022 17:58:44 +0200 Subject: [PATCH 1/5] core/muxing: Introduce `StreamMuxerEvent::map_stream` This reduces the noise within core/src/either.rs and will reduce the diff in #2648. --- core/src/either.rs | 18 ++++++------------ core/src/muxing.rs | 10 ++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/core/src/either.rs b/core/src/either.rs index e1c5316fd6a..4072be743a9 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -211,20 +211,14 @@ where ) -> Poll, Self::Error>> { match self { EitherOutput::First(inner) => inner.poll_event(cx).map(|result| { - result.map_err(|e| e.into()).map(|event| match event { - StreamMuxerEvent::AddressChange(addr) => StreamMuxerEvent::AddressChange(addr), - StreamMuxerEvent::InboundSubstream(substream) => { - StreamMuxerEvent::InboundSubstream(EitherOutput::First(substream)) - } - }) + result + .map_err(|e| e.into()) + .map(|event| event.map_stream(EitherOutput::First)) }), EitherOutput::Second(inner) => inner.poll_event(cx).map(|result| { - result.map_err(|e| e.into()).map(|event| match event { - StreamMuxerEvent::AddressChange(addr) => StreamMuxerEvent::AddressChange(addr), - StreamMuxerEvent::InboundSubstream(substream) => { - StreamMuxerEvent::InboundSubstream(EitherOutput::Second(substream)) - } - }) + result + .map_err(|e| e.into()) + .map(|event| event.map_stream(EitherOutput::Second)) }), } } diff --git a/core/src/muxing.rs b/core/src/muxing.rs index 170a4537901..0ef2d028891 100644 --- a/core/src/muxing.rs +++ b/core/src/muxing.rs @@ -241,6 +241,16 @@ impl StreamMuxerEvent { None } } + + /// Map the stream within [`StreamMuxerEvent::InboundSubstream`] to a new type. + pub fn map_stream(self, map: impl FnOnce(T) -> O) -> StreamMuxerEvent { + match self { + StreamMuxerEvent::InboundSubstream(stream) => { + StreamMuxerEvent::InboundSubstream(map(stream)) + } + StreamMuxerEvent::AddressChange(addr) => StreamMuxerEvent::AddressChange(addr), + } + } } /// Polls for an event from the muxer and, if an inbound substream, wraps this substream in an From 2405eaa80869beeea8d355bf239e5f4d60c74b8c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 3 Jun 2022 18:03:05 +0200 Subject: [PATCH 2/5] Use `map_err` and `map_ok` functions on `Poll` instead of matching --- core/src/either.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/core/src/either.rs b/core/src/either.rs index 4072be743a9..1e680b35051 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -210,16 +210,14 @@ where cx: &mut Context<'_>, ) -> Poll, Self::Error>> { match self { - EitherOutput::First(inner) => inner.poll_event(cx).map(|result| { - result - .map_err(|e| e.into()) - .map(|event| event.map_stream(EitherOutput::First)) - }), - EitherOutput::Second(inner) => inner.poll_event(cx).map(|result| { - result - .map_err(|e| e.into()) - .map(|event| event.map_stream(EitherOutput::Second)) - }), + EitherOutput::First(inner) => inner + .poll_event(cx) + .map_err(|e| e.into()) + .map_ok(|event| event.map_stream(EitherOutput::First)), + EitherOutput::Second(inner) => inner + .poll_event(cx) + .map_err(|e| e.into()) + .map_ok(|event| event.map_stream(EitherOutput::Second)), } } From ba92d3ccdba03732c4e44104b22c8a9905f827e3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 14 Jun 2022 22:39:06 +0200 Subject: [PATCH 3/5] Add changelog entry and bump version --- core/CHANGELOG.md | 6 ++++++ core/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index bebe025d468..9ea55268951 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.33.1 + +- Introduce `StreamMuxerEvent::map_inbound_stream`. See [PR 2691]. + +[PR 2691]: https://github.com/libp2p/rust-libp2p/pull/2691 + # 0.33.0 - Have methods on `Transport` take `&mut self` instead of `self`. See [PR 2529]. diff --git a/core/Cargo.toml b/core/Cargo.toml index 41e0c2a1763..b6a45248a56 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-core" edition = "2021" rust-version = "1.56.1" description = "Core traits and structs of libp2p" -version = "0.33.0" +version = "0.33.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 0f6ad6f65838b85926f541bc7e0d7858c442a256 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 14 Jun 2022 22:39:17 +0200 Subject: [PATCH 4/5] Incorporate review feedback --- core/src/either.rs | 4 ++-- core/src/muxing.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/either.rs b/core/src/either.rs index 1e680b35051..a9e51a47e79 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -213,11 +213,11 @@ where EitherOutput::First(inner) => inner .poll_event(cx) .map_err(|e| e.into()) - .map_ok(|event| event.map_stream(EitherOutput::First)), + .map_ok(|event| event.map_inbound_stream(EitherOutput::First)), EitherOutput::Second(inner) => inner .poll_event(cx) .map_err(|e| e.into()) - .map_ok(|event| event.map_stream(EitherOutput::Second)), + .map_ok(|event| event.map_inbound_stream(EitherOutput::Second)), } } diff --git a/core/src/muxing.rs b/core/src/muxing.rs index 0ef2d028891..9524acdcce7 100644 --- a/core/src/muxing.rs +++ b/core/src/muxing.rs @@ -243,7 +243,7 @@ impl StreamMuxerEvent { } /// Map the stream within [`StreamMuxerEvent::InboundSubstream`] to a new type. - pub fn map_stream(self, map: impl FnOnce(T) -> O) -> StreamMuxerEvent { + pub fn map_inbound_stream(self, map: impl FnOnce(T) -> O) -> StreamMuxerEvent { match self { StreamMuxerEvent::InboundSubstream(stream) => { StreamMuxerEvent::InboundSubstream(map(stream)) From b943b89a6f252e3ce3bf23604d7f0ce1b16ae599 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 15 Jun 2022 17:21:13 +0200 Subject: [PATCH 5/5] Update core/CHANGELOG.md --- core/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 9ea55268951..cdd252351c5 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.33.1 +# 0.33.1 - unreleased - Introduce `StreamMuxerEvent::map_inbound_stream`. See [PR 2691].