Skip to content

Commit 63512e5

Browse files
authored
swarm/src/lib: Remove Deref and DerefMut impls on Swarm (#1995)
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
1 parent 5a45f93 commit 63512e5

37 files changed

+313
-274
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Individual crates
32

43
## Main APIs
@@ -44,7 +43,15 @@
4443

4544
## Version 0.37.0 [unreleased]
4645

47-
- Update `libp2p-identify`.
46+
- Update individual crates.
47+
- `libp2p-floodsub`
48+
- `libp2p-gossipsub`
49+
- `libp2p-kad`
50+
- `libp2p-mdns`
51+
- `libp2p-ping`
52+
- `libp2p-relay`
53+
- `libp2p-request-response`
54+
- `libp2p-swarm`
4855

4956
## Version 0.36.0 [2021-03-17]
5057

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ bytes = "1"
6565
futures = "0.3.1"
6666
lazy_static = "1.2"
6767
libp2p-core = { version = "0.28.0", path = "core", default-features = false }
68-
libp2p-floodsub = { version = "0.28.0", path = "protocols/floodsub", optional = true }
69-
libp2p-gossipsub = { version = "0.29.0", path = "./protocols/gossipsub", optional = true }
68+
libp2p-floodsub = { version = "0.29.0", path = "protocols/floodsub", optional = true }
69+
libp2p-gossipsub = { version = "0.30.0", path = "./protocols/gossipsub", optional = true }
7070
libp2p-identify = { version = "0.29.0", path = "protocols/identify", optional = true }
71-
libp2p-kad = { version = "0.29.0", path = "protocols/kad", optional = true }
71+
libp2p-kad = { version = "0.30.0", path = "protocols/kad", optional = true }
7272
libp2p-mplex = { version = "0.28.0", path = "muxers/mplex", optional = true }
7373
libp2p-noise = { version = "0.30.0", path = "transports/noise", optional = true }
74-
libp2p-ping = { version = "0.28.0", path = "protocols/ping", optional = true }
74+
libp2p-ping = { version = "0.29.0", path = "protocols/ping", optional = true }
7575
libp2p-plaintext = { version = "0.28.0", path = "transports/plaintext", optional = true }
7676
libp2p-pnet = { version = "0.20.0", path = "transports/pnet", optional = true }
77-
libp2p-relay = { version = "0.1.0", path = "protocols/relay", optional = true }
78-
libp2p-request-response = { version = "0.10.0", path = "protocols/request-response", optional = true }
79-
libp2p-swarm = { version = "0.28.0", path = "swarm" }
77+
libp2p-relay = { version = "0.2.0", path = "protocols/relay", optional = true }
78+
libp2p-request-response = { version = "0.11.0", path = "protocols/request-response", optional = true }
79+
libp2p-swarm = { version = "0.29.0", path = "swarm" }
8080
libp2p-swarm-derive = { version = "0.22.0", path = "swarm-derive" }
8181
libp2p-uds = { version = "0.28.0", path = "transports/uds", optional = true }
8282
libp2p-wasm-ext = { version = "0.28.0", path = "transports/wasm-ext", default-features = false, optional = true }
@@ -90,7 +90,7 @@ wasm-timer = "0.2.4"
9090
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
9191
libp2p-deflate = { version = "0.28.0", path = "transports/deflate", optional = true }
9292
libp2p-dns = { version = "0.28.0", path = "transports/dns", optional = true, default-features = false }
93-
libp2p-mdns = { version = "0.29.0", path = "protocols/mdns", optional = true }
93+
libp2p-mdns = { version = "0.30.0", path = "protocols/mdns", optional = true }
9494
libp2p-tcp = { version = "0.28.0", path = "transports/tcp", default-features = false, optional = true }
9595
libp2p-websocket = { version = "0.29.0", path = "transports/websocket", optional = true }
9696

examples/chat-tokio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
138138
// Reach out to another node if specified
139139
if let Some(to_dial) = std::env::args().nth(1) {
140140
let addr: Multiaddr = to_dial.parse()?;
141-
Swarm::dial_addr(&mut swarm, addr)?;
141+
swarm.dial_addr(addr)?;
142142
println!("Dialed {:?}", to_dial)
143143
}
144144

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

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

151151
// Kick it off
152152
let mut listening = false;
@@ -166,7 +166,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
166166
}
167167
};
168168
if let Some((topic, line)) = to_publish {
169-
swarm.floodsub.publish(topic, line.as_bytes());
169+
swarm.behaviour_mut().floodsub.publish(topic, line.as_bytes());
170170
}
171171
if !listening {
172172
for addr in Swarm::listeners(&swarm) {

examples/chat.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
136136
// Reach out to another node if specified
137137
if let Some(to_dial) = std::env::args().nth(1) {
138138
let addr: Multiaddr = to_dial.parse()?;
139-
Swarm::dial_addr(&mut swarm, addr)?;
139+
swarm.dial_addr(addr)?;
140140
println!("Dialed {:?}", to_dial)
141141
}
142142

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

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

149149
// Kick it off
150150
let mut listening = false;
151151
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
152152
loop {
153153
match stdin.try_poll_next_unpin(cx)? {
154-
Poll::Ready(Some(line)) => swarm.floodsub.publish(floodsub_topic.clone(), line.as_bytes()),
154+
Poll::Ready(Some(line)) => swarm.behaviour_mut()
155+
.floodsub
156+
.publish(floodsub_topic.clone(), line.as_bytes()),
155157
Poll::Ready(None) => panic!("Stdin closed"),
156158
Poll::Pending => break
157159
}

examples/distributed-key-value-store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
161161
let mut stdin = io::BufReader::new(io::stdin()).lines();
162162

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

166166
// Kick it off.
167167
let mut listening = false;
168168
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
169169
loop {
170170
match stdin.try_poll_next_unpin(cx)? {
171-
Poll::Ready(Some(line)) => handle_input_line(&mut swarm.kademlia, line),
171+
Poll::Ready(Some(line)) => handle_input_line(&mut swarm.behaviour_mut().kademlia, line),
172172
Poll::Ready(None) => panic!("Stdin closed"),
173173
Poll::Pending => break
174174
}

examples/gossipsub-chat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
116116
};
117117

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

121121
// Reach out to another node if specified
122122
if let Some(to_dial) = std::env::args().nth(1) {
123123
let dialing = to_dial.clone();
124124
match to_dial.parse() {
125-
Ok(to_dial) => match libp2p::Swarm::dial_addr(&mut swarm, to_dial) {
125+
Ok(to_dial) => match swarm.dial_addr(to_dial) {
126126
Ok(_) => println!("Dialed {:?}", dialing),
127127
Err(e) => println!("Dial {:?} failed: {:?}", dialing, e),
128128
},
@@ -138,7 +138,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
138138
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
139139
loop {
140140
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
141-
Poll::Ready(Some(line)) => swarm.publish(topic.clone(), line.as_bytes()),
141+
Poll::Ready(Some(line)) => swarm.behaviour_mut().publish(topic.clone(), line.as_bytes()),
142142
Poll::Ready(None) => panic!("Stdin closed"),
143143
Poll::Pending => break,
144144
} {

examples/ipfs-kad.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
8686
};
8787

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

9191
// Kick it off!
9292
task::block_on(async move {

examples/ipfs-private.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,23 @@ fn main() -> Result<(), Box<dyn Error>> {
260260
// Reach out to other nodes if specified
261261
for to_dial in std::env::args().skip(1) {
262262
let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?;
263-
Swarm::dial_addr(&mut swarm, addr)?;
263+
swarm.dial_addr(addr)?;
264264
println!("Dialed {:?}", to_dial)
265265
}
266266

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

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

273273
// Kick it off
274274
let mut listening = false;
275275
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
276276
loop {
277277
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
278278
Poll::Ready(Some(line)) => swarm
279+
.behaviour_mut()
279280
.gossipsub
280281
.publish(gossipsub_topic.clone(), line.as_bytes()),
281282
Poll::Ready(None) => panic!("Stdin closed"),

examples/mdns-passive-discovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
4040
// Note that the MDNS behaviour itself will not actually inititiate any connections,
4141
// as it only uses UDP.
4242
let mut swarm = Swarm::new(transport, behaviour, peer_id);
43-
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
43+
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
4444

4545
loop {
4646
match swarm.next().await {

examples/ping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
7070
// command-line argument, if any.
7171
if let Some(addr) = std::env::args().nth(1) {
7272
let remote = addr.parse()?;
73-
Swarm::dial_addr(&mut swarm, remote)?;
73+
swarm.dial_addr(remote)?;
7474
println!("Dialed {}", addr)
7575
}
7676

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

8080
let mut listening = false;
8181
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {

0 commit comments

Comments
 (0)