Skip to main content

qubit_io/traits/
async_byte_input.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9use crate::AsyncInput;
10
11/// An asynchronous input whose items are bytes.
12///
13/// This trait adds no methods. Every [`AsyncInput<Item = u8>`](AsyncInput),
14/// including unsized streams, implements it automatically. Implement
15/// [`AsyncInput`] to provide the operations; no separate implementation of
16/// `AsyncByteInput` is needed.
17///
18/// The inherited polling methods retain their pinning and readiness contracts;
19/// implementors do not need to be `Unpin`.
20///
21/// # Examples
22///
23/// ```
24/// use std::io;
25/// use std::pin::Pin;
26/// use std::task::{Context, Poll};
27/// use qubit_io::AsyncByteInput;
28///
29/// fn read<S: AsyncByteInput + ?Sized>(
30///     stream: Pin<&mut S>, cx: &mut Context<'_>, output: &mut [u8],
31/// ) -> Poll<io::Result<usize>> {
32///     stream.poll_read(cx, output)
33/// }
34/// ```
35///
36/// Streams with a different item type do not satisfy this bound:
37///
38/// ```compile_fail
39/// use qubit_io::{AsyncInput, AsyncByteInput};
40///
41/// fn accept(stream: impl AsyncByteInput) {}
42///
43/// fn reject(stream: impl AsyncInput<Item = char>) {
44///     accept(stream);
45/// }
46/// ```
47pub trait AsyncByteInput: AsyncInput<Item = u8> {}
48
49impl<T: AsyncInput<Item = u8> + ?Sized> AsyncByteInput for T {}