Simplify trait bounds on NetworkBehaviour (#1405)

* Simplify trait bounds requirements

* More work

* Moar

* Finish

* Fix final tests

* More simplification

* Use separate traits for Inbound/Outbound

* Update gossipsub and remove warnings

* Add documentation to swarm

* Remove BoxSubstream

* Fix tests not compiling

* Fix stack overflow

* Address concerns

* For some reason my IDE ignored libp2p-kad
This commit is contained in:
Pierre Krieger
2020-02-07 16:29:30 +01:00
committed by GitHub
parent 69852a580b
commit 1eff4b9823
32 changed files with 580 additions and 652 deletions

View File

@ -47,22 +47,20 @@ pub mod handler;
pub use handler::{PingConfig, PingResult, PingSuccess, PingFailure};
use handler::PingHandler;
use futures::prelude::*;
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use std::{collections::VecDeque, marker::PhantomData, task::Context, task::Poll};
use std::{collections::VecDeque, task::Context, task::Poll};
use void::Void;
/// `Ping` is a [`NetworkBehaviour`] that responds to inbound pings and
/// periodically sends outbound pings on every established connection.
///
/// See the crate root documentation for more information.
pub struct Ping<TSubstream> {
pub struct Ping {
/// Configuration for outbound pings.
config: PingConfig,
/// Queue of events to yield to the swarm.
events: VecDeque<PingEvent>,
_marker: PhantomData<TSubstream>,
}
/// Event generated by the `Ping` network behaviour.
@ -74,28 +72,24 @@ pub struct PingEvent {
pub result: PingResult,
}
impl<TSubstream> Ping<TSubstream> {
impl Ping {
/// Creates a new `Ping` network behaviour with the given configuration.
pub fn new(config: PingConfig) -> Self {
Ping {
config,
events: VecDeque::new(),
_marker: PhantomData,
}
}
}
impl<TSubstream> Default for Ping<TSubstream> {
impl Default for Ping {
fn default() -> Self {
Ping::new(PingConfig::new())
}
}
impl<TSubstream> NetworkBehaviour for Ping<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type ProtocolsHandler = PingHandler<TSubstream>;
impl NetworkBehaviour for Ping {
type ProtocolsHandler = PingHandler;
type OutEvent = PingEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {