Skip to main content

async_rs/util/
dummy.rs

1use futures_core::Stream;
2use futures_io::{AsyncRead, AsyncWrite};
3use std::{
4    io,
5    marker::PhantomData,
6    pin::Pin,
7    task::{Context, Poll},
8};
9
10/// A dummy struct implementing Async IO traits
11///
12/// Every operation is `Poll::Pending`, forever: nothing is ever read, written, flushed or closed.
13/// No waker is registered either, so a task waiting on one cannot even be woken to give up.
14#[derive(Debug)]
15pub struct DummyIO;
16
17impl AsyncRead for DummyIO {
18    fn poll_read(
19        self: Pin<&mut Self>,
20        _cx: &mut Context<'_>,
21        _buf: &mut [u8],
22    ) -> Poll<io::Result<usize>> {
23        Poll::Pending
24    }
25}
26
27impl AsyncWrite for DummyIO {
28    fn poll_write(
29        self: Pin<&mut Self>,
30        _cx: &mut Context<'_>,
31        _buf: &[u8],
32    ) -> Poll<io::Result<usize>> {
33        Poll::Pending
34    }
35
36    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
37        Poll::Pending
38    }
39
40    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
41        Poll::Pending
42    }
43}
44
45/// A dummy struct implementing Stream
46///
47/// Like [`DummyIO`], it is `Poll::Pending` forever and registers no waker: the stream neither
48/// yields an item nor ends.
49#[derive(Debug)]
50pub struct DummyStream<T>(pub PhantomData<T>);
51
52impl<T> Stream for DummyStream<T> {
53    type Item = T;
54
55    fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
56        Poll::Pending
57    }
58}