pub struct Buffer<T: AsMut<[u8]> + AsRef<[u8]>> { /* private fields */ }
Expand description
A buffer that allows reading and writuing bytes u8
from / to an underlying generic source
Implementations§
Source§impl<T: AsMut<[u8]> + AsRef<[u8]>> Buffer<T>
impl<T: AsMut<[u8]> + AsRef<[u8]>> Buffer<T>
Sourcepub fn new(source: T) -> Self
pub fn new(source: T) -> Self
Create a new buffer from any source
§Example
use embytes_buffer::Buffer;
let mut bytes = [0; 1024];
let mut buffer = Buffer::new(&mut bytes);
Examples found in repository?
8fn main () {
9 let mut bytes = [0; 1024];
10 let mut buffer = Buffer::new(&mut bytes);
11
12 // Write some bytes
13 buffer.write_all("abc".as_bytes()).unwrap();
14
15 // try to read to a komma but there is none
16 let reader = buffer.create_reader();
17 let result = read_til_komma(&reader);
18 assert_eq!(result, None);
19 drop(reader);
20
21 // Write a string that now contains a comma
22 buffer.write_all("def,1234".as_bytes()).unwrap();
23
24 // try to read to a komma. now there is one
25 let reader = buffer.create_reader();
26 let result = read_til_komma(&reader);
27 assert_eq!(result, Some("abcdef"));
28 drop(reader);
29
30 assert_eq!(buffer.data(), "1234".as_bytes());
31}
More examples
6fn main () {
7 let mut bytes = [0; 1024];
8 let mut buffer = Buffer::new(&mut bytes);
9
10 // Write some bytes
11 buffer.write_all("abc".as_bytes()).unwrap();
12
13 // Create a writer, write some bytes but do not commit
14 // writer implements DerefMut<Target = [u8]> and can be used as a mutable bytes slice
15 let mut writer = buffer.create_writer();
16 writer[0] = 36; // ascii '$'
17 drop(writer);
18
19 // Create a new writer
20 let mut writer = buffer.create_writer();
21 writer[0] = 100; // ascii d
22 writer[1] = 101; // ascii e
23 writer[2] = 102; // ascii f
24
25 // Commit that 3 bytes are written
26 // writing bytes has only an effect if the written bytes are committed
27 writer.commit(3).unwrap();
28 drop(writer); // drop the writer to follow the borrowing rules. at drop the written bytes are committed
29
30 let mut result = [0; 1024];
31 let bytes_read = buffer.read(&mut result).unwrap();
32 assert_eq!("abcdef".as_bytes(), &result[..bytes_read]);
33}
Sourcepub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Returns the remaining space that can be written to.
This method does not perform a Buffer::shift
Sourcepub fn has_remaining_capacity(&self) -> bool
pub fn has_remaining_capacity(&self) -> bool
returns true
if there is remaining capacity to write to.
is equal to Buffer::remaining_capacity
> 0
Sourcepub fn remaining_len(&self) -> usize
pub fn remaining_len(&self) -> usize
Returns the remaining bytes to read
Sourcepub fn has_remaining_len(&self) -> bool
pub fn has_remaining_len(&self) -> bool
returns true
if there are remainng bytes to read.
Sourcepub fn has_dead_capacity(&self) -> bool
pub fn has_dead_capacity(&self) -> bool
Returns true
if there is dead capacity.
Dead capacity occures when bytes are read from a buffer.
Dead capacity can be regained by using Buffer::shift
Sourcepub fn shift(&mut self)
pub fn shift(&mut self)
Shifts the content of the source left to reuse space of read bytes.
See also Buffer::has_dead_capacity
Sourcepub fn ensure_remaining_capacity(&mut self) -> bool
pub fn ensure_remaining_capacity(&mut self) -> bool
Performa s Buffer::shift
if there is no remianing capacity and
returns true
if there is remainig capacity afterwards
Sourcepub fn create_reader_with_max(&mut self, max_bytes: usize) -> Reader<'_, T>
pub fn create_reader_with_max(&mut self, max_bytes: usize) -> Reader<'_, T>
Creates a reader that ready at most max_bytes
Sourcepub fn data(&self) -> &[u8] ⓘ
pub fn data(&self) -> &[u8] ⓘ
Returns a slice containing the readable data
Examples found in repository?
8fn main () {
9 let mut bytes = [0; 1024];
10 let mut buffer = Buffer::new(&mut bytes);
11
12 // Write some bytes
13 buffer.write_all("abc".as_bytes()).unwrap();
14
15 // try to read to a komma but there is none
16 let reader = buffer.create_reader();
17 let result = read_til_komma(&reader);
18 assert_eq!(result, None);
19 drop(reader);
20
21 // Write a string that now contains a comma
22 buffer.write_all("def,1234".as_bytes()).unwrap();
23
24 // try to read to a komma. now there is one
25 let reader = buffer.create_reader();
26 let result = read_til_komma(&reader);
27 assert_eq!(result, Some("abcdef"));
28 drop(reader);
29
30 assert_eq!(buffer.data(), "1234".as_bytes());
31}
Sourcepub fn push(&mut self, buf: &[u8]) -> Result<(), BufferError>
pub fn push(&mut self, buf: &[u8]) -> Result<(), BufferError>
Appends the provided slice to the buffer a a whole
§Error
BufferError::NoCapacity
if buf.len() > self.remaining_capacity()
Trait Implementations§
Source§impl<S: AsMut<[u8]> + AsRef<[u8]>> JsonWriter for Buffer<S>
impl<S: AsMut<[u8]> + AsRef<[u8]>> JsonWriter for Buffer<S>
fn serialize_json<T: Serialize>( &mut self, src: &T, ) -> Result<usize, BufferError>
Source§impl<T: AsMut<[u8]> + AsRef<[u8]>> Read for Buffer<T>
impl<T: AsMut<[u8]> + AsRef<[u8]>> Read for Buffer<T>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
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>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
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>
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>
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>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<T: AsMut<[u8]> + AsRef<[u8]>> Read for Buffer<T>
impl<T: AsMut<[u8]> + AsRef<[u8]>> Read for Buffer<T>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Source§fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>>
fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
buf
. Read moreSource§impl<T: AsMut<[u8]> + AsRef<[u8]>> ReadWrite for Buffer<T>
impl<T: AsMut<[u8]> + AsRef<[u8]>> ReadWrite for Buffer<T>
Source§fn create_reader<'a>(&'a mut self) -> impl BufferReader + 'a
fn create_reader<'a>(&'a mut self) -> impl BufferReader + 'a
Source§fn create_writer<'a>(&'a mut self) -> impl BufferWriter + 'a
fn create_writer<'a>(&'a mut self) -> impl BufferWriter + 'a
Source§impl<T: AsMut<[u8]> + AsRef<[u8]>> Write for Buffer<T>
impl<T: AsMut<[u8]> + AsRef<[u8]>> Write for Buffer<T>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)