Skip to main content

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