Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Add accessors to Consume #148

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions rubble/src/link/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,23 @@ pub trait Consumer {
/// Bundles a `T` along with information telling a queue whether to consume a packet.
#[derive(Debug)]
pub struct Consume<T> {
consume: bool,
should_consume: bool,
result: Result<T, Error>,
}

impl<T> Consume<T> {
/// Consume the currently processed packet iff `consume` is `true`, then return `result`.
pub fn new(consume: bool, result: Result<T, Error>) -> Self {
Self { consume, result }
Self {
should_consume: consume,
result,
}
}

/// Consume the currently processed packet, then return `result`.
pub fn always(result: Result<T, Error>) -> Self {
Self {
consume: true,
should_consume: true,
result,
}
}
Expand All @@ -177,7 +180,7 @@ impl<T> Consume<T> {
/// The next call to the `Consumer::consume_*` methods will yield the same packet again.
pub fn never(result: Result<T, Error>) -> Self {
Self {
consume: false,
should_consume: false,
result,
}
}
Expand All @@ -186,10 +189,25 @@ impl<T> Consume<T> {
/// result.
pub fn on_success(result: Result<T, Error>) -> Self {
Self {
consume: result.is_ok(),
should_consume: result.is_ok(),
result,
}
}

/// Retrieves whether the packet should be removed from the queue.
pub fn should_consume(&self) -> bool {
self.should_consume
}

/// Retrieves a reference to the inner result.
pub fn result(&self) -> &Result<T, Error> {
&self.result
}

/// Unwraps this `consume`, retrieving the inner result.
pub fn into_result(self) -> Result<T, Error> {
self.result
}
}

/// A simple packet queue that can hold a single packet.
Expand Down Expand Up @@ -286,7 +304,7 @@ impl<'a> Consumer for SimpleConsumer<'a> {
let raw_payload = bytes.read_slice(pl_len)?;

let res = f(header, raw_payload);
if res.consume {
if res.should_consume {
self.inner.dequeue().unwrap(); // can't fail
}
res.result
Expand Down