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>
impl<'a> Reader<'a>
Sourcepub fn new(b: &'a [u8]) -> Self
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
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}
pub fn is_empty(&self) -> bool
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub fn unread_byte(&mut self) -> Result<()>
pub fn unread_byte(&mut self) -> Result<()>
unread_byte complements read_byte in implementing the io.ByteScanner interface.
Sourcepub fn seek(&mut self, offset: i64, whence: Seek) -> Result<u64>
pub fn seek(&mut self, offset: i64, whence: Seek) -> Result<u64>
seek implements the io.Seeker interface.
Trait Implementations§
Source§impl BufRead for Reader<'_>
impl BufRead for Reader<'_>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
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 moreSource§fn consume(&mut self, amt: usize)
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 moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
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 more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
Skips all bytes until the delimiter
byte
or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
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 moreSource§impl ByteReader for Reader<'_>
impl ByteReader for Reader<'_>
Source§impl Read for Reader<'_>
impl Read for Reader<'_>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
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>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
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 more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
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 more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Reads the exact number of bytes required to fill
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
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>
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 more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Creates a “by reference” adaptor for this instance of
Read
. Read moreAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more