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