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