Skip to main content

BufReadExt

Trait BufReadExt 

Source
pub trait BufReadExt: BufRead {
    // Required methods
    fn read_until_limited(
        &mut self,
        delimiter: u8,
        max_len: usize,
    ) -> Result<Vec<u8>>;
    fn read_until_limited_into(
        &mut self,
        delimiter: u8,
        output: &mut Vec<u8>,
        max_len: usize,
    ) -> Result<usize>;
    fn read_line_limited(&mut self, max_len: usize) -> Result<String>;
    fn read_line_limited_into(
        &mut self,
        output: &mut String,
        max_len: usize,
    ) -> Result<usize>;
    fn discard_until_limited(
        &mut self,
        delimiter: u8,
        max_len: usize,
    ) -> Result<usize>;
}
Expand description

Extension methods for BufRead values.

BufReadExt provides bounded delimiter-oriented reads. These helpers are useful for line-based and delimiter-based formats where accepting unbounded input would make parsers vulnerable to excessive memory use.

Required Methods§

Source

fn read_until_limited( &mut self, delimiter: u8, max_len: usize, ) -> Result<Vec<u8>>

Reads bytes through delimiter while enforcing max_len.

The returned vector includes the delimiter when it is found. EOF before the delimiter is accepted as long as the accumulated bytes do not exceed max_len. If the limit is exceeded, this method may consume the accepted prefix before reporting the error.

§Parameters
  • delimiter: Delimiter byte to search for.
  • max_len: Maximum accepted result length, including the delimiter.
§Returns

Bytes read from the stream.

§Errors

Returns ErrorKind::InvalidData when more than max_len bytes are required before reaching delimiter or EOF. Returns the first I/O error reported by the underlying reader.

Source

fn read_until_limited_into( &mut self, delimiter: u8, output: &mut Vec<u8>, max_len: usize, ) -> Result<usize>

Reads bytes through delimiter into output while enforcing max_len.

This method appends at most max_len bytes from the current reader position to output. The delimiter is included when it is found. If the limit is exceeded, the accepted prefix may already have been appended to output and consumed from the reader.

§Parameters
  • delimiter: Delimiter byte to search for.
  • output: Destination vector to append to.
  • max_len: Maximum accepted result length, including the delimiter.
§Returns

Number of bytes appended to output.

§Errors

Returns ErrorKind::InvalidData when more than max_len bytes are required before reaching delimiter or EOF. Returns the first I/O error reported by the underlying reader.

Source

fn read_line_limited(&mut self, max_len: usize) -> Result<String>

Reads one UTF-8 line while enforcing max_len.

The returned string includes the trailing \n when it is present. EOF before a newline is accepted as long as the accumulated bytes do not exceed max_len.

§Parameters
  • max_len: Maximum accepted line length in bytes, including \n.
§Returns

The decoded UTF-8 line.

§Errors

Returns ErrorKind::InvalidData when the line exceeds max_len or is not valid UTF-8. Returns the first I/O error reported by the underlying reader.

Source

fn read_line_limited_into( &mut self, output: &mut String, max_len: usize, ) -> Result<usize>

Reads one UTF-8 line into output while enforcing max_len.

This method reads at most max_len bytes, validates the line as UTF-8, and appends it to output. If the line is oversized or invalid UTF-8, output is left unchanged. Oversized input may still consume the accepted prefix from the reader while detecting the limit violation.

§Parameters
  • output: Destination string to append to.
  • max_len: Maximum accepted line length in bytes, including \n.
§Returns

Number of bytes appended to output.

§Errors

Returns ErrorKind::InvalidData when the line exceeds max_len or is not valid UTF-8. Returns the first I/O error reported by the underlying reader.

Source

fn discard_until_limited( &mut self, delimiter: u8, max_len: usize, ) -> Result<usize>

Discards bytes through delimiter while enforcing max_len.

The delimiter is consumed when it is found. EOF before the delimiter is accepted as long as no more than max_len bytes are consumed.

§Parameters
  • delimiter: Delimiter byte to search for.
  • max_len: Maximum number of bytes to discard, including the delimiter.
§Returns

Number of bytes discarded.

§Errors

Returns ErrorKind::InvalidData when more than max_len bytes are required before reaching delimiter or EOF. Returns the first I/O error reported by the underlying reader.

Implementors§

Source§

impl<T> BufReadExt for T
where T: BufRead + ?Sized,