2020
2121//! Muxing is the process of splitting a connection into multiple substreams.
2222//!
23- //! The main item of this module is the `StreamMuxer` trait. An implementation of `StreamMuxer`
24- //! has ownership of a connection, lets you open and close substreams.
23+ //! The main item of this module is the `StreamMuxer` trait. An implementation
24+ //! of `StreamMuxer` has ownership of a connection, lets you open and close
25+ //! substreams.
2526//!
26- //! > **Note**: You normally don't need to use the methods of the `StreamMuxer` directly, as this
27- //! > is managed by the library's internals.
27+ //! > **Note**: You normally don't need to use the methods of the `StreamMuxer`
28+ //! > directly, as this
29+ //! > is managed by the library's internals.
2830//!
29- //! Each substream of a connection is an isolated stream of data. All the substreams are muxed
30- //! together so that the data read from or written to each substream doesn't influence the other
31- //! substreams.
31+ //! Each substream of a connection is an isolated stream of data. All the
32+ //! substreams are muxed together so that the data read from or written to each
33+ //! substream doesn't influence the other substreams.
3234//!
33- //! In the context of libp2p, each substream can use a different protocol. Contrary to opening a
34- //! connection, opening a substream is almost free in terms of resources. This means that you
35- //! shouldn't hesitate to rapidly open and close substreams, and to design protocols that don't
36- //! require maintaining long-lived channels of communication.
35+ //! In the context of libp2p, each substream can use a different protocol.
36+ //! Contrary to opening a connection, opening a substream is almost free in
37+ //! terms of resources. This means that you shouldn't hesitate to rapidly open
38+ //! and close substreams, and to design protocols that don't require maintaining
39+ //! long-lived channels of communication.
3740//!
38- //! > **Example**: The Kademlia protocol opens a new substream for each request it wants to
39- //! > perform. Multiple requests can be performed simultaneously by opening multiple
40- //! > substreams, without having to worry about associating responses with the
41- //! > right request.
41+ //! > **Example**: The Kademlia protocol opens a new substream for each request
42+ //! > it wants to
43+ //! > perform. Multiple requests can be performed simultaneously by opening
44+ //! > multiple
45+ //! > substreams, without having to worry about associating responses with the
46+ //! > right request.
4247//!
4348//! # Implementing a muxing protocol
4449//!
45- //! In order to implement a muxing protocol, create an object that implements the `UpgradeInfo`,
46- //! `InboundUpgrade` and `OutboundUpgrade` traits. See the `upgrade` module for more information.
47- //! The `Output` associated type of the `InboundUpgrade` and `OutboundUpgrade` traits should be
48- //! identical, and should be an object that implements the `StreamMuxer` trait.
50+ //! In order to implement a muxing protocol, create an object that implements
51+ //! the `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` traits. See the
52+ //! `upgrade` module for more information. The `Output` associated type of the
53+ //! `InboundUpgrade` and `OutboundUpgrade` traits should be identical, and
54+ //! should be an object that implements the `StreamMuxer` trait.
4955//!
50- //! The upgrade process will take ownership of the connection, which makes it possible for the
51- //! implementation of `StreamMuxer` to control everything that happens on the wire.
56+ //! The upgrade process will take ownership of the connection, which makes it
57+ //! possible for the implementation of `StreamMuxer` to control everything that
58+ //! happens on the wire.
59+
60+ use std:: { future:: Future , pin:: Pin } ;
5261
53- use futures:: { task:: Context , task:: Poll , AsyncRead , AsyncWrite } ;
62+ use futures:: {
63+ task:: { Context , Poll } ,
64+ AsyncRead , AsyncWrite ,
65+ } ;
5466use multiaddr:: Multiaddr ;
55- use std:: future:: Future ;
56- use std:: pin:: Pin ;
5767
58- pub use self :: boxed:: StreamMuxerBox ;
59- pub use self :: boxed:: SubstreamBox ;
68+ pub use self :: boxed:: { StreamMuxerBox , SubstreamBox } ;
6069
6170mod boxed;
6271
6372/// Provides multiplexing for a connection by allowing users to open substreams.
6473///
65- /// A substream created by a [`StreamMuxer`] is a type that implements [`AsyncRead`] and [`AsyncWrite`].
66- /// The [`StreamMuxer`] itself is modelled closely after [`AsyncWrite`]. It features `poll`-style
67- /// functions that allow the implementation to make progress on various tasks.
74+ /// A substream created by a [`StreamMuxer`] is a type that implements
75+ /// [`AsyncRead`] and [`AsyncWrite`]. The [`StreamMuxer`] itself is modelled
76+ /// closely after [`AsyncWrite`]. It features `poll`-style functions that allow
77+ /// the implementation to make progress on various tasks.
6878pub trait StreamMuxer {
69- /// Type of the object that represents the raw substream where data can be read and written.
79+ /// Type of the object that represents the raw substream where data can be
80+ /// read and written.
7081 type Substream : AsyncRead + AsyncWrite ;
7182
7283 /// Error type of the muxer
7384 type Error : std:: error:: Error ;
7485
7586 /// Poll for new inbound substreams.
7687 ///
77- /// This function should be called whenever callers are ready to accept more inbound streams. In
78- /// other words, callers may exercise back-pressure on incoming streams by not calling this
79- /// function if a certain limit is hit.
88+ /// This function should be called whenever callers are ready to accept more
89+ /// inbound streams. In other words, callers may exercise back-pressure
90+ /// on incoming streams by not calling this function
91+ /// if a certain limit is hit.
8092 fn poll_inbound (
8193 self : Pin < & mut Self > ,
8294 cx : & mut Context < ' _ > ,
@@ -90,20 +102,23 @@ pub trait StreamMuxer {
90102
91103 /// Poll to close this [`StreamMuxer`].
92104 ///
93- /// After this has returned `Poll::Ready(Ok(()))`, the muxer has become useless and may be safely
94- /// dropped.
105+ /// After this has returned `Poll::Ready(Ok(()))`, the muxer has become
106+ /// useless and may be safely dropped.
95107 ///
96- /// > **Note**: You are encouraged to call this method and wait for it to return `Ready`, so
97- /// > that the remote is properly informed of the shutdown. However, apart from
98- /// > properly informing the remote, there is no difference between this and
99- /// > immediately dropping the muxer.
108+ /// > **Note**: You are encouraged to call this method and wait for it to
109+ /// > return `Ready`, so
110+ /// > that the remote is properly informed of the shutdown. However, apart
111+ /// > from
112+ /// > properly informing the remote, there is no difference between this and
113+ /// > immediately dropping the muxer.
100114 fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > ;
101115
102116 /// Poll to allow the underlying connection to make progress.
103117 ///
104- /// In contrast to all other `poll`-functions on [`StreamMuxer`], this function MUST be called
105- /// unconditionally. Because it will be called regardless, this function can be used by
106- /// implementations to return events about the underlying connection that the caller MUST deal
118+ /// In contrast to all other `poll`-functions on [`StreamMuxer`], this
119+ /// function MUST be called unconditionally. Because it will be called
120+ /// regardless, this function can be used by implementations to return
121+ /// events about the underlying connection that the caller MUST deal
107122 /// with.
108123 fn poll (
109124 self : Pin < & mut Self > ,
@@ -120,7 +135,8 @@ pub enum StreamMuxerEvent {
120135
121136/// Extension trait for [`StreamMuxer`].
122137pub trait StreamMuxerExt : StreamMuxer + Sized {
123- /// Convenience function for calling [`StreamMuxer::poll_inbound`] for [`StreamMuxer`]s that are `Unpin`.
138+ /// Convenience function for calling [`StreamMuxer::poll_inbound`] for
139+ /// [`StreamMuxer`]s that are `Unpin`.
124140 fn poll_inbound_unpin (
125141 & mut self ,
126142 cx : & mut Context < ' _ > ,
@@ -131,7 +147,8 @@ pub trait StreamMuxerExt: StreamMuxer + Sized {
131147 Pin :: new ( self ) . poll_inbound ( cx)
132148 }
133149
134- /// Convenience function for calling [`StreamMuxer::poll_outbound`] for [`StreamMuxer`]s that are `Unpin`.
150+ /// Convenience function for calling [`StreamMuxer::poll_outbound`] for
151+ /// [`StreamMuxer`]s that are `Unpin`.
135152 fn poll_outbound_unpin (
136153 & mut self ,
137154 cx : & mut Context < ' _ > ,
@@ -142,15 +159,17 @@ pub trait StreamMuxerExt: StreamMuxer + Sized {
142159 Pin :: new ( self ) . poll_outbound ( cx)
143160 }
144161
145- /// Convenience function for calling [`StreamMuxer::poll`] for [`StreamMuxer`]s that are `Unpin`.
162+ /// Convenience function for calling [`StreamMuxer::poll`] for
163+ /// [`StreamMuxer`]s that are `Unpin`.
146164 fn poll_unpin ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < StreamMuxerEvent , Self :: Error > >
147165 where
148166 Self : Unpin ,
149167 {
150168 Pin :: new ( self ) . poll ( cx)
151169 }
152170
153- /// Convenience function for calling [`StreamMuxer::poll_close`] for [`StreamMuxer`]s that are `Unpin`.
171+ /// Convenience function for calling [`StreamMuxer::poll_close`] for
172+ /// [`StreamMuxer`]s that are `Unpin`.
154173 fn poll_close_unpin ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > >
155174 where
156175 Self : Unpin ,
0 commit comments