Skip to content

Commit

Permalink
rectify matching for ring buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
djellemah committed May 3, 2024
1 parent 5e141a7 commit 6bf3ea8
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,15 @@ pub mod rb {
fn send(&mut self, arg : T) -> Result<(), rtrb::PushError<T>> {
match self.0.push(arg) {
Ok(()) => Ok(()),
// TODO matching is all messed up here
err @ Err(rtrb::PushError::Full(_)) => {
// return false tells rapidjson to stop parsing
if self.0.is_abandoned() { err }
else {
// otherwise ringbuffer is full, so wait for signal from consumer
std::thread::park();
if let Err(rtrb::PushError::Full(rejected)) = err {
// recurse instead of loop - to avoid borrow and Rc in Self
self.send(rejected)
} else {
panic!("How can this not be PushError?")
}
}
Err(rtrb::PushError::Full(rejected)) if !self.0.is_abandoned() => {
// ringbuffer is full, so wait for signal from consumer
std::thread::park();
// recurse instead of loop - to avoid borrow and Rc in Self
// recursion will not be deep - because of park
self.send(rejected)
}
// ringbuffer is closed, or something else went wrong
err => err
}
}
}
Expand All @@ -44,18 +38,18 @@ pub mod rb {

impl<T> super::Consumer<T> for RbConsumer<T> {
fn recv(&mut self) -> Result<T, ()> {
while !self.0.is_abandoned() {
loop {
match self.0.pop() {
Ok(jev) => return Ok(jev),
Err(rtrb::PopError::Empty) => {
Err(rtrb::PopError::Empty) if !self.0.is_abandoned() => {
// tell the producer to carry on
self.1.unpark();
continue
},
// TODO how to hook this into an E type variable
_ => return Err(())
}
};
// real PITA to get a type constructor in here
Err(())
}
}
}

Expand Down

0 comments on commit 6bf3ea8

Please sign in to comment.