Skip to main content

base_scheduler/
pipe_reader.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use crate::{Cancellation, OperationResult};
5use std::{
6    future::Future,
7    io::{self, IoSliceMut},
8};
9
10/// Sequential, non-positional input from an adopted pipe, bound to its creating scheduler and owning its descriptor.
11///
12/// Reads consume bytes in pipe order. Each operation performs one possibly short transfer; there is no framing or read-ahead. Borrowed storage is accessed only synchronously within each poll, so dropping or forgetting a pending future leaves no backend access to it. Ordinary errors and cancellation end the current wait without closing the reader or rolling back consumed bytes; successful progress wins concurrent cancellation.
13pub trait PipeReader: Send {
14    /// Read once into borrowed storage, returning a possibly short count. Zero with nonempty storage is end of file: every write end has been closed. An empty destination completes with zero after checking that the handle is open and cancellation has not been requested.
15    fn read_into(
16        &mut self,
17        buffer: &mut [u8],
18        cancellation: &Cancellation,
19    ) -> impl Future<Output = OperationResult<usize>> + Send;
20
21    /// Scatter a single read across borrowed ranges, with the same end-of-file and empty-storage behavior as [`read_into`](Self::read_into). Short progress does not advance the caller's slices.
22    fn read_vectored(
23        &mut self,
24        buffers: &mut [IoSliceMut<'_>],
25        cancellation: &Cancellation,
26    ) -> impl Future<Output = OperationResult<usize>> + Send;
27
28    /// Stop input, retire backend registration and close the descriptor, reporting the close result. Later operations fail with `BrokenPipe`; repeating `close` succeeds without further effect. Dropping the reader also closes the descriptor, without awaiting retirement. Unlike socket and file handles, a pipe handle owns its descriptor. After backend shutdown, `close` can report the shutdown error; the descriptor is closed regardless.
29    fn close(&mut self) -> impl Future<Output = io::Result<()>> + Send;
30}