pub trait BufReadExt:BufRead { // Provided methods fnbyte_lines(self) ->ByteLines<Self>ⓘwhere Self:Sized { ... } fnbyte_records(self, terminator:u8) ->ByteRecords<Self>ⓘwhere Self:Sized { ... } fnfor_byte_line<F>(&mut self, for_each_line: F) ->Result<()>where Self:Sized, F:FnMut(&[u8]) ->Result<bool> { ... } fnfor_byte_record<F>( &mut self, terminator:u8, for_each_record: F, ) ->Result<()>where Self:Sized, F:FnMut(&[u8]) ->Result<bool> { ... } fnfor_byte_line_with_terminator<F>( &mut self, for_each_line: F, ) ->Result<()>where Self:Sized, F:FnMut(&[u8]) ->Result<bool> { ... } fnfor_byte_record_with_terminator<F>( &mut self, terminator:u8, for_each_record: F, ) ->Result<()>where Self:Sized, F:FnMut(&[u8]) ->Result<bool> { ... }}std only.Expand description
An extension trait forstd::io::BufReadwhich provides convenience APIs for dealing with byte strings.
Provided Methods§
Sourcefnbyte_lines(self) ->ByteLines<Self>ⓘwhere Self:Sized,
fnbyte_lines(self) ->ByteLines<Self>ⓘwhere Self:Sized,
Returns an iterator over the lines of this reader, where each lineis represented as a byte string.
Each item yielded by this iterator is aio::Result<Vec<u8>>, wherean error is yielded if there was a problem reading from the underlyingreader.
On success, the next line in the iterator is returned. The line doesnot contain a trailing\n or\r\n.
§Examples
Basic usage:
usestd::io;usebstr::io::BufReadExt;letmutcursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");letmutlines =vec![];forresultincursor.byte_lines() {letline = result?; lines.push(line);}assert_eq!(lines.len(),3);assert_eq!(lines[0],"lorem".as_bytes());assert_eq!(lines[1],"ipsum".as_bytes());assert_eq!(lines[2],"dolor".as_bytes());Sourcefnbyte_records(self, terminator:u8) ->ByteRecords<Self>ⓘwhere Self:Sized,
fnbyte_records(self, terminator:u8) ->ByteRecords<Self>ⓘwhere Self:Sized,
Returns an iterator over byte-terminated records of this reader, whereeach record is represented as a byte string.
Each item yielded by this iterator is aio::Result<Vec<u8>>, wherean error is yielded if there was a problem reading from the underlyingreader.
On success, the next record in the iterator is returned. The recorddoesnot contain its trailing terminator.
Note that callingbyte_records(b'\n') differs frombyte_lines() inthat it has no special handling for\r.
§Examples
Basic usage:
usestd::io;usebstr::io::BufReadExt;letmutcursor = io::Cursor::new(b"lorem\x00ipsum\x00dolor");letmutrecords =vec![];forresultincursor.byte_records(b'\x00') {letrecord = result?; records.push(record);}assert_eq!(records.len(),3);assert_eq!(records[0],"lorem".as_bytes());assert_eq!(records[1],"ipsum".as_bytes());assert_eq!(records[2],"dolor".as_bytes());Sourcefnfor_byte_line<F>(&mut self, for_each_line: F) ->Result<()>
fnfor_byte_line<F>(&mut self, for_each_line: F) ->Result<()>
Executes the given closure on each line in the underlying reader.
If the closure returns an error (or if the underlying reader returns anerror), then iteration is stopped and the error is returned. If falseis returned, then iteration is stopped and no error is returned.
The closure given is called on exactly the same values as yielded bythebyte_linesiterator. Namely, lines donot contain trailing\n or\r\n bytes.
This routine is useful for iterating over lines as quickly aspossible. Namely, a single allocation is reused for each line.
§Examples
Basic usage:
usestd::io;usebstr::io::BufReadExt;letmutcursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");letmutlines =vec![];cursor.for_byte_line(|line| { lines.push(line.to_vec());Ok(true)})?;assert_eq!(lines.len(),3);assert_eq!(lines[0],"lorem".as_bytes());assert_eq!(lines[1],"ipsum".as_bytes());assert_eq!(lines[2],"dolor".as_bytes());Sourcefnfor_byte_record<F>( &mut self, terminator:u8, for_each_record: F,) ->Result<()>
fnfor_byte_record<F>( &mut self, terminator:u8, for_each_record: F,) ->Result<()>
Executes the given closure on each byte-terminated record in theunderlying reader.
If the closure returns an error (or if the underlying reader returns anerror), then iteration is stopped and the error is returned. If falseis returned, then iteration is stopped and no error is returned.
The closure given is called on exactly the same values as yielded bythebyte_recordsiterator. Namely, records donot contain a trailing terminator byte.
This routine is useful for iterating over records as quickly aspossible. Namely, a single allocation is reused for each record.
§Examples
Basic usage:
usestd::io;usebstr::io::BufReadExt;letmutcursor = io::Cursor::new(b"lorem\x00ipsum\x00dolor");letmutrecords =vec![];cursor.for_byte_record(b'\x00', |record| { records.push(record.to_vec());Ok(true)})?;assert_eq!(records.len(),3);assert_eq!(records[0],"lorem".as_bytes());assert_eq!(records[1],"ipsum".as_bytes());assert_eq!(records[2],"dolor".as_bytes());Sourcefnfor_byte_line_with_terminator<F>(&mut self, for_each_line: F) ->Result<()>
fnfor_byte_line_with_terminator<F>(&mut self, for_each_line: F) ->Result<()>
Executes the given closure on each line in the underlying reader.
If the closure returns an error (or if the underlying reader returns anerror), then iteration is stopped and the error is returned. If falseis returned, then iteration is stopped and no error is returned.
Unlikefor_byte_line,the lines given to the closuredo include the line terminator, if oneexists.
This routine is useful for iterating over lines as quickly aspossible. Namely, a single allocation is reused for each line.
This is identical tofor_byte_record_with_terminator with aterminator of\n.
§Examples
Basic usage:
usestd::io;usebstr::io::BufReadExt;letmutcursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");letmutlines =vec![];cursor.for_byte_line_with_terminator(|line| { lines.push(line.to_vec());Ok(true)})?;assert_eq!(lines.len(),3);assert_eq!(lines[0],"lorem\n".as_bytes());assert_eq!(lines[1],"ipsum\r\n".as_bytes());assert_eq!(lines[2],"dolor".as_bytes());Sourcefnfor_byte_record_with_terminator<F>( &mut self, terminator:u8, for_each_record: F,) ->Result<()>
fnfor_byte_record_with_terminator<F>( &mut self, terminator:u8, for_each_record: F,) ->Result<()>
Executes the given closure on each byte-terminated record in theunderlying reader.
If the closure returns an error (or if the underlying reader returns anerror), then iteration is stopped and the error is returned. If falseis returned, then iteration is stopped and no error is returned.
Unlikefor_byte_record,the lines given to the closuredo include the record terminator, ifone exists.
This routine is useful for iterating over records as quickly aspossible. Namely, a single allocation is reused for each record.
§Examples
Basic usage:
usestd::io;usebstr::{io::BufReadExt, B};letmutcursor = io::Cursor::new(b"lorem\x00ipsum\x00dolor");letmutrecords =vec![];cursor.for_byte_record_with_terminator(b'\x00', |record| { records.push(record.to_vec());Ok(true)})?;assert_eq!(records.len(),3);assert_eq!(records[0], B(b"lorem\x00"));assert_eq!(records[1], B("ipsum\x00"));assert_eq!(records[2], B("dolor"));