#stream #data-transfer #closures #data-stream #drop #future #connection

drop-stream

A stream that wraps another stream with a closure that is called once it is dropped

5 unstable releases

0.3.2 Jul 21, 2024
0.3.1 May 26, 2024
0.3.0 Nov 23, 2022
0.2.0 Nov 23, 2022
0.1.0 May 23, 2022

#203 in Asynchronous

Download history 128/week @ 2024-06-30 106/week @ 2024-07-07 104/week @ 2024-07-14 276/week @ 2024-07-21 211/week @ 2024-07-28 559/week @ 2024-08-04 361/week @ 2024-08-11 508/week @ 2024-08-18 570/week @ 2024-08-25 107/week @ 2024-09-01 127/week @ 2024-09-08 312/week @ 2024-09-15 515/week @ 2024-09-22 441/week @ 2024-09-29 444/week @ 2024-10-06 320/week @ 2024-10-13

1,770 downloads per month

MIT license

9KB
115 lines

Drop Stream

crates.io license docs CI

A stream that wraps another stream with a closure that is called once it is dropped. Very useful for libraries that use streams for data transfer and you need to connect when the opposite site drops the connection, thus dropping the stream.

This crate only depends on futures-core and thus is a minimal dependency, suitable for any type of project utilising futures.

Example

use futures::Stream;
use drop_stream::DropStream;
let test_stream = futures::stream::repeat(true);
{
    let wrapped_stream = DropStream::new(test_stream, move || {
        println!("Stream has been dropped!");
    });
    let mut wrapped_stream = Box::pin(wrapped_stream);
    let waker = futures::task::noop_waker();
    let mut context = futures::task::Context::from_waker(&waker);
    assert_eq!(
        wrapped_stream.as_mut().poll_next(&mut context),
        std::task::Poll::Ready(Some(true))
    );
}

Acknowledgement

I thank Aadam Zocolo for letting me take over the crate name "drop-stream" on crates.io and replace his 0.1 version.

Dependencies

~0.3–0.8MB
~18K SLoC