Struct multi_readers::BytesReader
source · pub struct BytesReader { /* private fields */ }Expand description
A bytes wrapper and implements trait Read
§Usage
use multi_readers::BytesReader;
use std::io::Read;
let bytes = b"hello world";
let mut reader = BytesReader::new(bytes.to_vec());
let mut buf = [0; 6];
let size = reader.read(&mut buf).unwrap();
assert_eq!(&buf[..size], b"hello ");
let size = reader.read(&mut buf).unwrap();
assert_eq!(&buf[..size], b"world");Implementations§
source§impl BytesReader
impl BytesReader
sourcepub fn new(buf: Vec<u8>) -> BytesReader ⓘ
pub fn new(buf: Vec<u8>) -> BytesReader ⓘ
Create a new BytesReader with bytes
Examples found in repository?
examples/test.rs (line 5)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> std::io::Result<()> {
let slice = SliceReader::new(b"hello");
let bytes = BytesReader::new(b"world".to_vec());
let mut reader = join_readers!(slice, bytes);
let mut buf = [0; 5];
let len = reader.read(&mut buf)?;
assert_eq!(b"hello", &buf[..len]);
let len = reader.read(&mut buf)?;
assert_eq!(b"world", &buf[..len]);
let slice = SliceReader::new(b"First-");
let bytes = BytesReader::new(b"Second-".to_vec());
std::fs::write("test.txt", b"Third")?;
let f = File::open("test.txt")?;
let mut reader = join_readers!(slice, bytes, f);
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
assert_eq!(buf.as_str(), "First-Second-Third");
Ok(())
}Trait Implementations§
source§impl Read for BytesReader
impl Read for BytesReader
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 moresource§impl Seek for BytesReader
impl Seek for BytesReader
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in a stream. Read more
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Rewind to the beginning of a stream. Read more
source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
🔬This is a nightly-only experimental API. (
seek_stream_len)Returns the length of this stream (in bytes). Read more
Auto Trait Implementations§
impl Freeze for BytesReader
impl RefUnwindSafe for BytesReader
impl Send for BytesReader
impl Sync for BytesReader
impl Unpin for BytesReader
impl UnwindSafe for BytesReader
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