Skip to main content

qubit_io/std_io/traits/
write_seek.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
9//! Composite write and seek capability.
10
11use std::io::Seek;
12use std::io::Write;
13
14/// Object-safe capability trait for values that can be written and
15/// repositioned.
16///
17/// `WriteSeek` gives the common [`Write`] + [`Seek`] combination a named trait
18/// for APIs that write random-access output through trait objects. It is useful
19/// for serializers, archive writers, and binary encoders that write placeholder
20/// data first and later seek back to patch headers, offsets, or lengths.
21///
22/// The trait adds no methods of its own. All operations come from the
23/// standard-library supertraits, and every type implementing both [`Write`] and
24/// [`Seek`] automatically implements `WriteSeek`.
25///
26/// # Examples
27///
28/// ```rust
29/// use qubit_io::std_io::WriteSeek;
30/// use std::io::SeekFrom;
31///
32/// fn write_with_length(output: &mut dyn WriteSeek) -> std::io::Result<()> {
33///     output.write_all(&[0])?;
34///     output.write_all(b"data")?;
35///     output.seek(SeekFrom::Start(0))?;
36///     output.write_all(&[4])?;
37///     Ok(())
38/// }
39///
40/// let mut cursor = std::io::Cursor::new(Vec::new());
41/// write_with_length(&mut cursor)?;
42/// assert_eq!(cursor.into_inner(), b"\x04data");
43/// # Ok::<(), std::io::Error>(())
44/// ```
45pub trait WriteSeek: Write + Seek {
46    // empty
47}
48
49impl<T> WriteSeek for T where T: Write + Seek + ?Sized {}