Skip to main content

moirai_async/io/
traits.rs

1use std::io;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5/// Read bytes asynchronously.
6pub trait AsyncRead {
7    /// Attempt to read from the source into the provided buffer.
8    fn poll_read(
9        self: Pin<&mut Self>,
10        cx: &mut Context<'_>,
11        buf: &mut [u8],
12    ) -> Poll<io::Result<usize>>;
13}
14
15/// Write bytes asynchronously.
16pub trait AsyncWrite {
17    /// Attempt to write bytes from the buffer to the destination.
18    fn poll_write(
19        self: Pin<&mut Self>,
20        cx: &mut Context<'_>,
21        buf: &[u8],
22    ) -> Poll<io::Result<usize>>;
23
24    /// Attempt to flush pending writes.
25    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
26
27    /// Attempt to close/shutdown the write side of the stream.
28    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
29}
30
31/// Read bytes from a buffered source asynchronously.
32pub trait AsyncBufRead: AsyncRead {
33    /// Returns the contents of the internal buffer, filling it with more data
34    /// from the inner reader if necessary.
35    fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>>;
36
37    /// Consumes the specified amount of bytes from the internal buffer.
38    fn consume(self: Pin<&mut Self>, amt: usize);
39}