core/muxing: Introduce StreamMuxerEvent::map_inbound_stream (#2691)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Thomas Eizinger
2022-06-15 21:33:32 +02:00
committed by GitHub
parent 04f31cd501
commit 3c120ef971
4 changed files with 25 additions and 17 deletions

View File

@ -1,3 +1,9 @@
# 0.33.1 - unreleased
- Introduce `StreamMuxerEvent::map_inbound_stream`. See [PR 2691].
[PR 2691]: https://github.com/libp2p/rust-libp2p/pull/2691
# 0.33.0 # 0.33.0
- Have methods on `Transport` take `&mut self` instead of `self`. See [PR 2529]. - Have methods on `Transport` take `&mut self` instead of `self`. See [PR 2529].

View File

@ -3,7 +3,7 @@ name = "libp2p-core"
edition = "2021" edition = "2021"
rust-version = "1.56.1" rust-version = "1.56.1"
description = "Core traits and structs of libp2p" description = "Core traits and structs of libp2p"
version = "0.33.0" version = "0.33.1"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"

View File

@ -210,22 +210,14 @@ where
cx: &mut Context<'_>, cx: &mut Context<'_>,
) -> Poll<Result<StreamMuxerEvent<Self::Substream>, Self::Error>> { ) -> Poll<Result<StreamMuxerEvent<Self::Substream>, Self::Error>> {
match self { match self {
EitherOutput::First(inner) => inner.poll_event(cx).map(|result| { EitherOutput::First(inner) => inner
result.map_err(|e| e.into()).map(|event| match event { .poll_event(cx)
StreamMuxerEvent::AddressChange(addr) => StreamMuxerEvent::AddressChange(addr), .map_err(|e| e.into())
StreamMuxerEvent::InboundSubstream(substream) => { .map_ok(|event| event.map_inbound_stream(EitherOutput::First)),
StreamMuxerEvent::InboundSubstream(EitherOutput::First(substream)) EitherOutput::Second(inner) => inner
} .poll_event(cx)
}) .map_err(|e| e.into())
}), .map_ok(|event| event.map_inbound_stream(EitherOutput::Second)),
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))
}
})
}),
} }
} }

View File

@ -236,6 +236,16 @@ impl<T> StreamMuxerEvent<T> {
None None
} }
} }
/// Map the stream within [`StreamMuxerEvent::InboundSubstream`] to a new type.
pub fn map_inbound_stream<O>(self, map: impl FnOnce(T) -> O) -> StreamMuxerEvent<O> {
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 /// Polls for an event from the muxer and, if an inbound substream, wraps this substream in an