Introduce InterruptibleStream type.

An interruptible stream can query an `AtomicBool. If that bool is true,
the stream will no longer produce any values.

Also introducing the `Interruptible` trait, which extends any `Stream`
with the `interruptible` function, to simplify the construction and
allow chaining.
This commit is contained in:
Jason Gedge
2020-03-29 10:55:54 -04:00
committed by Jason Gedge
parent cad2741e9e
commit 2a54ee0c54
3 changed files with 52 additions and 1 deletions

View File

@ -0,0 +1,35 @@
use crate::prelude::*;
use futures::task::Poll;
use std::sync::atomic::{AtomicBool, Ordering};
pub struct InterruptibleStream<V> {
inner: BoxStream<'static, V>,
ctrl_c: Arc<AtomicBool>,
}
impl<V> InterruptibleStream<V> {
pub fn new<S>(inner: S, ctrl_c: Arc<AtomicBool>) -> InterruptibleStream<V>
where
S: Stream<Item = V> + Send + 'static,
{
InterruptibleStream {
inner: inner.boxed(),
ctrl_c,
}
}
}
impl<V> Stream for InterruptibleStream<V> {
type Item = V;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> core::task::Poll<Option<Self::Item>> {
if self.ctrl_c.load(Ordering::SeqCst) {
Poll::Ready(None)
} else {
Stream::poll_next(std::pin::Pin::new(&mut self.inner), cx)
}
}
}

View File

@ -1,5 +1,7 @@
mod input;
mod interruptible;
mod output;
pub use input::*;
pub use interruptible::*;
pub use output::*;