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::{
32/// Cursor,
33/// SeekFrom,
34/// };
35///
36/// use qubit_io::SeekableInput;
37///
38/// let input = Cursor::new(vec![1_u8, 2, 3]);
39/// let mut input: Box<dyn SeekableInput<Item = u8, Unit = u8>> =
40/// Box::new(input);
41/// let mut byte = [0_u8; 1];
42///
43/// assert_eq!(1, input.read(&mut byte)?);
44/// assert_eq!([1], byte);
45/// assert_eq!(0, input.seek_to(SeekFrom::Start(0))?);
46/// # Ok::<(), std::io::Error>(())
47/// ```
48pub trait SeekableInput: Input + Seekable<Unit = <Self as Input>::Item> {}
49
50impl<T> SeekableInput for T where T: Input + Seekable<Unit = <T as Input>::Item> {}