Skip to content

Commit 92af583

Browse files
committed
chore: rustfmt.toml
1 parent c9c44b1 commit 92af583

File tree

369 files changed

+8533
-5658
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

369 files changed

+8533
-5658
lines changed

core/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
- Update `Transport::dial` function signature with a `DialOpts` param and remove `Transport::dial_as_listener`:
99
- `DialOpts` struct contains `PortUse` and `Endpoint`,
10-
- `PortUse` allows controlling port allocation of new connections (defaults to `PortUse::Reuse`) -
10+
- `PortUse` allows controling port allocation of new connections (defaults to `PortUse::Reuse`) -
1111
- Add `port_use` field to `ConnectedPoint`
1212
- Set `endpoint` field in `DialOpts` to `Endpoint::Listener` to dial as a listener
1313
- Remove `Transport::address_translation` and relocate functionality to `libp2p_swarm`

core/src/connection.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ pub enum ConnectedPoint {
8383
/// connection as a dialer and one peer dial the other and upgrade the
8484
/// connection _as a listener_ overriding its role.
8585
role_override: Endpoint,
86-
/// Whether the port for the outgoing connection was reused from a listener
87-
/// or a new port was allocated. This is useful for address translation.
86+
/// Whether the port for the outgoing connection was reused from a
87+
/// listener or a new port was allocated. This is useful for
88+
/// address translation.
8889
///
89-
/// The port use is implemented on a best-effort basis. It is not guaranteed
90-
/// that [`PortUse::Reuse`] actually reused a port. A good example is the case
91-
/// where there is no listener available to reuse a port from.
90+
/// The port use is implemented on a best-effort basis. It is not
91+
/// guaranteed that [`PortUse::Reuse`] actually reused a port. A
92+
/// good example is the case where there is no listener
93+
/// available to reuse a port from.
9294
port_use: PortUse,
9395
},
9496
/// We received the node.
@@ -153,10 +155,11 @@ impl ConnectedPoint {
153155

154156
/// Returns the address of the remote stored in this struct.
155157
///
156-
/// For `Dialer`, this returns `address`. For `Listener`, this returns `send_back_addr`.
158+
/// For `Dialer`, this returns `address`. For `Listener`, this returns
159+
/// `send_back_addr`.
157160
///
158-
/// Note that the remote node might not be listening on this address and hence the address might
159-
/// not be usable to establish new connections.
161+
/// Note that the remote node might not be listening on this address and
162+
/// hence the address might not be usable to establish new connections.
160163
pub fn get_remote_address(&self) -> &Multiaddr {
161164
match self {
162165
ConnectedPoint::Dialer { address, .. } => address,
@@ -166,7 +169,8 @@ impl ConnectedPoint {
166169

167170
/// Modifies the address of the remote stored in this struct.
168171
///
169-
/// For `Dialer`, this modifies `address`. For `Listener`, this modifies `send_back_addr`.
172+
/// For `Dialer`, this modifies `address`. For `Listener`, this modifies
173+
/// `send_back_addr`.
170174
pub fn set_remote_address(&mut self, new_address: Multiaddr) {
171175
match self {
172176
ConnectedPoint::Dialer { address, .. } => *address = new_address,

core/src/either.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::muxing::StreamMuxerEvent;
22-
use crate::transport::DialOpts;
23-
use crate::{
24-
muxing::StreamMuxer,
25-
transport::{ListenerId, Transport, TransportError, TransportEvent},
26-
Multiaddr,
21+
use std::{
22+
pin::Pin,
23+
task::{Context, Poll},
2724
};
25+
2826
use either::Either;
2927
use futures::prelude::*;
3028
use pin_project::pin_project;
31-
use std::{pin::Pin, task::Context, task::Poll};
29+
30+
use crate::{
31+
muxing::{StreamMuxer, StreamMuxerEvent},
32+
transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent},
33+
Multiaddr,
34+
};
3235

3336
impl<A, B> StreamMuxer for future::Either<A, B>
3437
where
@@ -88,7 +91,8 @@ where
8891
}
8992
}
9093

91-
/// Implements `Future` and dispatches all method calls to either `First` or `Second`.
94+
/// Implements `Future` and dispatches all method calls to either `First` or
95+
/// `Second`.
9296
#[pin_project(project = EitherFutureProj)]
9397
#[derive(Debug, Copy, Clone)]
9498
#[must_use = "futures do nothing unless polled"]

core/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@
2828
//! to a remote and can subdivide this connection into multiple substreams.
2929
//! See the [`muxing`] module.
3030
//! - The [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] traits
31-
//! define how to upgrade each individual substream to use a protocol.
32-
//! See the `upgrade` module.
31+
//! define how to upgrade each individual substream to use a protocol. See the
32+
//! `upgrade` module.
3333
3434
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
3535

3636
mod proto {
3737
#![allow(unreachable_pub)]
3838
include!("generated/mod.rs");
3939
pub use self::{
40-
envelope_proto::*, peer_record_proto::mod_PeerRecord::*, peer_record_proto::PeerRecord,
40+
envelope_proto::*,
41+
peer_record_proto::{mod_PeerRecord::*, PeerRecord},
4142
};
4243
}
4344

core/src/muxing.rs

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,76 @@
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+
};
5467
use 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

6171
mod 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.
6879
pub 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`].
122138
pub 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,

core/src/muxing/boxed.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
use crate::muxing::{StreamMuxer, StreamMuxerEvent};
1+
use std::{
2+
error::Error,
3+
fmt,
4+
io,
5+
io::{IoSlice, IoSliceMut},
6+
pin::Pin,
7+
task::{Context, Poll},
8+
};
9+
210
use futures::{AsyncRead, AsyncWrite};
311
use pin_project::pin_project;
4-
use std::error::Error;
5-
use std::fmt;
6-
use std::io;
7-
use std::io::{IoSlice, IoSliceMut};
8-
use std::pin::Pin;
9-
use std::task::{Context, Poll};
12+
13+
use crate::muxing::{StreamMuxer, StreamMuxerEvent};
1014

1115
/// Abstract `StreamMuxer`.
1216
pub struct StreamMuxerBox {
@@ -21,8 +25,8 @@ impl fmt::Debug for StreamMuxerBox {
2125

2226
/// Abstract type for asynchronous reading and writing.
2327
///
24-
/// A [`SubstreamBox`] erases the concrete type it is given and only retains its `AsyncRead`
25-
/// and `AsyncWrite` capabilities.
28+
/// A [`SubstreamBox`] erases the concrete type it is given and only retains its
29+
/// `AsyncRead` and `AsyncWrite` capabilities.
2630
pub struct SubstreamBox(Pin<Box<dyn AsyncReadWrite + Send>>);
2731

2832
#[pin_project]
@@ -139,7 +143,8 @@ impl StreamMuxer for StreamMuxerBox {
139143
}
140144

141145
impl SubstreamBox {
142-
/// Construct a new [`SubstreamBox`] from something that implements [`AsyncRead`] and [`AsyncWrite`].
146+
/// Construct a new [`SubstreamBox`] from something that implements
147+
/// [`AsyncRead`] and [`AsyncWrite`].
143148
pub fn new<S: AsyncRead + AsyncWrite + Send + 'static>(stream: S) -> Self {
144149
Self(Box::pin(stream))
145150
}
@@ -155,7 +160,8 @@ impl fmt::Debug for SubstreamBox {
155160
trait AsyncReadWrite: AsyncRead + AsyncWrite {
156161
/// Helper function to capture the erased inner type.
157162
///
158-
/// Used to make the [`Debug`] implementation of [`SubstreamBox`] more useful.
163+
/// Used to make the [`Debug`] implementation of [`SubstreamBox`] more
164+
/// useful.
159165
fn type_name(&self) -> &'static str;
160166
}
161167

0 commit comments

Comments
 (0)