Function interrupt_when

Source
pub fn interrupt_when<O, F>(s: RS2Stream<O>, signal: F) -> RS2Stream<O>
where O: Send + 'static, F: Future<Output = ()> + Send + 'static,
Expand description

Interrupt a rs2_stream when a signal is received

This combinator takes a rs2_stream and a future that signals interruption. It stops processing the rs2_stream when the signal future completes. Resources are properly cleaned up when the rs2_stream is interrupted.

ยงExamples

use rs2_stream::rs2::*;
use std::time::Duration;
use tokio::time::sleep;
use async_stream::stream;
use futures_util::stream::StreamExt;

// Create a rs2_stream that emits numbers every 100ms
let rs2_stream = from_iter(0..100)
    .throttle_rs2(Duration::from_millis(100));

// Create a future that completes after 250ms
let interrupt_signal = sleep(Duration::from_millis(250));

// The rs2_stream will be interrupted after about 250ms,
// so we should get approximately 2-3 items
let result = interrupt_when(rs2_stream, interrupt_signal)
    .collect::<Vec<_>>()
    .await;

assert!(result.len() >= 2 && result.len() <= 3);