*/: Replace Into with From (#2169)

Unless restricted by orphan rules, implementing `From` is superior
because it implies `Into` but leaves the choice to the user, which
one to use. Especially for errors, `From` is convenient because that
is what `?` builds on.

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Thomas Eizinger
2021-08-03 23:55:36 +10:00
committed by GitHub
parent e1f1af899b
commit 9d6562229f
7 changed files with 29 additions and 21 deletions

View File

@ -233,13 +233,13 @@ impl GossipsubRpc {
}
}
impl Into<rpc_proto::Rpc> for GossipsubRpc {
impl From<GossipsubRpc> for rpc_proto::Rpc {
/// Converts the RPC into protobuf format.
fn into(self) -> rpc_proto::Rpc {
fn from(rpc: GossipsubRpc) -> Self {
// Messages
let mut publish = Vec::new();
for message in self.messages.into_iter() {
for message in rpc.messages.into_iter() {
let message = rpc_proto::Message {
from: message.source.map(|m| m.to_bytes()),
data: Some(message.data),
@ -253,7 +253,7 @@ impl Into<rpc_proto::Rpc> for GossipsubRpc {
}
// subscriptions
let subscriptions = self
let subscriptions = rpc
.subscriptions
.into_iter()
.map(|sub| rpc_proto::rpc::SubOpts {
@ -270,9 +270,9 @@ impl Into<rpc_proto::Rpc> for GossipsubRpc {
prune: Vec::new(),
};
let empty_control_msg = self.control_msgs.is_empty();
let empty_control_msg = rpc.control_msgs.is_empty();
for action in self.control_msgs {
for action in rpc.control_msgs {
match action {
// collect all ihave messages
GossipsubControlAction::IHave {

View File

@ -71,10 +71,10 @@ impl From<proto::message::ConnectionType> for KadConnectionType {
}
}
impl Into<proto::message::ConnectionType> for KadConnectionType {
fn into(self) -> proto::message::ConnectionType {
impl From<KadConnectionType> for proto::message::ConnectionType {
fn from(val: KadConnectionType) -> Self {
use proto::message::ConnectionType::*;
match self {
match val {
KadConnectionType::NotConnected => NotConnected,
KadConnectionType::Connected => Connected,
KadConnectionType::CanConnect => CanConnect,
@ -123,13 +123,13 @@ impl TryFrom<proto::message::Peer> for KadPeer {
}
}
impl Into<proto::message::Peer> for KadPeer {
fn into(self) -> proto::message::Peer {
impl From<KadPeer> for proto::message::Peer {
fn from(peer: KadPeer) -> Self {
proto::message::Peer {
id: self.node_id.to_bytes(),
addrs: self.multiaddrs.into_iter().map(|a| a.to_vec()).collect(),
id: peer.node_id.to_bytes(),
addrs: peer.multiaddrs.into_iter().map(|a| a.to_vec()).collect(),
connection: {
let ct: proto::message::ConnectionType = self.connection_ty.into();
let ct: proto::message::ConnectionType = peer.connection_ty.into();
ct as i32
}
}