mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:55:07 +02:00
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:
35
crates/nu-cli/src/stream/interruptible.rs
Normal file
35
crates/nu-cli/src/stream/interruptible.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
mod input;
|
||||
mod interruptible;
|
||||
mod output;
|
||||
|
||||
pub use input::*;
|
||||
pub use interruptible::*;
|
||||
pub use output::*;
|
||||
|
Reference in New Issue
Block a user