Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Fix some nits using clippy #8731

Merged
merged 2 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions util/network-devp2p/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ impl Handshake {
/// Create a new handshake object
pub fn new(token: StreamToken, id: Option<&NodeId>, socket: TcpStream, nonce: &H256) -> Result<Handshake, Error> {
Ok(Handshake {
id: if let Some(id) = id { id.clone()} else { NodeId::new() },
id: if let Some(id) = id { *id } else { NodeId::new() },
connection: Connection::new(token, socket),
originated: false,
state: HandshakeState::New,
ecdhe: Random.generate()?,
nonce: nonce.clone(),
nonce: *nonce,
remote_ephemeral: Public::new(),
remote_nonce: H256::new(),
remote_version: PROTOCOL_VERSION,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl Handshake {
self.remote_version = remote_version;
let shared = *ecdh::agree(host_secret, &self.id)?;
let signature = H520::from_slice(sig);
self.remote_ephemeral = recover(&signature.into(), &(&shared ^ &self.remote_nonce))?;
self.remote_ephemeral = recover(&signature.into(), &(shared ^ self.remote_nonce))?;
Ok(())
}

Expand All @@ -189,7 +189,7 @@ impl Handshake {
}
Err(_) => {
// Try to interpret as EIP-8 packet
let total = (((data[0] as u16) << 8 | (data[1] as u16)) as usize) + 2;
let total = ((u16::from(data[0]) << 8 | (u16::from(data[1]))) as usize) + 2;
if total < V4_AUTH_PACKET_SIZE {
debug!(target: "network", "Wrong EIP8 auth packet size");
return Err(ErrorKind::BadProtocol.into());
Expand Down Expand Up @@ -232,7 +232,7 @@ impl Handshake {
}
Err(_) => {
// Try to interpret as EIP-8 packet
let total = (((data[0] as u16) << 8 | (data[1] as u16)) as usize) + 2;
let total = (((u16::from(data[0])) << 8 | (u16::from(data[1]))) as usize) + 2;
if total < V4_ACK_PACKET_SIZE {
debug!(target: "network", "Wrong EIP8 ack packet size");
return Err(ErrorKind::BadProtocol.into());
Expand Down
30 changes: 15 additions & 15 deletions util/network-devp2p/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ impl<'s> NetworkContext<'s> {
) -> NetworkContext<'s> {
let id = session.as_ref().map(|s| s.lock().token());
NetworkContext {
io: io,
protocol: protocol,
io,
protocol,
session_id: id,
session: session,
sessions: sessions,
session,
sessions,
_reserved_peers: reserved_peers,
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ impl Host {
let tcp_listener = TcpListener::bind(&listen_address)?;
listen_address = SocketAddr::new(listen_address.ip(), tcp_listener.local_addr()?.port());
debug!(target: "network", "Listening at {:?}", listen_address);
let udp_port = config.udp_port.unwrap_or(listen_address.port());
let udp_port = config.udp_port.unwrap_or_else(|| listen_address.port());
let local_endpoint = NodeEndpoint { address: listen_address, udp_port: udp_port };

let boot_nodes = config.boot_nodes.clone();
Expand Down Expand Up @@ -325,7 +325,7 @@ impl Host {
match Node::from_str(id) {
Err(e) => { debug!(target: "network", "Could not add node {}: {:?}", id, e); },
Ok(n) => {
let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() };
let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id };

self.nodes.write().add_node(n);
if let Some(ref mut discovery) = *self.discovery.lock() {
Expand All @@ -338,9 +338,9 @@ impl Host {
pub fn add_reserved_node(&self, id: &str) -> Result<(), Error> {
let n = Node::from_str(id)?;

let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() };
self.reserved_nodes.write().insert(n.id.clone());
self.nodes.write().add_node(Node::new(entry.id.clone(), entry.endpoint.clone()));
let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id };
self.reserved_nodes.write().insert(n.id);
self.nodes.write().add_node(Node::new(entry.id, entry.endpoint.clone()));

if let Some(ref mut discovery) = *self.discovery.lock() {
discovery.add_node(entry);
Expand All @@ -349,10 +349,10 @@ impl Host {
Ok(())
}

pub fn set_non_reserved_mode(&self, mode: NonReservedPeerMode, io: &IoContext<NetworkIoMessage>) {
pub fn set_non_reserved_mode(&self, mode: &NonReservedPeerMode, io: &IoContext<NetworkIoMessage>) {
let mut info = self.info.write();

if info.config.non_reserved_mode != mode {
if &info.config.non_reserved_mode != mode {
info.config.non_reserved_mode = mode.clone();
drop(info);
if let NonReservedPeerMode::Deny = mode {
Expand Down Expand Up @@ -388,12 +388,12 @@ impl Host {

pub fn external_url(&self) -> Option<String> {
let info = self.info.read();
info.public_endpoint.as_ref().map(|e| format!("{}", Node::new(info.id().clone(), e.clone())))
info.public_endpoint.as_ref().map(|e| format!("{}", Node::new(*info.id(), e.clone())))
}

pub fn local_url(&self) -> String {
let info = self.info.read();
format!("{}", Node::new(info.id().clone(), info.local_endpoint.clone()))
format!("{}", Node::new(*info.id(), info.local_endpoint.clone()))
}

pub fn stop(&self, io: &IoContext<NetworkIoMessage>) {
Expand Down Expand Up @@ -554,7 +554,7 @@ impl Host {
// iterate over all nodes, reserved ones coming first.
// if we are pinned to only reserved nodes, ignore all others.
let nodes = reserved_nodes.iter().cloned().chain(if !pin {
self.nodes.read().nodes(allow_ips)
self.nodes.read().nodes(&allow_ips)
} else {
Vec::new()
});
Expand Down Expand Up @@ -752,7 +752,7 @@ impl Host {
let entry = NodeEntry { id: id, endpoint: endpoint };
let mut nodes = self.nodes.write();
if !nodes.contains(&entry.id) {
nodes.add_node(Node::new(entry.id.clone(), entry.endpoint.clone()));
nodes.add_node(Node::new(entry.id, entry.endpoint.clone()));
let mut discovery = self.discovery.lock();
if let Some(ref mut discovery) = *discovery {
discovery.add_node(entry);
Expand Down
42 changes: 21 additions & 21 deletions util/network-devp2p/src/ip_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl SocketAddrExt for Ipv4Addr {

fn is_within(&self, ipnet: &IpNetwork) -> bool {
match ipnet {
&IpNetwork::V4(ipnet) => ipnet.contains(*self),
IpNetwork::V4(ipnet) => ipnet.contains(*self),
_ => false
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ impl SocketAddrExt for Ipv6Addr {

fn is_within(&self, ipnet: &IpNetwork) -> bool {
match ipnet {
&IpNetwork::V6(ipnet) => ipnet.contains(*self),
IpNetwork::V6(ipnet) => ipnet.contains(*self),
_ => false
}
}
Expand Down Expand Up @@ -212,28 +212,28 @@ impl SocketAddrExt for IpAddr {

#[cfg(not(any(windows, target_os = "android")))]
mod getinterfaces {
use std::{mem, io, ptr};
use std::{mem, io};
use libc::{AF_INET, AF_INET6};
use libc::{getifaddrs, freeifaddrs, ifaddrs, sockaddr, sockaddr_in, sockaddr_in6};
use std::net::{Ipv4Addr, Ipv6Addr, IpAddr};

fn convert_sockaddr(sa: *mut sockaddr) -> Option<IpAddr> {
if sa == ptr::null_mut() { return None; }
if sa.is_null() { return None; }

let (addr, _) = match unsafe { *sa }.sa_family as i32 {
let (addr, _) = match i32::from(unsafe { *sa }.sa_family) {
AF_INET => {
let sa: *const sockaddr_in = unsafe { mem::transmute(sa) };
let sa = & unsafe { *sa };
let sa: *const sockaddr_in = sa as *const sockaddr_in;
let sa = unsafe { &*sa };
let (addr, port) = (sa.sin_addr.s_addr, sa.sin_port);
(IpAddr::V4(Ipv4Addr::new(
(addr & 0x000000FF) as u8,
((addr & 0x0000FF00) >> 8) as u8,
((addr & 0x00FF0000) >> 16) as u8,
((addr & 0xFF000000) >> 24) as u8)),
(addr & 0x0000_00FF) as u8,
((addr & 0x0000_FF00) >> 8) as u8,
((addr & 0x00FF_0000) >> 16) as u8,
((addr & 0xFF00_0000) >> 24) as u8)),
port)
},
AF_INET6 => {
let sa: *const sockaddr_in6 = unsafe { mem::transmute(sa) };
let sa: *const sockaddr_in6 = sa as *const sockaddr_in6;
let sa = & unsafe { *sa };
let (addr, port) = (sa.sin6_addr.s6_addr, sa.sin6_port);
let addr: [u16; 8] = unsafe { mem::transmute(addr) };
Expand Down Expand Up @@ -266,7 +266,7 @@ mod getinterfaces {

let mut ret = Vec::new();
let mut cur: *mut ifaddrs = ifap;
while cur != ptr::null_mut() {
while !cur.is_null() {
if let Some(ip_addr) = convert_ifaddrs(cur) {
ret.push(ip_addr);
}
Expand Down Expand Up @@ -297,16 +297,16 @@ pub fn select_public_address(port: u16) -> SocketAddr {
//prefer IPV4 bindings
for addr in &list { //TODO: use better criteria than just the first in the list
match addr {
&IpAddr::V4(a) if !a.is_reserved() => {
return SocketAddr::V4(SocketAddrV4::new(a, port));
IpAddr::V4(a) if !a.is_reserved() => {
return SocketAddr::V4(SocketAddrV4::new(*a, port));
},
_ => {},
}
}
for addr in &list {
match addr {
&IpAddr::V6(a) if !a.is_reserved() => {
return SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0));
IpAddr::V6(a) if !a.is_reserved() => {
return SocketAddr::V6(SocketAddrV6::new(*a, port, 0, 0));
},
_ => {},
}
Expand All @@ -319,25 +319,25 @@ pub fn select_public_address(port: u16) -> SocketAddr {

pub fn map_external_address(local: &NodeEndpoint) -> Option<NodeEndpoint> {
if let SocketAddr::V4(ref local_addr) = local.address {
match search_gateway_from_timeout(local_addr.ip().clone(), Duration::new(5, 0)) {
match search_gateway_from_timeout(*local_addr.ip(), Duration::new(5, 0)) {
Err(ref err) => debug!("Gateway search error: {}", err),
Ok(gateway) => {
match gateway.get_external_ip() {
Err(ref err) => {
debug!("IP request error: {}", err);
},
Ok(external_addr) => {
match gateway.add_any_port(PortMappingProtocol::TCP, SocketAddrV4::new(local_addr.ip().clone(), local_addr.port()), 0, "Parity Node/TCP") {
match gateway.add_any_port(PortMappingProtocol::TCP, SocketAddrV4::new(*local_addr.ip(), local_addr.port()), 0, "Parity Node/TCP") {
Err(ref err) => {
debug!("Port mapping error: {}", err);
},
Ok(tcp_port) => {
match gateway.add_any_port(PortMappingProtocol::UDP, SocketAddrV4::new(local_addr.ip().clone(), local.udp_port), 0, "Parity Node/UDP") {
match gateway.add_any_port(PortMappingProtocol::UDP, SocketAddrV4::new(*local_addr.ip(), local.udp_port), 0, "Parity Node/UDP") {
Err(ref err) => {
debug!("Port mapping error: {}", err);
},
Ok(udp_port) => {
return Some(NodeEndpoint { address: SocketAddr::V4(SocketAddrV4::new(external_addr, tcp_port)), udp_port: udp_port });
return Some(NodeEndpoint { address: SocketAddr::V4(SocketAddrV4::new(external_addr, tcp_port)), udp_port });
},
}
},
Expand Down
Loading