瀏覽代碼

Allow mutation of data buffer in RxToken

Closes: #294
Approved by: whitequark
jhwgh1968 6 年之前
父節點
當前提交
e6a6b1630f
共有 7 個文件被更改,包括 21 次插入17 次删除
  1. 1 1
      src/phy/fault_injector.rs
  2. 4 2
      src/phy/loopback.rs
  3. 5 5
      src/phy/mod.rs
  4. 1 1
      src/phy/pcap_writer.rs
  5. 6 4
      src/phy/raw_socket.rs
  6. 3 3
      src/phy/tap_interface.rs
  7. 1 1
      src/phy/tracer.rs

+ 1 - 1
src/phy/fault_injector.rs

@@ -240,7 +240,7 @@ pub struct RxToken<'a, Rx: phy::RxToken> {
 
 impl<'a, Rx: phy::RxToken> phy::RxToken for RxToken<'a, Rx> {
     fn consume<R, F>(self, timestamp: Instant, f: F) -> Result<R>
-        where F: FnOnce(&[u8]) -> Result<R>
+        where F: FnOnce(&mut [u8]) -> Result<R>
     {
         if self.state.borrow_mut().maybe(self.config.drop_pct) {
             net_trace!("rx: randomly dropping a packet");

+ 4 - 2
src/phy/loopback.rs

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

+ 5 - 5
src/phy/mod.rs

@@ -56,14 +56,14 @@ impl<'a> phy::Device<'a> for StmPhy {
     }
 }
 
-struct StmPhyRxToken<'a>(&'a [u8]);
+struct StmPhyRxToken<'a>(&'a mut [u8]);
 
 impl<'a> phy::RxToken for StmPhyRxToken<'a> {
-    fn consume<R, F>(self, _timestamp: Instant, f: F) -> Result<R>
-        where F: FnOnce(&[u8]) -> Result<R>
+    fn consume<R, F>(mut self, _timestamp: Instant, f: F) -> Result<R>
+        where F: FnOnce(&mut [u8]) -> Result<R>
     {
         // TODO: receive packet into buffer
-        let result = f(self.0);
+        let result = f(&mut self.0);
         println!("rx called");
         result
     }
@@ -248,7 +248,7 @@ pub trait RxToken {
     /// The timestamp must be a number of milliseconds, monotonically increasing since an
     /// arbitrary moment in time, such as system startup.
     fn consume<R, F>(self, timestamp: Instant, f: F) -> Result<R>
-        where F: FnOnce(&[u8]) -> Result<R>;
+        where F: FnOnce(&mut [u8]) -> Result<R>;
 }
 
 /// A token to transmit a single network packet.

+ 1 - 1
src/phy/pcap_writer.rs

@@ -168,7 +168,7 @@ pub struct RxToken<Rx: phy::RxToken, S: PcapSink> {
 }
 
 impl<Rx: phy::RxToken, S: PcapSink> phy::RxToken for RxToken<Rx, S> {
-    fn consume<R, F: FnOnce(&[u8]) -> Result<R>>(self, timestamp: Instant, f: F) -> Result<R> {
+    fn consume<R, F: FnOnce(&mut [u8]) -> Result<R>>(self, timestamp: Instant, f: F) -> Result<R> {
         let Self { token, sink, mode } = self;
         token.consume(timestamp, |buffer| {
             match mode {

+ 6 - 4
src/phy/raw_socket.rs

@@ -78,8 +78,10 @@ pub struct RxToken {
 }
 
 impl phy::RxToken for RxToken {
-    fn consume<R, F: FnOnce(&[u8]) -> Result<R>>(self, _timestamp: Instant, f: F) -> Result<R> {
-        f(&self.buffer[..])
+    fn consume<R, F>(mut self, _timestamp: Instant, f: F) -> Result<R>
+        where F: FnOnce(&mut [u8]) -> Result<R>
+    {
+        f(&mut self.buffer[..])
     }
 }
 
@@ -89,8 +91,8 @@ pub struct TxToken {
 }
 
 impl phy::TxToken for TxToken {
-    fn consume<R, F: FnOnce(&mut [u8]) -> Result<R>>(self, _timestamp: Instant, len: usize, f: F)
-        -> Result<R>
+    fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R>
+        where F: FnOnce(&mut [u8]) -> Result<R>
     {
         let mut lower = self.lower.borrow_mut();
         let mut buffer = vec![0; len];

+ 3 - 3
src/phy/tap_interface.rs

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

+ 1 - 1
src/phy/tracer.rs

@@ -74,7 +74,7 @@ pub struct RxToken<Rx: phy::RxToken, P: PrettyPrint> {
 
 impl<Rx: phy::RxToken, P: PrettyPrint> phy::RxToken for RxToken<Rx, P> {
     fn consume<R, F>(self, timestamp: Instant, f: F) -> Result<R>
-        where F: FnOnce(&[u8]) -> Result<R>
+        where F: FnOnce(&mut [u8]) -> Result<R>
     {
         let Self { token, writer } = self;
         token.consume(timestamp, |buffer| {