Change mutability of RxToken's consume argument.

This drops the requirement that we can mutate the receive buffer, which is not always te case.
This commit is contained in:
Lucas Bollen 2024-04-23 15:41:31 +02:00
parent 125773e282
commit 4055a19bc3
9 changed files with 26 additions and 22 deletions

View File

@ -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)
}

View File

@ -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)
})
}

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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

View File

@ -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[..])
}
}

View File

@ -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)(

View File

@ -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[..])
}
}

View File

@ -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)
}
}