Refine boxing during transport construction. #1794
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After #1783 I had to realise while experimenting with the latest
rust-libp2p
in substrate that there is still a use-case for boxing non-multiplexed transports, mainly the odd case where a transport is used without aNetwork
. Instead of just re-exposing thetransport::upgrade::boxed::boxed()
constructor directly for these cases, I decided to try some refinements of the API.In summary:
Authenticated
andMultiplexed
newtypes in the transport upgrade builder pipeline for a more type-directed API.Transport::boxed() -> Boxed<Self::Output>
and the newMultiplexed::boxed() -> Boxed<(TConnInfo, StreamMuxerBox)>
.Transport::timeout()
moved toMultiplexed::timeout()
to better reflect the common use-case and avoid the pitfall where the timeout is applied too early in the transport builder pipeline. It also makes theTransport
trait a bit leaner.Swarm
now expects aBoxed<(PeerId, StreamMuxerBox)>
transport. This avoids repeatingSend
/Sync
/'static
constraints required for boxing the transport in the Swarm API (which always boxes the transport) and also avoids common accidental use of double-boxed transports if the transport passed to theSwarm
is already boxed (which also happens right now in substrate).build_*
functions ofsrc/lib.rs
now return atransport::Boxed<(PeerId, StreamMuxerBox)>
. This is just a change of return type fromimpl Transport
toBoxed
, the implementation already boxed anyway. This obsoletes The return value oflibp2p::build_*
cannot be.boxed()
#783.Some more details:
Transport::boxed()
no longer requires a multiplexed transport, as was the case before [multistream-select] Fix panic withV1Lazy
(regression) and more convenient transport boxing. #1783. I.e.Transport::boxed()
can be used with any transport and yields atransport::boxed::Boxed
for some opaque outputTransport::Output
.Transport::boxed()
now always also boxes the transport errors, leaving only the opaqueTransport::Output
unboxed. Every use-case I've seen forTransport::boxed()
also wanted boxed transport errors.Transport::upgrade()
yields atransport::upgrade::Builder
as usual.transport::upgrade::Builder::authenticate()
yields atransport::upgrade::Authenticated
(newtype).transport::upgrade::Authenticated::apply()
yields atransport::upgrade::Authenticated
(previously this wasBuilder::apply() -> Builder
).transport::upgrade::Authenticated::multiplex()
yields atransport::upgrade::Multiplexed
transport (newtype).transport::upgrade::Multiplexed::timeout()
yields atransport::upgrade::Multiplexed<TransportTimeout<T>>
.transport::upgrade::Multiplexed::boxed()
yields atransport::boxed::Boxed
whose output muxer is aStreamMuxerBox
.