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
This commit is contained in:
Max Inden
2021-03-18 14:55:33 +01:00
committed by GitHub
parent 5a45f93fc2
commit 63512e5f16
37 changed files with 313 additions and 274 deletions

View File

@@ -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 {
@@ -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
@@ -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()));
}
})
}