qubit_io/traits/seekable_output.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 Output,
11 Seekable,
12};
13
14/// Object-safe capability trait for outputs that can be written and
15/// repositioned in the same item space.
16///
17/// `SeekableOutput` exists to give the common [`Output`] + [`Seekable`]
18/// combination a stable, named trait with a single item type. It is useful for
19/// buffered outputs and other adapters that must coordinate pending items when
20/// seeking the wrapped output.
21///
22/// [`Output::Item`] is the shared type for both writing and seeking. It
23/// always 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 [`Output`] and [`Seekable`]
27/// with matching [`Output::Item`] and [`Seekable::Unit`] automatically
28/// implements `SeekableOutput`.
29///
30/// # Examples
31///
32/// ```
33/// use std::io::{
34/// Cursor,
35/// SeekFrom,
36/// };
37///
38/// use qubit_io::SeekableOutput;
39///
40/// let output = Cursor::new(Vec::<u8>::new());
41/// let mut output: Box<dyn SeekableOutput<Item = u8, Unit = u8>> =
42/// Box::new(output);
43///
44/// assert_eq!(1, output.write(&[1_u8])?);
45/// assert_eq!(0, output.seek_to(SeekFrom::Start(0))?);
46/// # Ok::<(), std::io::Error>(())
47/// ```
48pub trait SeekableOutput:
49 Output + Seekable<Unit = <Self as Output>::Item>
50{
51}
52
53impl<T> SeekableOutput for T where
54 T: Output + Seekable<Unit = <T as Output>::Item>
55{
56}