Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Format with rustfmt #2188

Merged
merged 34 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2daec9c
Use type aliases to shorten types
thomaseizinger Jul 31, 2021
bfc7e11
.github/workflow: Enforce rustfmt
mxinden Aug 9, 2021
c3d1725
core/: Format with rustfmt
mxinden Aug 9, 2021
f5b82f6
protocols/floodsub: Format with rustfmt
mxinden Aug 9, 2021
470f3ef
protocols/gossipsub: Format with rustfmt
mxinden Aug 9, 2021
33b237e
protocols/identify: Format with rustfmt
mxinden Aug 9, 2021
48d8302
protocols/kad: Format with rustfmt
mxinden Aug 9, 2021
d9aec8b
protocols/mdns: Format with rustfmt
mxinden Aug 9, 2021
6d07f2b
protocols/ping: Format with rustfmt
mxinden Aug 9, 2021
ecf1cd7
protocols/relay: Format with rustfmt
mxinden Aug 9, 2021
432c3c8
protocols/request-response: Format with rustfmt
mxinden Aug 9, 2021
0a37752
misc/multistream-select: Format with rustfmt
mxinden Aug 9, 2021
22de20f
misc/peer-id-generator: Format with rustfmt
mxinden Aug 9, 2021
7957253
muxers/mplex: Format with rustfmt
mxinden Aug 9, 2021
cd247a5
muxers/yamux: Format with rustfmt
mxinden Aug 9, 2021
ada983a
swarm/: Format with rustfmt
mxinden Aug 9, 2021
acaec43
swarm-derive: Format with rustfmt
mxinden Aug 9, 2021
5c4479f
transports/deflate: Format with rustfmt
mxinden Aug 9, 2021
3279b77
transports/dns: Format with rustfmt
mxinden Aug 9, 2021
a4929f9
transports/noise: Format with rustfmt
mxinden Aug 9, 2021
6db1a16
transports/plaintext: Format with rustfmt
mxinden Aug 9, 2021
bbd8531
transports/pnet: Format with rustfmt
mxinden Aug 9, 2021
d75d0c8
transports/tcp: Format with rustfmt
mxinden Aug 9, 2021
91bd1e8
transports/uds: Format with rustfmt
mxinden Aug 9, 2021
3b08d68
transports/wasm-ext: Format with rustfmt
mxinden Aug 9, 2021
03945e7
transports/websocket: Format with rustfmt
mxinden Aug 9, 2021
f452e29
src/,examples/: Format with rustfmt
mxinden Aug 9, 2021
cd09d07
Revert "core/: Format with rustfmt"
mxinden Aug 11, 2021
d396cac
Revert "swarm/: Format with rustfmt"
mxinden Aug 11, 2021
f3ef87f
Merge branch 'libp2p/master' into rustfmt
mxinden Aug 11, 2021
dc8e962
core/: Format with rustfmt
mxinden Aug 11, 2021
656bda3
swarm/: Format with rustfmt
mxinden Aug 11, 2021
18fcf18
protocols/request-response: Format with rustfmt
mxinden Aug 11, 2021
3c56115
{examples,src/tutorial}: Format with rustfmt
mxinden Aug 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,24 @@ jobs:

- name: Run ipfs-kad example
run: RUST_LOG=libp2p_swarm=debug,libp2p_kad=trace,libp2p_tcp=debug cargo run --example ipfs-kad

rustfmt:
runs-on: ubuntu-latest
steps:

- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- uses: actions/[email protected]

- uses: actions-rs/[email protected]
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt

- name: Check formatting
run: cargo fmt -- --check
10 changes: 2 additions & 8 deletions core/benches/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ fn from_bytes(c: &mut Criterion) {
}

