Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change mutability of RxToken's consume argument. #924

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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