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 ,
65+ AsyncWrite ,
66+ } ;
5467use multiaddr:: Multiaddr ;
55- use std:: future:: Future ;
56- use std:: pin:: Pin ;
5768
58- pub use self :: boxed:: StreamMuxerBox ;
59- pub use self :: boxed:: SubstreamBox ;
69+ pub use self :: boxed:: { StreamMuxerBox , SubstreamBox } ;
6070
6171mod boxed;
6272
6373/// Provides multiplexing for a connection by allowing users to open substreams.
6474///
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.
75+ /// A substream created by a [`StreamMuxer`] is a type that implements
76+ /// [`AsyncRead`] and [`AsyncWrite`]. The [`StreamMuxer`] itself is modelled
77+ /// closely after [`AsyncWrite`]. It features `poll`-style functions that allow
78+ /// the implementation to make progress on various tasks.
6879pub trait StreamMuxer {
69- /// Type of the object that represents the raw substream where data can be read and written.
80+ /// Type of the object that represents the raw substream where data can be
81+ /// read and written.
7082 type Substream : AsyncRead + AsyncWrite ;
7183
7284 /// Error type of the muxer
7385 type Error : std:: error:: Error ;
7486
7587 /// Poll for new inbound substreams.
7688 ///
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.
89+ /// This function should be called whenever callers are ready to accept more
90+ /// inbound streams. In other words, callers may exercise back-pressure
91+ /// on incoming streams by not calling this function if a certain limit
92+ /// is hit.
8093 fn poll_inbound (
8194 self : Pin < & mut Self > ,
8295 cx : & mut Context < ' _ > ,
@@ -90,20 +103,23 @@ pub trait StreamMuxer {
90103
91104 /// Poll to close this [`StreamMuxer`].
92105 ///
93- /// After this has returned `Poll::Ready(Ok(()))`, the muxer has become useless and may be safely
94- /// dropped.
106+ /// After this has returned `Poll::Ready(Ok(()))`, the muxer has become
107+ /// useless and may be safely dropped.
95108 ///
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.
109+ /// > **Note**: You are encouraged to call this method and wait for it to
110+ /// > return `Ready`, so
111+ /// > that the remote is properly informed of the shutdown. However, apart
112+ /// > from
113+ /// > properly informing the remote, there is no difference between this and
114+ /// > immediately dropping the muxer.
100115 fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > ;
101116
102117 /// Poll to allow the underlying connection to make progress.
103118 ///
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
119+ /// In contrast to all other `poll`-functions on [`StreamMuxer`], this
120+ /// function MUST be called unconditionally. Because it will be called
121+ /// regardless, this function can be used by implementations to return
122+ /// events about the underlying connection that the caller MUST deal
107123 /// with.
108124 fn poll (
109125 self : Pin < & mut Self > ,
@@ -120,7 +136,8 @@ pub enum StreamMuxerEvent {
120136
121137/// Extension trait for [`StreamMuxer`].
122138pub trait StreamMuxerExt : StreamMuxer + Sized {
123- /// Convenience function for calling [`StreamMuxer::poll_inbound`] for [`StreamMuxer`]s that are `Unpin`.
139+ /// Convenience function for calling [`StreamMuxer::poll_inbound`] for
140+ /// [`StreamMuxer`]s that are `Unpin`.
124141 fn poll_inbound_unpin (
125142 & mut self ,
126143 cx : & mut Context < ' _ > ,
@@ -131,7 +148,8 @@ pub trait StreamMuxerExt: StreamMuxer + Sized {
131148 Pin :: new ( self ) . poll_inbound ( cx)
132149 }
133150
134- /// Convenience function for calling [`StreamMuxer::poll_outbound`] for [`StreamMuxer`]s that are `Unpin`.
151+ /// Convenience function for calling [`StreamMuxer::poll_outbound`] for
152+ /// [`StreamMuxer`]s that are `Unpin`.
135153 fn poll_outbound_unpin (
136154 & mut self ,
137155 cx : & mut Context < ' _ > ,
@@ -142,15 +160,17 @@ pub trait StreamMuxerExt: StreamMuxer + Sized {
142160 Pin :: new ( self ) . poll_outbound ( cx)
143161 }
144162
145- /// Convenience function for calling [`StreamMuxer::poll`] for [`StreamMuxer`]s that are `Unpin`.
163+ /// Convenience function for calling [`StreamMuxer::poll`] for
164+ /// [`StreamMuxer`]s that are `Unpin`.
146165 fn poll_unpin ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < StreamMuxerEvent , Self :: Error > >
147166 where
148167 Self : Unpin ,
149168 {
150169 Pin :: new ( self ) . poll ( cx)
151170 }
152171
153- /// Convenience function for calling [`StreamMuxer::poll_close`] for [`StreamMuxer`]s that are `Unpin`.
172+ /// Convenience function for calling [`StreamMuxer::poll_close`] for
173+ /// [`StreamMuxer`]s that are `Unpin`.
154174 fn poll_close_unpin ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > >
155175 where
156176 Self : Unpin ,
0 commit comments