Skip to main content

qubit_io/traits/
seekable_byte_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 crate::ByteOutput;
10use crate::SeekableOutput;
11
12/// A synchronous output that transfers and seeks in bytes.
13///
14/// This trait adds no methods. Every [`SeekableOutput`] with both
15/// `Item = u8` and `Unit = u8`, including unsized streams, implements it
16/// automatically. Implement the underlying output and seek traits instead.
17///
18/// # Examples
19///
20/// ```
21/// use std::io::{Result, SeekFrom};
22/// use qubit_io::SeekableByteOutput;
23///
24/// fn rewind(stream: &mut dyn SeekableByteOutput) -> Result<u64> {
25///     stream.seek_to(SeekFrom::Start(0))
26/// }
27/// ```
28///
29/// Streams with a different item type do not satisfy this bound:
30///
31/// ```compile_fail
32/// use qubit_io::{SeekableByteOutput, SeekableOutput};
33///
34/// fn accept(stream: impl SeekableByteOutput) {}
35///
36/// fn reject(stream: impl SeekableOutput<Item = char, Unit = char>) {
37///     accept(stream);
38/// }
39/// ```
40///
41/// The seek unit must also match the transferred items:
42///
43/// ```compile_fail
44/// use qubit_io::{Output, Seekable, SeekableByteOutput};
45///
46/// fn accept(stream: impl SeekableByteOutput) {}
47///
48/// fn reject(stream: impl Output<Item = u8> + Seekable<Unit = char>) {
49///     accept(stream);
50/// }
51/// ```
52pub trait SeekableByteOutput: ByteOutput + SeekableOutput<Item = u8, Unit = u8> {}
53
54impl<T: SeekableOutput<Item = u8, Unit = u8> + ?Sized> SeekableByteOutput for T {}