Skip to content

Commit

Permalink
swarm/src/lib: Remove Deref and DerefMut impls on Swarm (#1995)
Browse files Browse the repository at this point in the history
Remove `Deref` and `DerefMut` implementations previously dereferencing
to the `NetworkBehaviour` on `Swarm`. Instead one can access the
`NetworkBehaviour` via `Swarm::behaviour` and `Swarm::behaviour_mut`.
Methods on `Swarm` can now be accessed directly, e.g. via
`my_swarm.local_peer_id()`.

Reasoning: Accessing the `NetworkBehaviour` of a `Swarm` through `Deref`
and `DerefMut` instead of a method call is an unnecessary complication,
especially for newcomers. In addition, `Swarm` is not a smart-pointer
and should thus not make use of `Deref` and `DerefMut`, see documentation
from the standard library below.

> Deref should only be implemented for smart pointers to avoid
confusion.

https://doc.rust-lang.org/std/ops/trait.Deref.html
  • Loading branch information
mxinden authored Mar 18, 2021
1 parent 5a45f93 commit 63512e5
Show file tree
Hide file tree
Showing 37 changed files with 313 additions and 274 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Individual crates

## Main APIs
Expand Down Expand Up @@ -44,7 +43,15 @@

## Version 0.37.0 [unreleased]

- Update `libp2p-identify`.
- Update individual crates.
- `libp2p-floodsub`
- `libp2p-gossipsub`
- `libp2p-kad`
- `libp2p-mdns`
- `libp2p-ping`
- `libp2p-relay`
- `libp2p-request-response`
- `libp2p-swarm`

## Version 0.36.0 [2021-03-17]

Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ bytes = "1"
futures = "0.3.1"
lazy_static = "1.2"
libp2p-core = { version = "0.28.0", path = "core", default-features = false }
libp2p-floodsub = { version = "0.28.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.29.0", path = "./protocols/gossipsub", optional = true }
libp2p-floodsub = { version = "0.29.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.30.0", path = "./protocols/gossipsub", optional = true }
libp2p-identify = { version = "0.29.0", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.29.0", path = "protocols/kad", optional = true }
libp2p-kad = { version = "0.30.0", path = "protocols/kad", optional = true }
libp2p-mplex = { version = "0.28.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.30.0", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.28.0", path = "protocols/ping", optional = true }
libp2p-ping = { version = "0.29.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.28.0", path = "transports/plaintext", optional = true }
libp2p-pnet = { version = "0.20.0", path = "transports/pnet", optional = true }
libp2p-relay = { version = "0.1.0", path = "protocols/relay", optional = true }
libp2p-request-response = { version = "0.10.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.28.0", path = "swarm" }
libp2p-relay = { version = "0.2.0", path = "protocols/relay", optional = true }
libp2p-request-response = { version = "0.11.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.29.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.22.0", path = "swarm-derive" }
libp2p-uds = { version = "0.28.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.28.0", path = "transports/wasm-ext", default-features = false, optional = true }
Expand All @@ -90,7 +90,7 @@ wasm-timer = "0.2.4"
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.28.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.28.0", path = "transports/dns", optional = true, default-features = false }
libp2p-mdns = { version = "0.29.0", path = "protocols/mdns", optional = true }
libp2p-mdns = { version = "0.30.0", path = "protocols/mdns", optional = true }
libp2p-tcp = { version = "0.28.0", path = "transports/tcp", default-features = false, optional = true }
libp2p-websocket = { version = "0.29.0", path = "transports/websocket", optional = true }

Expand Down
6 changes: 3 additions & 3 deletions examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Reach out to another node if specified
if let Some(to_dial) = std::env::args().nth(1) {
let addr: Multiaddr = to_dial.parse()?;
Swarm::dial_addr(&mut swarm, addr)?;
swarm.dial_addr(addr)?;
println!("Dialed {:?}", to_dial)
}

// Read full lines from stdin
let mut stdin = io::BufReader::new(io::stdin()).lines();

// Listen on all interfaces and whatever port the OS assigns
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

// Kick it off
let mut listening = false;
Expand All @@ -166,7 +166,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
};
if let Some((topic, line)) = to_publish {
swarm.floodsub.publish(topic, line.as_bytes());
swarm.behaviour_mut().floodsub.publish(topic, line.as_bytes());
}
if !listening {
for addr in Swarm::listeners(&swarm) {
Expand Down
8 changes: 5 additions & 3 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Reach out to another node if specified
if let Some(to_dial) = std::env::args().nth(1) {
let addr: Multiaddr = to_dial.parse()?;
Swarm::dial_addr(&mut swarm, addr)?;
swarm.dial_addr(addr)?;
println!("Dialed {:?}", to_dial)
}

// Read full lines from stdin
let mut stdin = io::BufReader::new(io::stdin()).lines();

// Listen on all interfaces and whatever port the OS assigns
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

// Kick it off
let mut listening = false;
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
loop {
match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => swarm.floodsub.publish(floodsub_topic.clone(), line.as_bytes()),
Poll::Ready(Some(line)) => swarm.behaviour_mut()
.floodsub
.publish(floodsub_topic.clone(), line.as_bytes()),
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break
}
Expand Down
4 changes: 2 additions & 2 deletions examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut stdin = io::BufReader::new(io::stdin()).lines();

// Listen on all interfaces and whatever port the OS assigns.
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

// Kick it off.
let mut listening = false;
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
loop {
match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => handle_input_line(&mut swarm.kademlia, line),
Poll::Ready(Some(line)) => handle_input_line(&mut swarm.behaviour_mut().kademlia, line),
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break
}
Expand Down
6 changes: 3 additions & 3 deletions examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
};

// Listen on all interfaces and whatever port the OS assigns
libp2p::Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();

// Reach out to another node if specified
if let Some(to_dial) = std::env::args().nth(1) {
let dialing = to_dial.clone();
match to_dial.parse() {
Ok(to_dial) => match libp2p::Swarm::dial_addr(&mut swarm, to_dial) {
Ok(to_dial) => match swarm.dial_addr(to_dial) {
Ok(_) => println!("Dialed {:?}", dialing),
Err(e) => println!("Dial {:?} failed: {:?}", dialing, e),
},
Expand All @@ -138,7 +138,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
loop {
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => swarm.publish(topic.clone(), line.as_bytes()),
Poll::Ready(Some(line)) => swarm.behaviour_mut().publish(topic.clone(), line.as_bytes()),
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break,
} {
Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
};

println!("Searching for the closest peers to {:?}", to_search);
swarm.get_closest_peers(to_search);
swarm.behaviour_mut().get_closest_peers(to_search);

// Kick it off!
task::block_on(async move {
Expand Down
5 changes: 3 additions & 2 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,23 @@ fn main() -> Result<(), Box<dyn Error>> {
// Reach out to other nodes if specified
for to_dial in std::env::args().skip(1) {
let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?;
Swarm::dial_addr(&mut swarm, addr)?;
swarm.dial_addr(addr)?;
println!("Dialed {:?}", to_dial)
}

// Read full lines from stdin
let mut stdin = io::BufReader::new(io::stdin()).lines();

// Listen on all interfaces and whatever port the OS assigns
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

// Kick it off
let mut listening = false;
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
loop {
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => swarm
.behaviour_mut()
.gossipsub
.publish(gossipsub_topic.clone(), line.as_bytes()),
Poll::Ready(None) => panic!("Stdin closed"),
Expand Down
2 changes: 1 addition & 1 deletion examples/mdns-passive-discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Note that the MDNS behaviour itself will not actually inititiate any connections,
// as it only uses UDP.
let mut swarm = Swarm::new(transport, behaviour, peer_id);
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

loop {
match swarm.next().await {
Expand Down
4 changes: 2 additions & 2 deletions examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
// command-line argument, if any.
if let Some(addr) = std::env::args().nth(1) {
let remote = addr.parse()?;
Swarm::dial_addr(&mut swarm, remote)?;
swarm.dial_addr(remote)?;
println!("Dialed {}", addr)
}

// Tell the swarm to listen on all interfaces and a random, OS-assigned port.
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

let mut listening = false;
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
Expand Down
4 changes: 4 additions & 0 deletions protocols/floodsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.29.0 [unreleased]

- Update `libp2p-swarm`.

# 0.28.0 [2021-03-17]

- Update `libp2p-swarm`.
Expand Down
4 changes: 2 additions & 2 deletions protocols/floodsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-floodsub"
edition = "2018"
description = "Floodsub protocol for libp2p"
version = "0.28.0"
version = "0.29.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -14,7 +14,7 @@ cuckoofilter = "0.5.0"
fnv = "1.0"
futures = "0.3.1"
libp2p-core = { version = "0.28.0", path = "../../core" }
libp2p-swarm = { version = "0.28.0", path = "../../swarm" }
libp2p-swarm = { version = "0.29.0", path = "../../swarm" }
log = "0.4"
prost = "0.7"
rand = "0.7"
Expand Down
4 changes: 4 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.30.0 [unreleased]

- Update `libp2p-swarm`.

# 0.29.0 [2021-03-17]

- Update `libp2p-swarm`.
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
name = "libp2p-gossipsub"
edition = "2018"
description = "Gossipsub protocol for libp2p"
version = "0.29.0"
version = "0.30.0"
authors = ["Age Manning <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
libp2p-swarm = { version = "0.28.0", path = "../../swarm" }
libp2p-swarm = { version = "0.29.0", path = "../../swarm" }
libp2p-core = { version = "0.28.0", path = "../../core" }
bytes = "1.0"
byteorder = "1.3.4"
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
//!
//! // Listen on a memory transport.
//! let memory: Multiaddr = libp2p_core::multiaddr::Protocol::Memory(10).into();
//! let addr = libp2p_swarm::Swarm::listen_on(&mut swarm, memory).unwrap();
//! let addr = swarm.listen_on(memory).unwrap();
//! println!("Listening on {:?}", addr);
//! ```

Expand Down
6 changes: 3 additions & 3 deletions protocols/gossipsub/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn build_node() -> (Multiaddr, Swarm<Gossipsub>) {

let port = 1 + random::<u64>();
let mut addr: Multiaddr = Protocol::Memory(port).into();
Swarm::listen_on(&mut swarm, addr.clone()).unwrap();
swarm.listen_on(addr.clone()).unwrap();

addr = addr.with(libp2p_core::multiaddr::Protocol::P2p(
public_key.into_peer_id().into(),
Expand All @@ -196,7 +196,7 @@ fn multi_hop_propagation() {
// Subscribe each node to the same topic.
let topic = Topic::new("test-net");
for (_addr, node) in &mut graph.nodes {
node.subscribe(&topic).unwrap();
node.behaviour_mut().subscribe(&topic).unwrap();
}

// Wait for all nodes to be subscribed.
Expand All @@ -223,7 +223,7 @@ fn multi_hop_propagation() {
graph = graph.drain_poll();

// Publish a single message.
graph.nodes[0].1.publish(topic, vec![1, 2, 3]).unwrap();
graph.nodes[0].1.behaviour_mut().publish(topic, vec![1, 2, 3]).unwrap();

// Wait for all nodes to receive the published message.
let mut received_msgs = 0;
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories = ["network-programming", "asynchronous"]
[dependencies]
futures = "0.3.1"
libp2p-core = { version = "0.28.0", path = "../../core" }
libp2p-swarm = { version = "0.28.0", path = "../../swarm" }
libp2p-swarm = { version = "0.29.0", path = "../../swarm" }
log = "0.4.1"
prost = "0.7"
smallvec = "1.6.1"
Expand Down
6 changes: 3 additions & 3 deletions protocols/identify/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ mod tests {
(swarm, pubkey)
};

Swarm::listen_on(&mut swarm1, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
swarm1.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();

let listen_addr = async_std::task::block_on(async {
loop {
Expand All @@ -457,7 +457,7 @@ mod tests {
}
}
});
Swarm::dial_addr(&mut swarm2, listen_addr).unwrap();
swarm2.dial_addr(listen_addr).unwrap();

// nb. Either swarm may receive the `Identified` event first, upon which
// it will permit the connection to be closed, as defined by
Expand Down Expand Up @@ -560,7 +560,7 @@ mod tests {
}
}

swarm2.push(std::iter::once(pubkey1.clone().into_peer_id()));
swarm2.behaviour_mut().push(std::iter::once(pubkey1.clone().into_peer_id()));
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.30.0 [unreleased]

- Update `libp2p-swarm`.

# 0.29.0 [2021-03-17]

- Add `KademliaCaching` and `KademliaConfig::set_caching` to configure
Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-kad"
edition = "2018"
description = "Kademlia protocol for libp2p"
version = "0.29.0"
version = "0.30.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -18,7 +18,7 @@ asynchronous-codec = "0.6"
futures = "0.3.1"
log = "0.4"
libp2p-core = { version = "0.28.0", path = "../../core" }
libp2p-swarm = { version = "0.28.0", path = "../../swarm" }
libp2p-swarm = { version = "0.29.0", path = "../../swarm" }
prost = "0.7"
rand = "0.7.2"
sha2 = "0.9.1"
Expand Down
Loading

0 comments on commit 63512e5

Please sign in to comment.