Skip to content

Commit

Permalink
Apply clippy recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
josephrhobbs committed Jul 23, 2024
1 parent 4888609 commit 7b3d149
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions proton_nat/src/nat.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Network Address Translation (NAT) table.

use std::net::{
Ipv4Addr,
SocketAddrV4,
use std::{
cmp::Ordering,
net::{
Ipv4Addr,
SocketAddrV4,
},
};

use bimap::BiMap;
Expand Down Expand Up @@ -39,6 +42,7 @@ pub struct NatTable {
available: usize,
}

#[allow(clippy::len_without_is_empty)]
impl NatTable {
/// Construct a new NAT table.
///
Expand Down Expand Up @@ -96,17 +100,17 @@ impl NatTable {
let port = self.next_port;

// Increment next port
if self.next_port < MAXIMUM_NAT_PORT {
self.next_port += 1;
} else if self.next_port == MAXIMUM_NAT_PORT {
if self.ip_index + 1 < self.ips.len() {
match self.next_port.cmp(&MAXIMUM_NAT_PORT) {
Ordering::Less => self.next_port += 1,
Ordering::Equal => if self.ip_index + 1 < self.ips.len() {
// We have another IP address
self.ip_index += 1;
self.next_port = MINIMUM_NAT_PORT;
} else {
// We are out of IP addresses
return None;
}
},
Ordering::Greater => unreachable!(),
}

Some (SocketAddrV4::new(ipv4, port))
Expand All @@ -128,12 +132,10 @@ impl NatTable {
}

// Get the last used port number
let port = if self.next_port > MINIMUM_NAT_PORT {
self.next_port - 1
} else if self.next_port == MINIMUM_NAT_PORT {
MAXIMUM_NAT_PORT
} else {
unreachable!()
let port = match self.next_port.cmp(&MINIMUM_NAT_PORT) {
Ordering::Less => unreachable!(),
Ordering::Equal => MAXIMUM_NAT_PORT,
Ordering::Greater => self.next_port - 1,
};

// Get the last used IP address
Expand Down Expand Up @@ -187,7 +189,7 @@ impl NatTable {
// Remove the address from the table, if it exists
let external = self.table.remove_by_left(&internal);

let socket = if let Some (_) = external {
let socket = if external.is_some() {
// Free the last used socket, returning the freed address
self.free_last_socket()
} else {
Expand Down

0 comments on commit 7b3d149

Please sign in to comment.