Struct Reader

Source
pub struct Reader<'a> { /* private fields */ }
Expand description

A Reader implements the crate::io::Reader, crate::io::ReaderAt, crate::io::WriterTo, crate::io::Seeker, crate::io::ByteScanner, and io.RuneScanner interfaces by reading from a u8 slice. Unlike a Buffer, a Reader is read-only and supports seeking. The zero value for Reader operates like a Reader of an empty slice.

Implementations§

Source§

impl<'a> Reader<'a>

Source

pub fn new(b: &'a [u8]) -> Self

new returns a new Reader reading from b.

Examples found in repository?
examples/zlib-reader.rs (line 11)
6fn main() {
7    let buff: &[u8] = &[
8        120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255, 255,
9        33, 231, 4, 147,
10    ];
11    let mut b = bytes::Reader::new(buff);
12    let mut r = zlib::Reader::new(&mut b).unwrap();
13
14    // reading using Reader::read
15    // loop {
16    //     let mut buf = [0; 10];
17    //     let res = r.read(&mut buf);
18    //     if res.is_err() {
19    //         break;
20    //     }
21    //     let n = res.unwrap();
22    //     if n == 0 {
23    //         break;
24    //     }
25    // }
26
27    // reading using std::io::Read::read_to_string
28    let mut output = String::new();
29    r.read_to_string(&mut output).unwrap();
30    println!("{}", output);
31
32    r.close().unwrap();
33}
More examples
Hide additional examples
examples/png-decode.rs (line 18)
16fn example_decode() {
17    let image_data = base64::get_std_encoding().decode_to_vec(GOPHER).unwrap();
18    let mut r = bytes::Reader::new(&image_data);
19
20    // This example uses png.Decode which can only decode PNG images.
21    // Consider using the general image.Decode as it can sniff and decode any registered image format.
22    let img = png::decode(&mut r).unwrap();
23
24    let levels = [" ", "░", "▒", "▓", "█"];
25
26    for y in img.bounds().min.y..img.bounds().max.y {
27        for x in img.bounds().min.x..img.bounds().max.x {
28            let c = color::Gray::new_from(&img.at(x, y));
29            let mut level = c.y / 51; // 51 * 5 = 255
30            if level == 5 {
31                level -= 1;
32            }
33            print!("{}", levels[level as usize]);
34        }
35        println!();
36    }
37}
Source

pub fn len(&self) -> usize

len returns the number of bytes of the unread portion of the slice.

Source

pub fn is_empty(&self) -> bool

Source

pub fn size(&mut self) -> usize

size returns the original length of the underlying u8 slice. size is the number of bytes available for reading via read_at. The result is unaffected by any method calls except reset.

Source§

impl Reader<'_>

Source

pub fn read_at(&mut self, b: &mut [u8], off: u64) -> Result<usize>

read_at implements the io.ReaderAt interface.

Source§

impl<'a> Reader<'a>

Source

pub fn unread_byte(&mut self) -> Result<()>

unread_byte complements read_byte in implementing the io.ByteScanner interface.

Source

pub fn seek(&mut self, offset: i64, whence: Seek) -> Result<u64>

seek implements the io.Seeker interface.

Source

pub fn write_to(&mut self, w: &mut dyn Write) -> Result<usize>

write_to implements the io.WriterTo interface.

Source

pub fn reset(&mut self, b: &'a [u8])

reset resets the Reader to be reading from b.

Trait Implementations§

Source§

impl BufRead for Reader<'_>

Source§

fn fill_buf(&mut self) -> Result<&[u8]>

Returns the contents of the internal buffer, filling it with more data, via Read methods, if empty. Read more
Source§

fn consume(&mut self, amt: usize)

Marks the given amount of additional bytes from the internal buffer as having been read. Subsequent calls to read only return bytes that have not been marked as read. Read more
Source§

fn has_data_left(&mut self) -> Result<bool, Error>

🔬This is a nightly-only experimental API. (buf_read_has_data_left)
Checks if there is any data left to be read. Read more
1.0.0 · Source§

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
1.83.0 · Source§

fn skip_until(&mut self, byte: u8) -> Result<usize, Error>

Skips all bytes until the delimiter byte or EOF is reached. Read more
1.0.0 · Source§

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until a newline (the 0xA byte) is reached, and append them to the provided String buffer. Read more
1.0.0 · Source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte. Read more
1.0.0 · Source§

fn lines(self) -> Lines<Self>
where Self: Sized,

Returns an iterator over the lines of this reader. Read more
Source§

impl ByteReader for Reader<'_>

Source§

fn read_byte(&mut self) -> Result<u8>

read_byte implements the io.ByteReader interface.

Source§

impl Read for Reader<'_>

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<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

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.