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