Struct ChainReader

Source
pub struct ChainReader<R, F>
where R: Read, F: FnMut(Error) -> ErrorAction,
{ /* 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>
where R: Read, F: FnMut(Error) -> ErrorAction,

Source

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 sequentially
  • handle: 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);
Source

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>
where R: Read + Clone, F: FnMut(Error) -> ErrorAction + Clone,

Source§

fn clone(&self) -> ChainReader<R, F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R, F> Debug for ChainReader<R, F>
where R: Read + Debug, F: FnMut(Error) -> ErrorAction + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R, F> Read for ChainReader<R, F>
where R: Read, F: FnMut(Error) -> ErrorAction,

Source§

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>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

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 more
1.0.0 · Source§

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 more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

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>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementations§

§

impl<R, F> Freeze for ChainReader<R, F>
where F: Freeze,

§

impl<R, F> RefUnwindSafe for ChainReader<R, F>

§

impl<R, F> Send for ChainReader<R, F>
where F: Send, R: Send,

§

impl<R, F> Sync for ChainReader<R, F>
where F: Sync, R: Sync,

§

impl<R, F> Unpin for ChainReader<R, F>
where F: Unpin, R: Unpin,

§

impl<R, F> UnwindSafe for ChainReader<R, F>
where F: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.