فهرست منبع

Change mutability of `RxToken`'s `consume` argument.

This drops the requirement that we can mutate the receive buffer, which is not always te case.
Lucas Bollen 11 ماه پیش
والد
کامیت
4055a19bc3
9فایلهای تغییر یافته به همراه26 افزوده شده و 22 حذف شده
  1. 1 1
      src/phy/fault_injector.rs
  2. 7 3
      src/phy/fuzz_injector.rs
  3. 3 3
      src/phy/loopback.rs
  4. 4 4
      src/phy/mod.rs
  5. 1 1
      src/phy/pcap_writer.rs
  6. 3 3
      src/phy/raw_socket.rs
  7. 1 1
      src/phy/tracer.rs
  8. 3 3
      src/phy/tuntap_interface.rs
  9. 3 3
      src/tests.rs

+ 1 - 1
src/phy/fault_injector.rs

@@ -274,7 +274,7 @@ pub struct RxToken<'a> {
 impl<'a> phy::RxToken for RxToken<'a> {
     fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
         f(self.buf)
     }

+ 7 - 3
src/phy/fuzz_injector.rs

@@ -1,3 +1,6 @@
+extern crate alloc;
+use alloc::vec::Vec;
+
 use crate::phy::{self, Device, DeviceCapabilities};
 use crate::time::Instant;
 
@@ -92,11 +95,12 @@ pub struct RxToken<'a, Rx: phy::RxToken, F: Fuzzer + 'a> {
 impl<'a, Rx: phy::RxToken, FRx: Fuzzer> phy::RxToken for RxToken<'a, Rx, FRx> {
     fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
         self.token.consume(|buffer| {
-            self.fuzzer.fuzz_packet(buffer);
-            f(buffer)
+            let mut new_buffer: Vec<u8> = buffer.to_vec();
+            self.fuzzer.fuzz_packet(&mut new_buffer);
+            f(&mut new_buffer)
         })
     }
 

+ 3 - 3
src/phy/loopback.rs

@@ -61,11 +61,11 @@ pub struct RxToken {
 }
 
 impl phy::RxToken for RxToken {
-    fn consume<R, F>(mut self, f: F) -> R
+    fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
-        f(&mut self.buffer)
+        f(&self.buffer)
     }
 }
 

+ 4 - 4
src/phy/mod.rs

@@ -62,11 +62,11 @@ impl phy::Device for StmPhy {
 struct StmPhyRxToken<'a>(&'a mut [u8]);
 
 impl<'a> phy::RxToken for StmPhyRxToken<'a> {
-    fn consume<R, F>(mut self, f: F) -> R
-        where F: FnOnce(&mut [u8]) -> R
+    fn consume<R, F>(self, f: F) -> R
+        where F: FnOnce(& [u8]) -> R
     {
         // TODO: receive packet into buffer
-        let result = f(&mut self.0);
+        let result = f(&self.0);
         println!("rx called");
         result
     }
@@ -372,7 +372,7 @@ pub trait RxToken {
     /// packet bytes as argument.
     fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R;
+        F: FnOnce(&[u8]) -> R;
 
     /// The Packet ID associated with the frame received by this [`RxToken`]
     fn meta(&self) -> PacketMeta {

+ 1 - 1
src/phy/pcap_writer.rs

@@ -219,7 +219,7 @@ pub struct RxToken<'a, Rx: phy::RxToken, S: PcapSink> {
 }
 
 impl<'a, Rx: phy::RxToken, S: PcapSink> phy::RxToken for RxToken<'a, Rx, S> {
-    fn consume<R, F: FnOnce(&mut [u8]) -> R>(self, f: F) -> R {
+    fn consume<R, F: FnOnce(&[u8]) -> R>(self, f: F) -> R {
         self.token.consume(|buffer| {
             match self.mode {
                 PcapMode::Both | PcapMode::RxOnly => self

+ 3 - 3
src/phy/raw_socket.rs

@@ -104,11 +104,11 @@ pub struct RxToken {
 }
 
 impl phy::RxToken for RxToken {
-    fn consume<R, F>(mut self, f: F) -> R
+    fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
-        f(&mut self.buffer[..])
+        f(&self.buffer[..])
     }
 }
 

+ 1 - 1
src/phy/tracer.rs

@@ -94,7 +94,7 @@ pub struct RxToken<Rx: phy::RxToken> {
 impl<Rx: phy::RxToken> phy::RxToken for RxToken<Rx> {
     fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
         self.token.consume(|buffer| {
             (self.writer)(

+ 3 - 3
src/phy/tuntap_interface.rs

@@ -93,11 +93,11 @@ pub struct RxToken {
 }
 
 impl phy::RxToken for RxToken {
-    fn consume<R, F>(mut self, f: F) -> R
+    fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
-        f(&mut self.buffer[..])
+        f(&self.buffer[..])
     }
 }
 

+ 3 - 3
src/tests.rs

@@ -120,11 +120,11 @@ pub struct RxToken {
 }
 
 impl phy::RxToken for RxToken {
-    fn consume<R, F>(mut self, f: F) -> R
+    fn consume<R, F>(self, f: F) -> R
     where
-        F: FnOnce(&mut [u8]) -> R,
+        F: FnOnce(&[u8]) -> R,
     {
-        f(&mut self.buffer)
+        f(&self.buffer)
     }
 }