Fix some nits using clippy (#8731)

* fix some nits using clippy

* fix tests
This commit is contained in:
Niklas Adolfsson
2018-05-31 13:38:46 +02:00
committed by Afri Schoedon
parent 6b9314eaa9
commit 118588ef6c
7 changed files with 75 additions and 72 deletions

View File

@@ -26,7 +26,7 @@ use std::hash::{Hash, Hasher};
use std::net::{SocketAddr, ToSocketAddrs, SocketAddrV4, SocketAddrV6, Ipv4Addr, Ipv6Addr};
use std::path::PathBuf;
use std::str::FromStr;
use std::{fs, mem, slice};
use std::{fs, slice};
use std::time::{self, Duration, SystemTime};
use rand::{self, Rng};
@@ -45,8 +45,8 @@ pub struct NodeEndpoint {
impl NodeEndpoint {
pub fn udp_address(&self) -> SocketAddr {
match self.address {
SocketAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a.ip().clone(), self.udp_port)),
SocketAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a.ip().clone(), self.udp_port, a.flowinfo(), a.scope_id())),
SocketAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(*a.ip(), self.udp_port)),
SocketAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(*a.ip(), self.udp_port, a.flowinfo(), a.scope_id())),
}
}
@@ -61,10 +61,10 @@ impl NodeEndpoint {
pub fn is_allowed_by_predefined(&self, filter: &AllowIP) -> bool {
match filter {
&AllowIP::All => true,
&AllowIP::Private => self.address.ip().is_usable_private(),
&AllowIP::Public => self.address.ip().is_usable_public(),
&AllowIP::None => false,
AllowIP::All => true,
AllowIP::Private => self.address.ip().is_usable_private(),
AllowIP::Public => self.address.ip().is_usable_public(),
AllowIP::None => false,
}
}
@@ -75,13 +75,13 @@ impl NodeEndpoint {
let address = match addr_bytes.len() {
4 => Ok(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(addr_bytes[0], addr_bytes[1], addr_bytes[2], addr_bytes[3]), tcp_port))),
16 => unsafe {
let o: *const u16 = mem::transmute(addr_bytes.as_ptr());
let o: *const u16 = addr_bytes.as_ptr() as *const u16;
let o = slice::from_raw_parts(o, 8);
Ok(SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(o[0], o[1], o[2], o[3], o[4], o[5], o[6], o[7]), tcp_port, 0, 0)))
},
_ => Err(DecoderError::RlpInconsistentLengthAndData)
}?;
Ok(NodeEndpoint { address: address, udp_port: udp_port })
Ok(NodeEndpoint { address, udp_port })
}
pub fn to_rlp(&self, rlp: &mut RlpStream) {
@@ -90,7 +90,7 @@ impl NodeEndpoint {
rlp.append(&(&a.ip().octets()[..]));
}
SocketAddr::V6(a) => unsafe {
let o: *const u8 = mem::transmute(a.ip().segments().as_ptr());
let o: *const u8 = a.ip().segments().as_ptr() as *const u8;
rlp.append(&slice::from_raw_parts(o, 16));
}
};
@@ -184,8 +184,8 @@ pub struct Node {
impl Node {
pub fn new(id: NodeId, endpoint: NodeEndpoint) -> Node {
Node {
id: id,
endpoint: endpoint,
id,
endpoint,
peer_type: PeerType::Optional,
last_contact: None,
}
@@ -214,8 +214,8 @@ impl FromStr for Node {
};
Ok(Node {
id: id,
endpoint: endpoint,
id,
endpoint,
peer_type: PeerType::Optional,
last_contact: None,
})
@@ -258,7 +258,7 @@ impl NodeTable {
pub fn add_node(&mut self, mut node: Node) {
// preserve node last_contact
node.last_contact = self.nodes.get(&node.id).and_then(|n| n.last_contact);
self.nodes.insert(node.id.clone(), node);
self.nodes.insert(node.id, node);
}
/// Returns a list of ordered nodes according to their most recent contact
@@ -315,7 +315,7 @@ impl NodeTable {
/// Returns node ids sorted by failure percentage, for nodes with the same failure percentage the absolute number of
/// failures is considered.
pub fn nodes(&self, filter: IpFilter) -> Vec<NodeId> {
pub fn nodes(&self, filter: &IpFilter) -> Vec<NodeId> {
self.ordered_entries().iter()
.filter(|n| n.endpoint.is_allowed(&filter))
.map(|n| n.id)
@@ -327,7 +327,7 @@ impl NodeTable {
pub fn entries(&self) -> Vec<NodeEntry> {
self.ordered_entries().iter().map(|n| NodeEntry {
endpoint: n.endpoint.clone(),
id: n.id.clone(),
id: n.id,
}).collect()
}
@@ -344,7 +344,7 @@ impl NodeTable {
/// Apply table changes coming from discovery
pub fn update(&mut self, mut update: TableUpdates, reserved: &HashSet<NodeId>) {
for (_, node) in update.added.drain() {
let entry = self.nodes.entry(node.id.clone()).or_insert_with(|| Node::new(node.id.clone(), node.endpoint.clone()));
let entry = self.nodes.entry(node.id).or_insert_with(|| Node::new(node.id, node.endpoint.clone()));
entry.endpoint = node.endpoint;
}
for r in update.removed {
@@ -389,7 +389,7 @@ impl NodeTable {
return;
}
path.push(NODES_FILE);
let node_ids = self.nodes(IpFilter::default());
let node_ids = self.nodes(&IpFilter::default());
let nodes = node_ids.into_iter()
.map(|id| self.nodes.get(&id).expect("self.nodes() only returns node IDs from self.nodes"))
.take(MAX_NODES)
@@ -428,7 +428,7 @@ impl NodeTable {
Ok(table) => {
table.nodes.into_iter()
.filter_map(|n| n.into_node())
.map(|n| (n.id.clone(), n))
.map(|n| (n.id, n))
.collect()
},
Err(e) => {
@@ -625,7 +625,7 @@ mod tests {
// unknown - node 6
let r = table.nodes(IpFilter::default());
let r = table.nodes(&IpFilter::default());
assert_eq!(r[0][..], id4[..]); // most recent success
assert_eq!(r[1][..], id3[..]);
@@ -662,7 +662,7 @@ mod tests {
{
let table = NodeTable::new(Some(tempdir.path().to_str().unwrap().to_owned()));
let r = table.nodes(IpFilter::default());
let r = table.nodes(&IpFilter::default());
assert_eq!(r[0][..], id2[..]); // latest success
assert_eq!(r[1][..], id1[..]); // unknown
assert_eq!(r[2][..], id3[..]); // oldest failure