CharReader

Struct CharReader 

Source
pub struct CharReader<R: Read> { /* private fields */ }
Expand description

A buffered reader able to read chars or lines without crashing when the stream doesn’t finish and/or doesn’t contain newlines.

It’s also able to avoid storying whole lines if you’re only interested in their beginning.

Bad UTF8 is reported as io::Error with kind InvalidData.

Implementations§

Source§

impl<R: Read> CharReader<R>

Source

pub fn new(src: R) -> Self

Source

pub fn load_char(&mut self) -> Result<Option<(char, usize)>>

ensure there’s at least one char in the buffer, and returns it with its size in bytes (or None if the underlying stream is finished).

You probably don’t need this function but next_char.

Source

pub fn next_char(&mut self) -> Result<Option<char>>

read and return the next char, or NONE in case of EOF

Source

pub fn peek_char(&mut self) -> Result<Option<char>>

return the next char, but doesn’t advance the cursor

Source

pub fn read_line( &mut self, line: &mut String, drop_after: usize, fail_after: usize, ) -> Result<bool>

append the next line, if any, but with some protection against wild stream content:

  • don’t store chars after the drop_after threshold
  • throw an error after the fail_after threshold

Thresholds are in chars, not bytes nor cols nor graphemes. Only difference with next_line is that you pass (and may reuse) the string to fill.

Return Ok(false) when there was no error but nothing to read (stream finished or paused).

This function may return Ok(true) and not have written anything: it means there was an empty line (i.e. next char will be a CR or LF)

Source

pub fn next_line( &mut self, drop_after: usize, fail_after: usize, ) -> Result<Option<String>>

return the next line, if any, but with some protection against wild stream content:

  • don’t store chars after the drop_after threshold
  • throw an error after the fail_after threshold

Thresholds are in chars, not bytes nor cols nor graphemes.

Auto Trait Implementations§

§

impl<R> Freeze for CharReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for CharReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for CharReader<R>
where R: Send,

§

impl<R> Sync for CharReader<R>
where R: Sync,

§

impl<R> Unpin for CharReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for CharReader<R>
where 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> 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, 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.