Skip to content

Commit

Permalink
Merge pull request #924 from lmbollen/rx-token-consume-mutability
Browse files Browse the repository at this point in the history
Change mutability of `RxToken`'s `consume` argument.
  • Loading branch information
whitequark authored Aug 9, 2024
2 parents 4ed4f2a + 4055a19 commit 30966f3
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/phy/fault_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 7 additions & 3 deletions src/phy/fuzz_injector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
extern crate alloc;
use alloc::vec::Vec;

use crate::phy::{self, Device, DeviceCapabilities};
use crate::time::Instant;

Expand Down Expand Up @@ -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)
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/phy/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/phy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/phy/pcap_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/phy/raw_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..])
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/phy/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down
6 changes: 3 additions & 3 deletions src/phy/tuntap_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..])
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 30966f3

Please sign in to comment.