Struct Buffer

Source
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>

Source

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?
examples/reader.rs (line 10)
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
Hide additional examples
examples/writer.rs (line 8)
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}
Source

pub fn reset(&mut self)

Reset the buffer to its initial state

Source

pub fn capacity(&self) -> usize

Returns the length of the undelying buffer

Source

pub fn remaining_capacity(&self) -> usize

Returns the remaining space that can be written to. This method does not perform a Buffer::shift

Source

pub fn has_remaining_capacity(&self) -> bool

returns true if there is remaining capacity to write to. is equal to Buffer::remaining_capacity > 0

Source

pub fn remaining_len(&self) -> usize

Returns the remaining bytes to read

Source

pub fn has_remaining_len(&self) -> bool

returns true if there are remainng bytes to read.

Source

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

Source

pub fn shift(&mut self)

Shifts the content of the source left to reuse space of read bytes. See also Buffer::has_dead_capacity

Source

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

Source

pub fn create_reader_with_max(&mut self, max_bytes: usize) -> Reader<'_, T>

Creates a reader that ready at most max_bytes

Source

pub fn data(&self) -> &[u8]

Returns a slice containing the readable data

Examples found in repository?
examples/reader.rs (line 30)
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}
Source

pub fn skip(&mut self, n: usize) -> Result<(), BufferError>

Skips n readable bytes

§Errors

BufferError::NoData if n < self.remaining_len()

Source

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<T: AsMut<[u8]> + AsRef<[u8]> + Clone> Clone for Buffer<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + AsMut<[u8]> + AsRef<[u8]>> Debug for Buffer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> ErrorType for Buffer<T>

Source§

type Error = ErrorKind

Error type of all the IO operations on this type.
Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Format for Buffer<T>

Source§

fn format(&self, fmt: Formatter<'_>)

Writes the defmt representation of self to fmt.
Source§

impl<S: AsMut<[u8]> + AsRef<[u8]>> JsonWriter for Buffer<S>

Source§

fn serialize_json<T: Serialize>( &mut self, src: &T, ) -> Result<usize, BufferError>

Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Read for Buffer<T>

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
Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Read for Buffer<T>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> ReadWrite for Buffer<T>

Source§

fn create_reader<'a>(&'a mut self) -> impl BufferReader + 'a

Creates a reader to read from the buffer
Source§

fn create_writer<'a>(&'a mut self) -> impl BufferWriter + 'a

Creates a writer to write to the buffer
Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Write for Buffer<T>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
Source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Write for Buffer<T>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, blocking until all intermediately buffered contents reach their destination.
Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more
Source§

fn write_fmt( &mut self, fmt: Arguments<'_>, ) -> Result<(), WriteFmtError<Self::Error>>

Write a formatted string into this writer, returning any error encountered. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Buffer<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Buffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for Buffer<T>
where T: Send,

§

impl<T> Sync for Buffer<T>
where T: Sync,

§

impl<T> Unpin for Buffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for Buffer<T>
where T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.