Browse Source

Use iterators instead of manually looping

These were flagged by `cargo clippy`:

    warning: the loop variable is used to index

I've verified that this doesn't increase the size of consuming binaries.
Pretty impressive. I tested this with a project that uses the following
features: ethernet, proto-dhcpv4, socket-tcp.
Alex Crawford 4 years ago
parent
commit
902817e2aa
3 changed files with 12 additions and 17 deletions
  1. 4 4
      src/parsers.rs
  2. 4 7
      src/wire/dhcpv4.rs
  3. 4 6
      src/wire/ipv6.rs

+ 4 - 4
src/parsers.rs

@@ -121,8 +121,8 @@ impl<'a> Parser<'a> {
     #[cfg(feature = "ethernet")]
     fn accept_mac_joined_with(&mut self, separator: u8) -> Result<EthernetAddress> {
         let mut octets = [0u8; 6];
-        for n in 0..6 {
-            octets[n] = self.accept_number(2, 0x100, true)? as u8;
+        for (n, octet) in octets.iter_mut().enumerate() {
+            *octet = self.accept_number(2, 0x100, true)? as u8;
             if n != 5 {
                 self.accept_char(separator)?;
             }
@@ -270,8 +270,8 @@ impl<'a> Parser<'a> {
 
     fn accept_ipv4_octets(&mut self) -> Result<[u8; 4]> {
         let mut octets = [0u8; 4];
-        for n in 0..4 {
-            octets[n] = self.accept_number(3, 0x100, false)? as u8;
+        for (n, octet) in octets.iter_mut().enumerate() {
+            *octet = self.accept_number(3, 0x100, false)? as u8;
             if n != 3 {
                 self.accept_char(b'.')?;
             }

+ 4 - 7
src/wire/dhcpv4.rs

@@ -759,14 +759,11 @@ impl<'a> Repr<'a> {
                     parameter_request_list = Some(data);
                 }
                 DhcpOption::Other {kind: field::OPT_DOMAIN_NAME_SERVER, data} => {
-                    let mut dns_servers_inner = [None; 3];
-                    for i in 0..3 {
-                        let offset = 4 * i;
-                        let end = offset + 4;
-                        if end > data.len() { break }
-                        dns_servers_inner[i] = Some(Ipv4Address::from_bytes(&data[offset..end]));
+                    let mut servers = [None; 3];
+                    for (server, chunk) in servers.iter_mut().zip(data.chunks(4)) {
+                        *server = Some(Ipv4Address::from_bytes(chunk));
                     }
-                    dns_servers = Some(dns_servers_inner);
+                    dns_servers = Some(servers);
                 }
                 DhcpOption::Other {..} => {}
             }

+ 4 - 6
src/wire/ipv6.rs

@@ -78,9 +78,8 @@ impl Address {
     pub fn from_parts(data: &[u16]) -> Address {
         assert!(data.len() >= 8);
         let mut bytes = [0; 16];
-        for word_idx in 0..8 {
-            let byte_idx = word_idx * 2;
-            NetworkEndian::write_u16(&mut bytes[byte_idx..(byte_idx + 2)], data[word_idx]);
+        for (word_idx, chunk) in bytes.chunks_mut(2).enumerate() {
+            NetworkEndian::write_u16(chunk, data[word_idx]);
         }
         Address(bytes)
     }
@@ -91,9 +90,8 @@ impl Address {
     /// The function panics if `data` is not 8 words long.
     pub fn write_parts(&self, data: &mut [u16]) {
         assert!(data.len() >= 8);
-        for i in 0..8 {
-            let byte_idx = i * 2;
-            data[i] = NetworkEndian::read_u16(&self.0[byte_idx..(byte_idx + 2)]);
+        for (i, chunk) in self.0.chunks(2).enumerate() {
+            data[i] = NetworkEndian::read_u16(chunk);
         }
     }