Skip to main content

cobs_io_async/
transport.rs

1use core::task::{Context, Poll};
2
3/// A poll-based, runtime-independent byte-stream interface.
4///
5/// Implement this for the link underneath a [`Reliable`](crate::Reliable)
6/// connection. [`Reliable`](crate::Reliable) implements it too, so the
7/// reliable stream can be used wherever a byte stream is expected.
8///
9/// Each method follows the usual poll contract: when it returns
10/// [`Poll::Pending`], it has arranged for `cx.waker()` to be woken when the
11/// call could make progress. Methods take `&mut self` and impose no `Send`,
12/// `'static` or pinning requirements; runtime adapters handle those
13/// themselves.
14pub trait Transport {
15    /// The error produced by the underlying link.
16    type Error;
17
18    /// Attempts to read bytes into `buf`, returning the number read.
19    ///
20    /// For a nonempty `buf`, `Ready(Ok(0))` means end of stream. Temporary
21    /// lack of input must be reported as `Pending`.
22    fn poll_read(
23        &mut self,
24        cx: &mut Context<'_>,
25        buf: &mut [u8],
26    ) -> Poll<Result<usize, Self::Error>>;
27
28    /// Attempts to write a prefix of `buf`, returning the number of bytes
29    /// accepted.
30    ///
31    /// Accepted bytes need not have been transmitted yet; use
32    /// [`poll_flush`](Self::poll_flush) for that. For a nonempty `buf`,
33    /// `Ready(Ok(0))` is treated as an error by [`Reliable`](crate::Reliable).
34    fn poll_write(&mut self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, Self::Error>>;
35
36    /// Attempts to push all previously accepted bytes to the peer.
37    ///
38    /// Completes with `Ready(Ok(()))` once nothing remains buffered.
39    fn poll_flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
40}