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