Skip to content

Commit

Permalink
*: Format with rustfmt (#2188)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
  • Loading branch information
mxinden and thomaseizinger authored Aug 11, 2021
1 parent 0085612 commit f701b24
Show file tree
Hide file tree
Showing 171 changed files with 10,002 additions and 7,144 deletions.
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/cancel-workflow-action@0.9.0
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v2.3.4

- uses: actions-rs/toolchain@v1.0.7
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
35 changes: 23 additions & 12 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::{fmt::Debug, 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 @@ -91,9 +97,12 @@ where
}
}

pub(crate) type THandlerInEvent<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
pub(crate) type THandlerOutEvent<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
pub(crate) type THandlerError<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error;
pub(crate) type THandlerInEvent<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
pub(crate) type THandlerOutEvent<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
pub(crate) type THandlerError<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error;

/// Event produced by a handler.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand All @@ -109,24 +118,26 @@ 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)),
}
}
Expand Down
Loading

0 comments on commit f701b24

Please sign in to comment.