pub struct ChainReader<R, F>{ /* private fields */ }Expand description
A sequential chaining reader that combines multiple Read instances with configurable error handling.
§Behavior
- Readers are consumed in FIFO (first-in, first-out) order.
- Automatically switches to the next reader when the current one reaches EOF (returns
Ok(0)). - Allows custom error handling to decide whether to retry or skip on I/O errors.
§Comparison with io::Chain
- Supports a dynamic queue of readers instead of a fixed pair.
- Configurable error handling strategies: Retry or Skip
- Automatically advances to the next reader on EOF.
顺序管道读取器,将多个 Read 实例组合为具有可配置错误处理的顺序读取管道。
§行为特性:
- 按先进先出顺序消费读取器。
- 当前读取器到达 EOF 时(返回
Ok(0))自动切换至下一个。 - 允许自定义错误处理策略,决定在 I/O 错误时重试或跳过。
§与 io::Chain 的区别:
- 支持动态读取器队列而非固定两个。
- 可配置错误处理策略:重试或跳过。
- EOF 时自动管理读取器切换。
Implementations§
Source§impl<R, F> ChainReader<R, F>
impl<R, F> ChainReader<R, F>
Sourcepub fn new(readers: VecDeque<R>, handle: F) -> Self
pub fn new(readers: VecDeque<R>, handle: F) -> Self
Creates a new ChainReader with the given reader queue and error handler.
The readers will be consumed in the order they appear in the readers queue.
The handle callback determines behavior on I/O errors during reading.
§Parameters
readers: Queue of readers to process sequentiallyhandle: Error handling strategy callback
§Examples
use std::collections::VecDeque;
use std::io;
use chain_reader::{ChainReader, ErrorAction};
let mut readers = VecDeque::new();
readers.push_back("hello".as_bytes());
let error_handler = |e: io::Error| ErrorAction::Skip;
let chain = ChainReader::new(readers, error_handler);使用指定的读取器队列和错误处理回调创建新的 ChainReader
读取器将按照队列中的顺序被消费。handle 回调用于决定读取时遇到 I/O 错误的处理策略
§参数
readers: 要顺序处理的读取器队列handle: 错误处理策略回调
§示例
use std::collections::VecDeque;
use std::io;
use chain_reader::{ChainReader, ErrorAction};
let mut readers = VecDeque::new();
readers.push_back("hello".as_bytes());
let error_handler = |e: io::Error| ErrorAction::Skip;
let chain = ChainReader::new(readers, error_handler);Sourcepub fn push_back(&mut self, reader: R)
pub fn push_back(&mut self, reader: R)
Appends a reader to the end of the processing queue
The reader will be processed after all existing readers in the queue have either reached EOF or been skipped due to errors.
§Parameters
reader: Reader to add to the end of the queue
§Examples
use std::collections::VecDeque;
use std::io;
use chain_reader::{ChainReader, ErrorAction};
let mut chain = ChainReader::new(VecDeque::new(), |e| ErrorAction::Skip);
chain.push_back(std::io::empty());将读取器追加到处理队列末尾
该读取器将在队列中所有现有读取器处理完毕(到达 EOF 或因为错误被跳过)后进行处理
§参数
reader: 要添加到队列末尾的读取器
§示例
use std::collections::VecDeque;
use std::io;
use chain_reader::{ChainReader, ErrorAction};
let mut chain = ChainReader::new(VecDeque::new(), |e| ErrorAction::Skip);
chain.push_back(std::io::empty());Trait Implementations§
Source§impl<R, F> Clone for ChainReader<R, F>
impl<R, F> Clone for ChainReader<R, F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<R, F> Debug for ChainReader<R, F>
impl<R, F> Debug for ChainReader<R, F>
Source§impl<R, F> Read for ChainReader<R, F>
impl<R, F> Read for ChainReader<R, F>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning
how many bytes were read. Read more
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Reads all bytes until EOF in this source, placing them into
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Reads all bytes until EOF in this source, appending them to
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Reads the exact number of bytes required to fill
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
read_buf)Pull some bytes from this source into the specified buffer. Read more
Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
read_buf)Reads the exact number of bytes required to fill
cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Creates a “by reference” adaptor for this instance of
Read. Read moreAuto Trait Implementations§
impl<R, F> Freeze for ChainReader<R, F>where
F: Freeze,
impl<R, F> RefUnwindSafe for ChainReader<R, F>where
F: RefUnwindSafe,
R: RefUnwindSafe,
impl<R, F> Send for ChainReader<R, F>
impl<R, F> Sync for ChainReader<R, F>
impl<R, F> Unpin for ChainReader<R, F>
impl<R, F> UnwindSafe for ChainReader<R, F>where
F: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more