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