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/// A byte cursor supports seeking through the trait object interface:
20///
21/// ```
22/// use std::io::{Cursor, Result, SeekFrom};
23/// use qubit_io::SeekableByteInput;
24/// fn rewind(stream: &mut dyn SeekableByteInput) -> Result<u64> { stream.seek_to(SeekFrom::Start(0)) }
25/// let mut cursor = Cursor::new(b"QBIT");
26/// let stream: &mut dyn SeekableByteInput = &mut cursor;
27/// assert_eq!(stream.seek_to(SeekFrom::Start(3))?, 3);
28/// assert_eq!(rewind(stream)?, 0);
29/// let mut byte = [0];
30/// assert_eq!(stream.read(&mut byte)?, 1);
31/// assert_eq!(&byte, b"Q");
32/// assert_eq!(stream.seek_to(SeekFrom::Start(1))?, 1);
33/// assert_eq!(stream.read(&mut byte)?, 1);
34/// assert_eq!(&byte, b"B");
35/// # Ok::<(), std::io::Error>(())
36/// ```
37/// Streams with a different item type do not satisfy this bound:
38///
39/// ```compile_fail
40/// use qubit_io::{SeekableByteInput, SeekableInput};
41///
42/// fn accept(stream: impl SeekableByteInput) {}
43///
44/// fn reject(stream: impl SeekableInput<Item = char, Unit = char>) {
45/// accept(stream);
46/// }
47/// ```
48///
49/// The seek unit must also match the transferred items:
50///
51/// ```compile_fail
52/// use qubit_io::{Input, Seekable, SeekableByteInput};
53///
54/// fn accept(stream: impl SeekableByteInput) {}
55///
56/// fn reject(stream: impl Input<Item = u8> + Seekable<Unit = char>) {
57/// accept(stream);
58/// }
59/// ```
60pub trait SeekableByteInput: ByteInput + SeekableInput<Item = u8, Unit = u8> {
61 // empty
62}
63
64impl<T: SeekableInput<Item = u8, Unit = u8> + ?Sized> SeekableByteInput for T {}