qubit_io/traits/read_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 Read,
10 Seek,
11};
12
13/// Object-safe capability trait for values that can be read and repositioned.
14///
15/// `ReadSeek` exists to give the common [`Read`] + [`Seek`] combination a
16/// stable, named trait that can be used behind `dyn`. It is useful for APIs
17/// that need to accept heterogeneous random-access inputs, such as files,
18/// cursors, archive members, or custom readers, while still calling both
19/// reading and seeking methods through the same trait object.
20///
21/// The trait adds no methods of its own. All operations come from the
22/// standard-library supertraits, and every type implementing both [`Read`] and
23/// [`Seek`] automatically implements `ReadSeek`.
24///
25/// # Examples
26///
27/// ```rust
28/// use qubit_io::ReadSeek;
29/// use std::io::SeekFrom;
30///
31/// fn read_from_start(input: &mut dyn ReadSeek) -> std::io::Result<Vec<u8>> {
32/// input.seek(SeekFrom::Start(0))?;
33///
34/// let mut bytes = Vec::new();
35/// input.read_to_end(&mut bytes)?;
36/// Ok(bytes)
37/// }
38///
39/// let mut cursor = std::io::Cursor::new(b"abc".to_vec());
40/// assert_eq!(read_from_start(&mut cursor)?, b"abc");
41/// # Ok::<(), std::io::Error>(())
42/// ```
43pub trait ReadSeek: Read + Seek {}
44
45impl<T> ReadSeek for T where T: Read + Seek + ?Sized {}