Skip to content

Commit f701b24

Browse files
*: Format with rustfmt (#2188)
Co-authored-by: Thomas Eizinger <[email protected]>
1 parent 0085612 commit f701b24

File tree

171 files changed

+10002
-7144
lines changed

Some content is hidden

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

171 files changed

+10002
-7144
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,24 @@ jobs:
182182

183183
- name: Run ipfs-kad example
184184
run: RUST_LOG=libp2p_swarm=debug,libp2p_kad=trace,libp2p_tcp=debug cargo run --example ipfs-kad
185+
186+
rustfmt:
187+
runs-on: ubuntu-latest
188+
steps:
189+
190+
- name: Cancel Previous Runs
191+
uses: styfle/[email protected]
192+
with:
193+
access_token: ${{ github.token }}
194+
195+
- uses: actions/[email protected]
196+
197+
- uses: actions-rs/[email protected]
198+
with:
199+
profile: minimal
200+
toolchain: stable
201+
override: true
202+
components: rustfmt
203+
204+
- name: Check formatting
205+
run: cargo fmt -- --check

core/benches/peer_id.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ fn from_bytes(c: &mut Criterion) {
3535
}
3636

3737
fn clone(c: &mut Criterion) {
38-
let peer_id = identity::Keypair::generate_ed25519()
39-
.public()
40-
.to_peer_id();
38+
let peer_id = identity::Keypair::generate_ed25519().public().to_peer_id();
4139

4240
c.bench_function("clone", |b| {
4341
b.iter(|| {
@@ -48,11 +46,7 @@ fn clone(c: &mut Criterion) {
4846

4947
fn sort_vec(c: &mut Criterion) {
5048
let peer_ids: Vec<_> = (0..100)
51-
.map(|_| {
52-
identity::Keypair::generate_ed25519()
53-
.public()
54-
.to_peer_id()
55-
})
49+
.map(|_| identity::Keypair::generate_ed25519().public().to_peer_id())
5650
.collect();
5751

5852
c.bench_function("sort_vec", |b| {

core/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
fn main() {
22-
prost_build::compile_protos(&["src/keys.proto"], &["src"]).unwrap();
22+
prost_build::compile_protos(&["src/keys.proto"], &["src"]).unwrap();
2323
}

core/src/connection.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ pub(crate) mod pool;
2828

2929
pub use error::{ConnectionError, PendingConnectionError};
3030
pub use handler::{ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler};
31-
pub use listeners::{ListenerId, ListenersStream, ListenersEvent};
31+
pub use listeners::{ListenerId, ListenersEvent, ListenersStream};
3232
pub use manager::ConnectionId;
33-
pub use substream::{Substream, SubstreamEndpoint, Close};
33+
pub use pool::{ConnectionCounters, ConnectionLimits};
3434
pub use pool::{EstablishedConnection, EstablishedConnectionIter, PendingConnection};
35-
pub use pool::{ConnectionLimits, ConnectionCounters};
35+
pub use substream::{Close, Substream, SubstreamEndpoint};
3636

3737
use crate::muxing::StreamMuxer;
3838
use crate::{Multiaddr, PeerId};
39-
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
4039
use std::hash::Hash;
40+
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
4141
use substream::{Muxing, SubstreamEvent};
4242

4343
/// The endpoint roles associated with a peer-to-peer communication channel.
@@ -55,7 +55,7 @@ impl std::ops::Not for Endpoint {
5555
fn not(self) -> Self::Output {
5656
match self {
5757
Endpoint::Dialer => Endpoint::Listener,
58-
Endpoint::Listener => Endpoint::Dialer
58+
Endpoint::Listener => Endpoint::Dialer,
5959
}
6060
}
6161
}
@@ -86,7 +86,7 @@ pub enum ConnectedPoint {
8686
local_addr: Multiaddr,
8787
/// Stack of protocols used to send back data to the remote.
8888
send_back_addr: Multiaddr,
89-
}
89+
},
9090
}
9191

9292
impl From<&'_ ConnectedPoint> for Endpoint {
@@ -106,23 +106,23 @@ impl ConnectedPoint {
106106
pub fn to_endpoint(&self) -> Endpoint {
107107
match self {
108108
ConnectedPoint::Dialer { .. } => Endpoint::Dialer,
109-
ConnectedPoint::Listener { .. } => Endpoint::Listener
109+
ConnectedPoint::Listener { .. } => Endpoint::Listener,
110110
}
111111
}
112112

113113
/// Returns true if we are `Dialer`.
114114
pub fn is_dialer(&self) -> bool {
115115
match self {
116116
ConnectedPoint::Dialer { .. } => true,
117-
ConnectedPoint::Listener { .. } => false
117+
ConnectedPoint::Listener { .. } => false,
118118
}
119119
}
120120

121121
/// Returns true if we are `Listener`.
122122
pub fn is_listener(&self) -> bool {
123123
match self {
124124
ConnectedPoint::Dialer { .. } => false,
125-
ConnectedPoint::Listener { .. } => true
125+
ConnectedPoint::Listener { .. } => true,
126126
}
127127
}
128128

@@ -237,20 +237,24 @@ where
237237

238238
/// Polls the connection for events produced by the associated handler
239239
/// as a result of I/O activity on the substream multiplexer.
240-
pub fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
241-
-> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>>
242-
{
240+
pub fn poll(
241+
mut self: Pin<&mut Self>,
242+
cx: &mut Context<'_>,
243+
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
243244
loop {
244245
let mut io_pending = false;
245246

246247
// Perform I/O on the connection through the muxer, informing the handler
247248
// of new substreams.
248249
match self.muxing.poll(cx) {
249250
Poll::Pending => io_pending = true,
250-
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => {
251-
self.handler.inject_substream(substream, SubstreamEndpoint::Listener)
252-
}
253-
Poll::Ready(Ok(SubstreamEvent::OutboundSubstream { user_data, substream })) => {
251+
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => self
252+
.handler
253+
.inject_substream(substream, SubstreamEndpoint::Listener),
254+
Poll::Ready(Ok(SubstreamEvent::OutboundSubstream {
255+
user_data,
256+
substream,
257+
})) => {
254258
let endpoint = SubstreamEndpoint::Dialer(user_data);
255259
self.handler.inject_substream(substream, endpoint)
256260
}
@@ -265,7 +269,7 @@ where
265269
match self.handler.poll(cx) {
266270
Poll::Pending => {
267271
if io_pending {
268-
return Poll::Pending // Nothing to do
272+
return Poll::Pending; // Nothing to do
269273
}
270274
}
271275
Poll::Ready(Ok(ConnectionHandlerEvent::OutboundSubstreamRequest(user_data))) => {
@@ -310,7 +314,7 @@ impl<'a> OutgoingInfo<'a> {
310314
/// Builds a `ConnectedPoint` corresponding to the outgoing connection.
311315
pub fn to_connected_point(&self) -> ConnectedPoint {
312316
ConnectedPoint::Dialer {
313-
address: self.address.clone()
317+
address: self.address.clone(),
314318
}
315319
}
316320
}

core/src/connection/error.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use crate::connection::ConnectionLimit;
2222
use crate::transport::TransportError;
23-
use std::{io, fmt};
23+
use std::{fmt, io};
2424

2525
/// Errors that can occur in the context of an established `Connection`.
2626
#[derive(Debug)]
@@ -33,23 +33,19 @@ pub enum ConnectionError<THandlerErr> {
3333
Handler(THandlerErr),
3434
}
3535

36-
impl<THandlerErr> fmt::Display
37-
for ConnectionError<THandlerErr>
36+
impl<THandlerErr> fmt::Display for ConnectionError<THandlerErr>
3837
where
3938
THandlerErr: fmt::Display,
4039
{
4140
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4241
match self {
43-
ConnectionError::IO(err) =>
44-
write!(f, "Connection error: I/O error: {}", err),
45-
ConnectionError::Handler(err) =>
46-
write!(f, "Connection error: Handler error: {}", err),
42+
ConnectionError::IO(err) => write!(f, "Connection error: I/O error: {}", err),
43+
ConnectionError::Handler(err) => write!(f, "Connection error: Handler error: {}", err),
4744
}
4845
}
4946
}
5047

51-
impl<THandlerErr> std::error::Error
52-
for ConnectionError<THandlerErr>
48+
impl<THandlerErr> std::error::Error for ConnectionError<THandlerErr>
5349
where
5450
THandlerErr: std::error::Error + 'static,
5551
{
@@ -80,29 +76,29 @@ pub enum PendingConnectionError<TTransErr> {
8076
IO(io::Error),
8177
}
8278

83-
impl<TTransErr> fmt::Display
84-
for PendingConnectionError<TTransErr>
79+
impl<TTransErr> fmt::Display for PendingConnectionError<TTransErr>
8580
where
8681
TTransErr: fmt::Display,
8782
{
8883
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8984
match self {
90-
PendingConnectionError::IO(err) =>
91-
write!(f, "Pending connection: I/O error: {}", err),
92-
PendingConnectionError::Transport(err) =>
93-
write!(f, "Pending connection: Transport error: {}", err),
94-
PendingConnectionError::InvalidPeerId =>
95-
write!(f, "Pending connection: Invalid peer ID."),
96-
PendingConnectionError::ConnectionLimit(l) =>
97-
write!(f, "Connection error: Connection limit: {}.", l),
85+
PendingConnectionError::IO(err) => write!(f, "Pending connection: I/O error: {}", err),
86+
PendingConnectionError::Transport(err) => {
87+
write!(f, "Pending connection: Transport error: {}", err)
88+
}
89+
PendingConnectionError::InvalidPeerId => {
90+
write!(f, "Pending connection: Invalid peer ID.")
91+
}
92+
PendingConnectionError::ConnectionLimit(l) => {
93+
write!(f, "Connection error: Connection limit: {}.", l)
94+
}
9895
}
9996
}
10097
}
10198

102-
impl<TTransErr> std::error::Error
103-
for PendingConnectionError<TTransErr>
99+
impl<TTransErr> std::error::Error for PendingConnectionError<TTransErr>
104100
where
105-
TTransErr: std::error::Error + 'static
101+
TTransErr: std::error::Error + 'static,
106102
{
107103
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
108104
match self {

core/src/connection/handler.rs

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

21+
use super::{Connected, SubstreamEndpoint};
2122
use crate::Multiaddr;
2223
use std::{fmt::Debug, task::Context, task::Poll};
23-
use super::{Connected, SubstreamEndpoint};
2424

2525
/// The interface of a connection handler.
2626
///
@@ -53,7 +53,11 @@ pub trait ConnectionHandler {
5353
/// Implementations are allowed to panic in the case of dialing if the `user_data` in
5454
/// `endpoint` doesn't correspond to what was returned earlier when polling, or is used
5555
/// multiple times.
56-
fn inject_substream(&mut self, substream: Self::Substream, endpoint: SubstreamEndpoint<Self::OutboundOpenInfo>);
56+
fn inject_substream(
57+
&mut self,
58+
substream: Self::Substream,
59+
endpoint: SubstreamEndpoint<Self::OutboundOpenInfo>,
60+
);
5761

5862
/// Notifies the handler of an event.
5963
fn inject_event(&mut self, event: Self::InEvent);
@@ -64,8 +68,10 @@ pub trait ConnectionHandler {
6468
/// Polls the handler for events.
6569
///
6670
/// Returning an error will close the connection to the remote.
67-
fn poll(&mut self, cx: &mut Context<'_>)
68-
-> Poll<Result<ConnectionHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>>;
71+
fn poll(
72+
&mut self,
73+
cx: &mut Context<'_>,
74+
) -> Poll<Result<ConnectionHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>>;
6975
}
7076

7177
/// Prototype for a `ConnectionHandler`.
@@ -82,7 +88,7 @@ pub trait IntoConnectionHandler {
8288

8389
impl<T> IntoConnectionHandler for T
8490
where
85-
T: ConnectionHandler
91+
T: ConnectionHandler,
8692
{
8793
type Handler = Self;
8894

@@ -91,9 +97,12 @@ where
9197
}
9298
}
9399

94-
pub(crate) type THandlerInEvent<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
95-
pub(crate) type THandlerOutEvent<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
96-
pub(crate) type THandlerError<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error;
100+
pub(crate) type THandlerInEvent<THandler> =
101+
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
102+
pub(crate) type THandlerOutEvent<THandler> =
103+
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
104+
pub(crate) type THandlerError<THandler> =
105+
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error;
97106

98107
/// Event produced by a handler.
99108
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -109,24 +118,26 @@ pub enum ConnectionHandlerEvent<TOutboundOpenInfo, TCustom> {
109118
impl<TOutboundOpenInfo, TCustom> ConnectionHandlerEvent<TOutboundOpenInfo, TCustom> {
110119
/// If this is `OutboundSubstreamRequest`, maps the content to something else.
111120
pub fn map_outbound_open_info<F, I>(self, map: F) -> ConnectionHandlerEvent<I, TCustom>
112-
where F: FnOnce(TOutboundOpenInfo) -> I
121+
where
122+
F: FnOnce(TOutboundOpenInfo) -> I,
113123
{
114124
match self {
115125
ConnectionHandlerEvent::OutboundSubstreamRequest(val) => {
116126
ConnectionHandlerEvent::OutboundSubstreamRequest(map(val))
117-
},
127+
}
118128
ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val),
119129
}
120130
}
121131

122132
/// If this is `Custom`, maps the content to something else.
123133
pub fn map_custom<F, I>(self, map: F) -> ConnectionHandlerEvent<TOutboundOpenInfo, I>
124-
where F: FnOnce(TCustom) -> I
134+
where
135+
F: FnOnce(TCustom) -> I,
125136
{
126137
match self {
127138
ConnectionHandlerEvent::OutboundSubstreamRequest(val) => {
128139
ConnectionHandlerEvent::OutboundSubstreamRequest(val)
129-
},
140+
}
130141
ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(map(val)),
131142
}
132143
}

0 commit comments

Comments
 (0)