Skip to main content

qubit_io/traits/
seekable_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 super::Input;
10use super::Seekable;
11
12/// Object-safe capability trait for inputs that can be read and repositioned
13/// in the same item space.
14///
15/// `SeekableInput` exists to give the common [`Input`] + [`Seekable`]
16/// combination a stable, named trait with a single item type. It is useful for
17/// buffered inputs and other adapters that must adjust seek offsets by unread
18/// items already pulled from the wrapped input.
19///
20/// [`Input::Item`] is the shared type for both reading and seeking. It always
21/// matches [`Seekable::Unit`].
22///
23/// The trait adds no methods of its own. All operations come from the
24/// supertraits, and every type implementing both [`Input`] and [`Seekable`]
25/// with matching [`Input::Item`] and [`Seekable::Unit`] automatically
26/// implements `SeekableInput`.
27///
28/// # Examples
29///
30/// ```
31/// use std::io::Cursor;
32/// use std::io::SeekFrom;
33///
34/// use qubit_io::SeekableInput;
35///
36/// let input = Cursor::new(vec![1_u8, 2, 3]);
37/// let mut input: Box<dyn SeekableInput<Item = u8, Unit = u8>> =
38///     Box::new(input);
39/// let mut byte = [0_u8; 1];
40///
41/// assert_eq!(1, input.read(&mut byte)?);
42/// assert_eq!([1], byte);
43/// assert_eq!(0, input.seek_to(SeekFrom::Start(0))?);
44/// # Ok::<(), std::io::Error>(())
45/// ```
46pub trait SeekableInput: Input + Seekable<Unit = <Self as Input>::Item> {
47    // empty
48}
49
50impl<T> SeekableInput for T
51where
52    T: Input + Seekable<Unit = <T as Input>::Item> + ?Sized,
53{
54    // empty
55}