Allow mutation of data buffer in RxToken

Closes: #294
Approved by: whitequark
This commit is contained in:
jhwgh1968 2019-05-01 22:12:33 -05:00 committed by Homu
parent a009e0ad9d
commit 9f2febcaea
7 changed files with 21 additions and 17 deletions

View File

@ -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");

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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