use crate::prelude::*; use std::sync::atomic::{AtomicBool, Ordering}; pub struct InterruptibleStream { inner: Box + Send + Sync>, interrupt_signal: Arc, } impl InterruptibleStream { pub fn new(inner: S, interrupt_signal: Arc) -> InterruptibleStream where S: Iterator + Send + Sync + 'static, { InterruptibleStream { inner: Box::new(inner), interrupt_signal, } } } impl Iterator for InterruptibleStream { type Item = V; fn next(&mut self) -> Option { if self.interrupt_signal.load(Ordering::SeqCst) { None } else { self.inner.next() } } } pub trait Interruptible { fn interruptible(self, ctrl_c: Arc) -> InterruptibleStream; } impl Interruptible for S where S: Iterator + Send + Sync + 'static, { fn interruptible(self, ctrl_c: Arc) -> InterruptibleStream { InterruptibleStream::new(self, ctrl_c) } }