Skip to content

Commit

Permalink
Add default implementations for NetworkBehaviour trait
Browse files Browse the repository at this point in the history
Not all implementations of `NetworkBehaviour` need all callbacks.
We've have been adding new callbacks with default implementations
for a while now. There is no reason the initial ones cannot also
be defaulted, thus making it easier create new implementations.
  • Loading branch information
thomaseizinger committed Jul 20, 2021
1 parent 20183c1 commit 2dd3abd
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ pub trait NetworkBehaviour: Send + 'static {
/// The addresses will be tried in the order returned by this function, which means that they
/// should be ordered by decreasing likelihood of reachability. In other words, the first
/// address should be the most likely to be reachable.
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
vec![]
}

/// Indicate to the behaviour that we connected to the node with the given peer id.
///
/// This node now has a handler (as spawned by `new_handler`) running in the background.
///
/// This method is only called when the first connection to the peer is established, preceded by
/// [`inject_connection_established`](NetworkBehaviour::inject_connection_established).
fn inject_connected(&mut self, peer_id: &PeerId);
fn inject_connected(&mut self, _: &PeerId) { }

/// Indicates to the behaviour that we disconnected from the node with the given peer id.
///
Expand All @@ -97,7 +99,7 @@ pub trait NetworkBehaviour: Send + 'static {
///
/// This method is only called when the last established connection to the peer is closed,
/// preceded by [`inject_connection_closed`](NetworkBehaviour::inject_connection_closed).
fn inject_disconnected(&mut self, peer_id: &PeerId);
fn inject_disconnected(&mut self, _: &PeerId) { }

/// Informs the behaviour about a newly established connection to a peer.
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
Expand Down Expand Up @@ -127,10 +129,10 @@ pub trait NetworkBehaviour: Send + 'static {
/// has previously been called with this `PeerId`.
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
);
_: PeerId,
_: ConnectionId,
_: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
) { }

/// Indicates to the behaviour that we tried to reach an address, but failed.
///
Expand Down

0 comments on commit 2dd3abd

Please sign in to comment.