qubit_io/traits/write_seek.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use std::io::{
11 Seek,
12 Write,
13};
14
15/// Object-safe capability trait for values that can be written and 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::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
47impl<T> WriteSeek for T where T: Write + Seek + ?Sized {}