Skip to main content

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