Skip to content

Commit e68a173

Browse files
committed
chore: introduce rustfmt.toml
1 parent 2371922 commit e68a173

File tree

364 files changed

+7469
-5523
lines changed

Some content is hidden

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

364 files changed

+7469
-5523
lines changed

core/src/connection.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,16 @@ pub enum ConnectedPoint {
7070
///
7171
/// - [`Endpoint::Dialer`] represents the default non-overriding option.
7272
///
73-
/// - [`Endpoint::Listener`] represents the overriding option.
74-
/// Realization depends on the transport protocol. E.g. in the case of
75-
/// TCP, both endpoints dial each other, resulting in a _simultaneous
76-
/// open_ TCP connection. On this new connection both endpoints assume
77-
/// to be the dialer of the connection. This is problematic during the
78-
/// connection upgrade process where an upgrade assumes one side to be
79-
/// the listener. With the help of this option, both peers can
80-
/// negotiate the roles (dialer and listener) for the new connection
81-
/// ahead of time, through some external channel, e.g. the DCUtR
82-
/// protocol, and thus have one peer dial the other and upgrade the
83-
/// connection as a dialer and one peer dial the other and upgrade the
84-
/// connection _as a listener_ overriding its role.
73+
/// - [`Endpoint::Listener`] represents the overriding option. Realization depends on the
74+
/// transport protocol. E.g. in the case of TCP, both endpoints dial each other,
75+
/// resulting in a _simultaneous open_ TCP connection. On this new connection both
76+
/// endpoints assume to be the dialer of the connection. This is problematic during the
77+
/// connection upgrade process where an upgrade assumes one side to be the listener. With
78+
/// the help of this option, both peers can negotiate the roles (dialer and listener) for
79+
/// the new connection ahead of time, through some external channel, e.g. the DCUtR
80+
/// protocol, and thus have one peer dial the other and upgrade the connection as a
81+
/// dialer and one peer dial the other and upgrade the connection _as a listener_
82+
/// overriding its role.
8583
role_override: Endpoint,
8684
/// Whether the port for the outgoing connection was reused from a listener
8785
/// or a new port was allocated. This is useful for address translation.

core/src/either.rs

Lines changed: 10 additions & 7 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

core/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@
2222
//!
2323
//! The main concepts of libp2p-core are:
2424
//!
25-
//! - The [`Transport`] trait defines how to reach a remote node or listen for
26-
//! incoming remote connections. See the [`transport`] module.
27-
//! - The [`StreamMuxer`] trait is implemented on structs that hold a connection
28-
//! to a remote and can subdivide this connection into multiple substreams.
29-
//! See the [`muxing`] module.
30-
//! - The [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] traits
31-
//! define how to upgrade each individual substream to use a protocol.
32-
//! See the `upgrade` module.
25+
//! - The [`Transport`] trait defines how to reach a remote node or listen for incoming remote
26+
//! connections. See the [`transport`] module.
27+
//! - The [`StreamMuxer`] trait is implemented on structs that hold a connection to a remote and can
28+
//! subdivide this connection into multiple substreams. See the [`muxing`] module.
29+
//! - The [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] traits define how to upgrade
30+
//! each individual substream to use a protocol. See the `upgrade` module.
3331
3432
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
3533

3634
mod proto {
3735
#![allow(unreachable_pub)]
3836
include!("generated/mod.rs");
3937
pub use self::{
40-
envelope_proto::*, peer_record_proto::mod_PeerRecord::*, peer_record_proto::PeerRecord,
38+
envelope_proto::*,
39+
peer_record_proto::{mod_PeerRecord::*, PeerRecord},
4140
};
4241
}
4342

core/src/muxing.rs

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

6170
mod 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.
6878
pub 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`].
122137
pub 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,

core/src/muxing/boxed.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use crate::muxing::{StreamMuxer, StreamMuxerEvent};
1+
use std::{
2+
error::Error,
3+
fmt, io,
4+
io::{IoSlice, IoSliceMut},
5+
pin::Pin,
6+
task::{Context, Poll},
7+
};
8+
29
use futures::{AsyncRead, AsyncWrite};
310
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};
11+
12+
use crate::muxing::{StreamMuxer, StreamMuxerEvent};
1013

1114
/// Abstract `StreamMuxer`.
1215
pub struct StreamMuxerBox {
@@ -21,8 +24,8 @@ impl fmt::Debug for StreamMuxerBox {
2124

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

2831
#[pin_project]
@@ -139,7 +142,8 @@ impl StreamMuxer for StreamMuxerBox {
139142
}
140143

141144
impl SubstreamBox {
142-
/// Construct a new [`SubstreamBox`] from something that implements [`AsyncRead`] and [`AsyncWrite`].
145+
/// Construct a new [`SubstreamBox`] from something that implements
146+
/// [`AsyncRead`] and [`AsyncWrite`].
143147
pub fn new<S: AsyncRead + AsyncWrite + Send + 'static>(stream: S) -> Self {
144148
Self(Box::pin(stream))
145149
}
@@ -155,7 +159,8 @@ impl fmt::Debug for SubstreamBox {
155159
trait AsyncReadWrite: AsyncRead + AsyncWrite {
156160
/// Helper function to capture the erased inner type.
157161
///
158-
/// Used to make the [`Debug`] implementation of [`SubstreamBox`] more useful.
162+
/// Used to make the [`Debug`] implementation of [`SubstreamBox`] more
163+
/// useful.
159164
fn type_name(&self) -> &'static str;
160165
}
161166

core/src/peer_record.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use crate::signed_envelope::SignedEnvelope;
2-
use crate::{proto, signed_envelope, DecodeError, Multiaddr};
3-
use libp2p_identity::Keypair;
4-
use libp2p_identity::PeerId;
5-
use libp2p_identity::SigningError;
1+
use libp2p_identity::{Keypair, PeerId, SigningError};
62
use quick_protobuf::{BytesReader, Writer};
73
use web_time::SystemTime;
84

5+
use crate::{proto, signed_envelope, signed_envelope::SignedEnvelope, DecodeError, Multiaddr};
6+
97
const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record";
108
const DOMAIN_SEP: &str = "libp2p-routing-state";
119

1210
/// Represents a peer routing record.
1311
///
14-
/// Peer records are designed to be distributable and carry a signature by being wrapped in a signed envelope.
15-
/// For more information see RFC0003 of the libp2p specifications: <https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md>
12+
/// Peer records are designed to be distributable and carry a signature by being
13+
/// wrapped in a signed envelope. For more information see RFC0003 of the libp2p specifications: <https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md>
1614
#[derive(Debug, PartialEq, Eq, Clone)]
1715
pub struct PeerRecord {
1816
peer_id: PeerId,
@@ -21,14 +19,16 @@ pub struct PeerRecord {
2119

2220
/// A signed envelope representing this [`PeerRecord`].
2321
///
24-
/// If this [`PeerRecord`] was constructed from a [`SignedEnvelope`], this is the original instance.
22+
/// If this [`PeerRecord`] was constructed from a [`SignedEnvelope`], this
23+
/// is the original instance.
2524
envelope: SignedEnvelope,
2625
}
2726

2827
impl PeerRecord {
2928
/// Attempt to re-construct a [`PeerRecord`] from a [`SignedEnvelope`].
3029
///
31-
/// If this function succeeds, the [`SignedEnvelope`] contained a peer record with a valid signature and can hence be considered authenticated.
30+
/// If this function succeeds, the [`SignedEnvelope`] contained a peer
31+
/// record with a valid signature and can hence be considered authenticated.
3232
pub fn from_signed_envelope(envelope: SignedEnvelope) -> Result<Self, FromEnvelopeError> {
3333
use quick_protobuf::MessageRead;
3434

@@ -58,9 +58,12 @@ impl PeerRecord {
5858
})
5959
}
6060

61-
/// Construct a new [`PeerRecord`] by authenticating the provided addresses with the given key.
61+
/// Construct a new [`PeerRecord`] by authenticating the provided addresses
62+
/// with the given key.
6263
///
63-
/// This is the same key that is used for authenticating every libp2p connection of your application, i.e. what you use when setting up your [`crate::transport::Transport`].
64+
/// This is the same key that is used for authenticating every libp2p
65+
/// connection of your application, i.e. what you use when setting up your
66+
/// [`crate::transport::Transport`].
6467
pub fn new(key: &Keypair, addresses: Vec<Multiaddr>) -> Result<Self, SigningError> {
6568
use quick_protobuf::MessageWrite;
6669

0 commit comments

Comments
 (0)