Crates.io | drop-stream |
lib.rs | drop-stream |
version | 0.3.2 |
source | src |
created_at | 2022-05-23 23:51:55.948177 |
updated_at | 2024-07-21 08:36:00.482157 |
description | A stream that wraps another stream with a closure that is called once it is dropped. |
homepage | |
repository | https://github.com/DreamplaySE/drop-stream |
max_upload_size | |
id | 592299 |
size | 9,846 |
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.
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))
);
}
I thank Aadam Zocolo for letting me take over the crate name "drop-stream" on crates.io and replace his 0.1 version.