Skip to content

Commit

Permalink
Change mutability of RxToken's consume argument.
Browse files Browse the repository at this point in the history
This drops the requirement that we can mutate the receive buffer, which is not always te case.
  • Loading branch information
lmbollen committed Apr 24, 2024
1 parent 125773e commit 6fbd46c
Show file tree
Hide file tree
Showing 9 changed files with 25 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
9 changes: 6 additions & 3 deletions src/phy/fuzz_injector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::phy::{self, Device, DeviceCapabilities};
use crate::time::Instant;

extern crate alloc;
use alloc::vec::Vec;
// This could be fixed once associated consts are stable.
const MTU: usize = 1536;

Expand Down Expand Up @@ -92,11 +94,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 @@ -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)
}
}

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 6fbd46c

Please sign in to comment.