broker_tokio/io/
async_buf_read.rs

1use crate::io::AsyncRead;
2
3use std::io;
4use std::ops::DerefMut;
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8/// Read bytes asynchronously.
9///
10/// This trait inherits from [`std::io::BufRead`] and indicates that an I/O object is
11/// **non-blocking**. All non-blocking I/O objects must return an error when
12/// bytes are unavailable instead of blocking the current thread.
13///
14/// Utilities for working with `AsyncBufRead` values are provided by
15/// [`AsyncBufReadExt`].
16///
17/// [`std::io::BufRead`]: std::io::BufRead
18/// [`AsyncBufReadExt`]: crate::io::AsyncBufReadExt
19pub trait AsyncBufRead: AsyncRead {
20    /// Attempt to return the contents of the internal buffer, filling it with more data
21    /// from the inner reader if it is empty.
22    ///
23    /// On success, returns `Poll::Ready(Ok(buf))`.
24    ///
25    /// If no data is available for reading, the method returns
26    /// `Poll::Pending` and arranges for the current task (via
27    /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes
28    /// readable or is closed.
29    ///
30    /// This function is a lower-level call. It needs to be paired with the
31    /// [`consume`] method to function properly. When calling this
32    /// method, none of the contents will be "read" in the sense that later
33    /// calling [`poll_read`] may return the same contents. As such, [`consume`] must
34    /// be called with the number of bytes that are consumed from this buffer to
35    /// ensure that the bytes are never returned twice.
36    ///
37    /// An empty buffer returned indicates that the stream has reached EOF.
38    ///
39    /// [`poll_read`]: AsyncRead::poll_read
40    /// [`consume`]: AsyncBufRead::consume
41    fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>>;
42
43    /// Tells this buffer that `amt` bytes have been consumed from the buffer,
44    /// so they should no longer be returned in calls to [`poll_read`].
45    ///
46    /// This function is a lower-level call. It needs to be paired with the
47    /// [`poll_fill_buf`] method to function properly. This function does
48    /// not perform any I/O, it simply informs this object that some amount of
49    /// its buffer, returned from [`poll_fill_buf`], has been consumed and should
50    /// no longer be returned. As such, this function may do odd things if
51    /// [`poll_fill_buf`] isn't called before calling it.
52    ///
53    /// The `amt` must be `<=` the number of bytes in the buffer returned by
54    /// [`poll_fill_buf`].
55    ///
56    /// [`poll_read`]: AsyncRead::poll_read
57    /// [`poll_fill_buf`]: AsyncBufRead::poll_fill_buf
58    fn consume(self: Pin<&mut Self>, amt: usize);
59}
60
61macro_rules! deref_async_buf_read {
62    () => {
63        fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>)
64            -> Poll<io::Result<&[u8]>>
65        {
66            Pin::new(&mut **self.get_mut()).poll_fill_buf(cx)
67        }
68
69        fn consume(mut self: Pin<&mut Self>, amt: usize) {
70            Pin::new(&mut **self).consume(amt)
71        }
72    }
73}
74
75impl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for Box<T> {
76    deref_async_buf_read!();
77}
78
79impl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for &mut T {
80    deref_async_buf_read!();
81}
82
83impl<P> AsyncBufRead for Pin<P>
84where
85    P: DerefMut + Unpin,
86    P::Target: AsyncBufRead,
87{
88    fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
89        self.get_mut().as_mut().poll_fill_buf(cx)
90    }
91
92    fn consume(self: Pin<&mut Self>, amt: usize) {
93        self.get_mut().as_mut().consume(amt)
94    }
95}
96
97impl AsyncBufRead for &[u8] {
98    fn poll_fill_buf(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
99        Poll::Ready(Ok(*self))
100    }
101
102    fn consume(mut self: Pin<&mut Self>, amt: usize) {
103        *self = &self[amt..];
104    }
105}
106
107impl<T: AsRef<[u8]> + Unpin> AsyncBufRead for io::Cursor<T> {
108    fn poll_fill_buf(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
109        Poll::Ready(io::BufRead::fill_buf(self.get_mut()))
110    }
111
112    fn consume(self: Pin<&mut Self>, amt: usize) {
113        io::BufRead::consume(self.get_mut(), amt)
114    }
115}