Skip to main content

qubit_io/traits/
seekable_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::ByteInput;
10use crate::SeekableInput;
11
12/// A synchronous input that transfers and seeks in bytes.
13///
14/// This trait adds no methods. Every [`SeekableInput`] with both
15/// `Item = u8` and `Unit = u8`, including unsized streams, implements it
16/// automatically. Implement the underlying input and seek traits instead.
17///
18/// # Examples
19///
20/// ```
21/// use std::io::{Result, SeekFrom};
22/// use qubit_io::SeekableByteInput;
23///
24/// fn rewind(stream: &mut dyn SeekableByteInput) -> Result<u64> {
25///     stream.seek_to(SeekFrom::Start(0))
26/// }
27/// ```
28///
29/// Streams with a different item type do not satisfy this bound:
30///
31/// ```compile_fail
32/// use qubit_io::{SeekableByteInput, SeekableInput};
33///
34/// fn accept(stream: impl SeekableByteInput) {}
35///
36/// fn reject(stream: impl SeekableInput<Item = char, Unit = char>) {
37///     accept(stream);
38/// }
39/// ```
40///
41/// The seek unit must also match the transferred items:
42///
43/// ```compile_fail
44/// use qubit_io::{Input, Seekable, SeekableByteInput};
45///
46/// fn accept(stream: impl SeekableByteInput) {}
47///
48/// fn reject(stream: impl Input<Item = u8> + Seekable<Unit = char>) {
49///     accept(stream);
50/// }
51/// ```
52pub trait SeekableByteInput: ByteInput + SeekableInput<Item = u8, Unit = u8> {}
53
54impl<T: SeekableInput<Item = u8, Unit = u8> + ?Sized> SeekableByteInput for T {}