fn clone(c: &mut Criterion) {
let peer_id = identity::Keypair::generate_ed25519()
.public()
.to_peer_id();
let peer_id = identity::Keypair::generate_ed25519().public().to_peer_id();

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

fn sort_vec(c: &mut Criterion) {
let peer_ids: Vec<_> = (0..100)
.map(|_| {
identity::Keypair::generate_ed25519()
.public()
.to_peer_id()
})
.map(|_| identity::Keypair::generate_ed25519().public().to_peer_id())
.collect();

c.bench_function("sort_vec", |b| {
Expand Down
2 changes: 1 addition & 1 deletion core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
// DEALINGS IN THE SOFTWARE.

fn main() {
prost_build::compile_protos(&["src/keys.proto"], &["src"]).unwrap();
prost_build::compile_protos(&["src/keys.proto"], &["src"]).unwrap();
}
40 changes: 22 additions & 18 deletions core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ pub(crate) mod pool;

pub use error::{ConnectionError, PendingConnectionError};
pub use handler::{ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler};
pub use listeners::{ListenerId, ListenersStream, ListenersEvent};
pub use listeners::{ListenerId, ListenersEvent, ListenersStream};
pub use manager::ConnectionId;
pub use substream::{Substream, SubstreamEndpoint, Close};
pub use pool::{ConnectionCounters, ConnectionLimits};
pub use pool::{EstablishedConnection, EstablishedConnectionIter, PendingConnection};
pub use pool::{ConnectionLimits, ConnectionCounters};
pub use substream::{Close, Substream, SubstreamEndpoint};

use crate::muxing::StreamMuxer;
use crate::{Multiaddr, PeerId};
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
use std::hash::Hash;
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
use substream::{Muxing, SubstreamEvent};

/// The endpoint roles associated with a peer-to-peer communication channel.
Expand All @@ -55,7 +55,7 @@ impl std::ops::Not for Endpoint {
fn not(self) -> Self::Output {
match self {
Endpoint::Dialer => Endpoint::Listener,
Endpoint::Listener => Endpoint::Dialer
Endpoint::Listener => Endpoint::Dialer,
}
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ pub enum ConnectedPoint {
local_addr: Multiaddr,
/// Stack of protocols used to send back data to the remote.
send_back_addr: Multiaddr,
}
},
}

impl From<&'_ ConnectedPoint> for Endpoint {
Expand All @@ -106,23 +106,23 @@ impl ConnectedPoint {
pub fn to_endpoint(&self) -> Endpoint {
match self {
ConnectedPoint::Dialer { .. } => Endpoint::Dialer,
ConnectedPoint::Listener { .. } => Endpoint::Listener
ConnectedPoint::Listener { .. } => Endpoint::Listener,
}
}

/// Returns true if we are `Dialer`.
pub fn is_dialer(&self) -> bool {
match self {
ConnectedPoint::Dialer { .. } => true,
ConnectedPoint::Listener { .. } => false
ConnectedPoint::Listener { .. } => false,
}
}

/// Returns true if we are `Listener`.
pub fn is_listener(&self) -> bool {
match self {
ConnectedPoint::Dialer { .. } => false,
ConnectedPoint::Listener { .. } => true
ConnectedPoint::Listener { .. } => true,
}
}

Expand Down Expand Up @@ -237,20 +237,24 @@ where

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

// Perform I/O on the connection through the muxer, informing the handler
// of new substreams.
match self.muxing.poll(cx) {
Poll::Pending => io_pending = true,
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => {
self.handler.inject_substream(substream, SubstreamEndpoint::Listener)
}
Poll::Ready(Ok(SubstreamEvent::OutboundSubstream { user_data, substream })) => {
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => self
.handler
.inject_substream(substream, SubstreamEndpoint::Listener),
Poll::Ready(Ok(SubstreamEvent::OutboundSubstream {
user_data,
substream,
})) => {
let endpoint = SubstreamEndpoint::Dialer(user_data);
self.handler.inject_substream(substream, endpoint)
}
Expand All @@ -265,7 +269,7 @@ where
match self.handler.poll(cx) {
Poll::Pending => {
if io_pending {
return Poll::Pending // Nothing to do
return Poll::Pending; // Nothing to do
}
}
Poll::Ready(Ok(ConnectionHandlerEvent::OutboundSubstreamRequest(user_data))) => {
Expand Down Expand Up @@ -310,7 +314,7 @@ impl<'a> OutgoingInfo<'a> {
/// Builds a `ConnectedPoint` corresponding to the outgoing connection.
pub fn to_connected_point(&self) -> ConnectedPoint {
ConnectedPoint::Dialer {
address: self.address.clone()
address: self.address.clone(),
}
}
}
Expand Down
40 changes: 18 additions & 22 deletions core/src/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use crate::connection::ConnectionLimit;
use crate::transport::TransportError;
use std::{io, fmt};
use std::{fmt, io};

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

impl<THandlerErr> fmt::Display
for ConnectionError<THandlerErr>
impl<THandlerErr> fmt::Display for ConnectionError<THandlerErr>
where
THandlerErr: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConnectionError::IO(err) =>
write!(f, "Connection error: I/O error: {}", err),
ConnectionError::Handler(err) =>
write!(f, "Connection error: Handler error: {}", err),
ConnectionError::IO(err) => write!(f, "Connection error: I/O error: {}", err),
ConnectionError::Handler(err) => write!(f, "Connection error: Handler error: {}", err),
}
}
}

impl<THandlerErr> std::error::Error
for ConnectionError<THandlerErr>
impl<THandlerErr> std::error::Error for ConnectionError<THandlerErr>
where
THandlerErr: std::error::Error + 'static,
{
Expand Down Expand Up @@ -80,29 +76,29 @@ pub enum PendingConnectionError<TTransErr> {
IO(io::Error),
}

impl<TTransErr> fmt::Display
for PendingConnectionError<TTransErr>
impl<TTransErr> fmt::Display for PendingConnectionError<TTransErr>
where
TTransErr: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PendingConnectionError::IO(err) =>
write!(f, "Pending connection: I/O error: {}", err),
PendingConnectionError::Transport(err) =>
write!(f, "Pending connection: Transport error: {}", err),
PendingConnectionError::InvalidPeerId =>
write!(f, "Pending connection: Invalid peer ID."),
PendingConnectionError::ConnectionLimit(l) =>
write!(f, "Connection error: Connection limit: {}.", l),
PendingConnectionError::IO(err) => write!(f, "Pending connection: I/O error: {}", err),
PendingConnectionError::Transport(err) => {
write!(f, "Pending connection: Transport error: {}", err)
}
PendingConnectionError::InvalidPeerId => {
write!(f, "Pending connection: Invalid peer ID.")
}
PendingConnectionError::ConnectionLimit(l) => {
write!(f, "Connection error: Connection limit: {}.", l)
}
}
}
}

impl<TTransErr> std::error::Error
for PendingConnectionError<TTransErr>
impl<TTransErr> std::error::Error for PendingConnectionError<TTransErr>
where
TTransErr: std::error::Error + 'static
TTransErr: std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
27 changes: 17 additions & 10 deletions core/src/connection/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use super::{Connected, SubstreamEndpoint};
use crate::Multiaddr;
use std::{task::Context, task::Poll};
use super::{Connected, SubstreamEndpoint};

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

/// Notifies the handler of an event.
fn inject_event(&mut self, event: Self::InEvent);
Expand All @@ -64,8 +68,10 @@ pub trait ConnectionHandler {
/// Polls the handler for events.
///
/// Returning an error will close the connection to the remote.
fn poll(&mut self, cx: &mut Context<'_>)
-> Poll<Result<ConnectionHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>>;
fn poll(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<ConnectionHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>>;
}

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

impl<T> IntoConnectionHandler for T
where
T: ConnectionHandler
T: ConnectionHandler,
{
type Handler = Self;

Expand All @@ -105,26 +111,27 @@ pub enum ConnectionHandlerEvent<TOutboundOpenInfo, TCustom> {
impl<TOutboundOpenInfo, TCustom> ConnectionHandlerEvent<TOutboundOpenInfo, TCustom> {
/// If this is `OutboundSubstreamRequest`, maps the content to something else.
pub fn map_outbound_open_info<F, I>(self, map: F) -> ConnectionHandlerEvent<I, TCustom>
where F: FnOnce(TOutboundOpenInfo) -> I
where
F: FnOnce(TOutboundOpenInfo) -> I,
{
match self {
ConnectionHandlerEvent::OutboundSubstreamRequest(val) => {
ConnectionHandlerEvent::OutboundSubstreamRequest(map(val))
},
}
ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val),
}
}

/// If this is `Custom`, maps the content to something else.
pub fn map_custom<F, I>(self, map: F) -> ConnectionHandlerEvent<TOutboundOpenInfo, I>
where F: FnOnce(TCustom) -> I
where
F: FnOnce(TCustom) -> I,
{
match self {
ConnectionHandlerEvent::OutboundSubstreamRequest(val) => {
ConnectionHandlerEvent::OutboundSubstreamRequest(val)
},
}
ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(map(val)),
}
}
}

Loading