qubit_io/traits/read_write.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 Write,
11};
12
13/// Object-safe capability trait for values that can be both read and written.
14///
15/// `ReadWrite` gives the common [`Read`] + [`Write`] combination a named trait
16/// for APIs that work with duplex streams or mutable buffers through trait
17/// objects. Typical use cases include protocol handlers, in-memory transport
18/// tests, and adapters that should not care about the concrete stream type.
19///
20/// The trait adds no methods of its own. All operations come from the
21/// standard-library supertraits, and every type implementing both [`Read`] and
22/// [`Write`] automatically implements `ReadWrite`.
23///
24/// # Examples
25///
26/// ```rust
27/// use qubit_io::ReadWrite;
28///
29/// fn send_ping(stream: &mut dyn ReadWrite) -> std::io::Result<()> {
30/// stream.write_all(b"ping")
31/// }
32///
33/// let mut cursor = std::io::Cursor::new(Vec::new());
34/// send_ping(&mut cursor)?;
35/// assert_eq!(cursor.into_inner(), b"ping");
36/// # Ok::<(), std::io::Error>(())
37/// ```
38pub trait ReadWrite: Read + Write {}
39
40impl<T> ReadWrite for T where T: Read + Write + ?Sized {}