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