BufViewMut

Struct BufViewMut 

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

Wrap a &mut [u8] buffer as read and write.

BufViewMut support many methods to read/write primitive types from a byte buffer easily, it support to read/write primitive types as big endian or little endian. BufViewMut wrap the original buffer with reader_index and writer_index as fllowing structure. When read data from it, the reader_index will advance the data length, and the read must between the reader_index and writer_index. When write data to it, the writer_index will advance the data length, too.

Any more, it support get method, too. It make random get data from the BufViewMut, and support set method, too, which make random put data to the BufViewMut.

BufViewMut structure

-----------------------------------------------------
|       |                         |                 |
-----------------------------------------------------
        ^                         ^                 ^
        |                         |                 |
  reader_index              writer_index        buf.len()

Example

use buf_view::BufViewMut;

let mut buf = [0u8;7];
let mut buf_view = BufViewMut::wrap(&mut buf);

buf_view.write_u8(0);
buf_view.write_u16(0x0102);
buf_view.write_u32(0x03040506);

assert_eq!(buf_view.read_u8(), 0);
assert_eq!(buf_view.read_u16(), 0x0102);
assert_eq!(buf_view.read_u32(), 0x03040506);
assert_eq!(buf_view.get_u16(1), 0x0102);

Implementations§

Source§

impl<'a> BufViewMut<'a>

Source

pub fn wrap(buf: &'a mut [u8]) -> Self

Wrap the buf as BufViewMut, set the reader_index=0 and writer_index=0, this make the whole buf can write by default.

Source

pub fn wrap_with( buf: &'a mut [u8], reader_index: usize, writer_index: usize, ) -> Self

Wrap the buf as BufViewMut, and specify the reader_index and writer_index.

use buf_view::BufViewMut;

let mut buf = [0, 1, 2, 3, 4, 5, 6, 7];
let mut buf = BufViewMut::wrap_with(&mut buf, 1, 3);

buf.write_u32(0x01020304);

assert_eq!(buf.read_u16(), 0x0102);
assert_eq!(buf.read_u32(), 0x01020304);
Source

pub fn read_u8(&mut self) -> u8

Source

pub fn read_i8(&mut self) -> i8

Source

pub fn read_u16(&mut self) -> u16

Source

pub fn read_u16_le(&mut self) -> u16

Source

pub fn read_i16(&mut self) -> i16

Source

pub fn read_i16_le(&mut self) -> i16

Source

pub fn read_u32(&mut self) -> u32

Source

pub fn read_u32_le(&mut self) -> u32

Source

pub fn read_i32(&mut self) -> i32

Source

pub fn read_i32_le(&mut self) -> i32

Source

pub fn read_u64(&mut self) -> u64

Source

pub fn read_u64_le(&mut self) -> u64

Source

pub fn read_i64(&mut self) -> i64

Source

pub fn read_i64_le(&mut self) -> i64

Source

pub fn read_u128(&mut self) -> u128

Source

pub fn read_u128_le(&mut self) -> u128

Source

pub fn read_i128(&mut self) -> i128

Source

pub fn read_i128_le(&mut self) -> i128

Source

pub fn read_f32(&mut self) -> f32

Source

pub fn read_f32_le(&mut self) -> f32

Source

pub fn read_f64(&mut self) -> f64

Source

pub fn read_f64_le(&mut self) -> f64

Source

pub fn read_bytes(&mut self, dest: &mut [u8]) -> usize

Source

pub fn get_u8(&mut self, index: usize) -> u8

Source

pub fn get_i8(&mut self, index: usize) -> i8

Source

pub fn get_u16(&mut self, index: usize) -> u16

Source

pub fn get_u16_le(&mut self, index: usize) -> u16

Source

pub fn get_i16(&mut self, index: usize) -> i16

Source

pub fn get_i16_le(&mut self, index: usize) -> i16

Source

pub fn get_u32(&mut self, index: usize) -> u32

Source

pub fn get_u32_le(&mut self, index: usize) -> u32

Source

pub fn get_i32(&mut self, index: usize) -> i32

Source

pub fn get_i32_le(&mut self, index: usize) -> i32

Source

pub fn get_u64(&mut self, index: usize) -> u64

Source

pub fn get_u64_le(&mut self, index: usize) -> u64

Source

pub fn get_i64(&mut self, index: usize) -> i64

Source

pub fn get_i64_le(&mut self, index: usize) -> i64

Source

pub fn get_u128(&mut self, index: usize) -> u128

Source

pub fn get_u128_le(&mut self, index: usize) -> u128

Source

pub fn get_i128(&mut self, index: usize) -> i128

Source

pub fn get_i128_le(&mut self, index: usize) -> i128

Source

pub fn get_f32(&mut self, index: usize) -> f32

Source

pub fn get_f32_le(&mut self, index: usize) -> f32

Source

pub fn get_f64(&mut self, index: usize) -> f64

Source

pub fn get_f64_le(&mut self, index: usize) -> f64

Source

pub fn get_bytes(&mut self, index: usize, dest: &mut [u8]) -> usize

Source

pub fn write_u8(&mut self, val: u8)

Source

pub fn write_u16(&mut self, val: u16)

Source

pub fn write_u16_le(&mut self, val: u16)

Source

pub fn write_i16(&mut self, val: i16)

Source

pub fn write_i16_le(&mut self, val: i16)

Source

pub fn write_u32(&mut self, val: u32)

Source

pub fn write_u32_le(&mut self, val: u32)

Source

pub fn write_i32(&mut self, val: i32)

Source

pub fn write_i32_le(&mut self, val: i32)

Source

pub fn write_u64(&mut self, val: u64)

Source

pub fn write_u64_le(&mut self, val: u64)

Source

pub fn write_i64(&mut self, val: i64)

Source

pub fn write_i64_le(&mut self, val: i64)

Source

pub fn write_u128(&mut self, val: u128)

Source

pub fn write_u128_le(&mut self, val: u128)

Source

pub fn write_i128(&mut self, val: i128)

Source

pub fn write_i128_le(&mut self, val: i128)

Source

pub fn write_f32(&mut self, val: f32)

Source

pub fn write_f32_le(&mut self, val: f32)

Source

pub fn write_f64(&mut self, val: f64)

Source

pub fn write_f64_le(&mut self, val: f64)

Source

pub fn write_bytes(&mut self, src: &[u8])

Source

pub fn write_bytes_uncheck(&mut self, src: &[u8]) -> usize

Source

pub fn set_u8(&mut self, index: usize, val: u8)

Source

pub fn set_u16(&mut self, index: usize, val: u16)

Source

pub fn set_u16_le(&mut self, index: usize, val: u16)

Source

pub fn set_i16(&mut self, index: usize, val: i16)

Source

pub fn set_i16_le(&mut self, index: usize, val: i16)

Source

pub fn set_u32(&mut self, index: usize, val: u32)

Source

pub fn set_u32_le(&mut self, index: usize, val: u32)

Source

pub fn set_i32(&mut self, index: usize, val: i32)

Source

pub fn set_i32_le(&mut self, index: usize, val: i32)

Source

pub fn set_u64(&mut self, index: usize, val: u64)

Source

pub fn set_u64_le(&mut self, index: usize, val: u64)

Source

pub fn set_i64(&mut self, index: usize, val: i64)

Source

pub fn set_i64_le(&mut self, index: usize, val: i64)

Source

pub fn set_u128(&mut self, index: usize, val: u128)

Source

pub fn set_u128_le(&mut self, index: usize, val: u128)

Source

pub fn set_i128(&mut self, index: usize, val: i128)

Source

pub fn set_i128_le(&mut self, index: usize, val: i128)

Source

pub fn set_f32(&mut self, index: usize, val: f32)

Source

pub fn set_f32_le(&mut self, index: usize, val: f32)

Source

pub fn set_f64(&mut self, index: usize, val: f64)

Source

pub fn set_f64_le(&mut self, index: usize, val: f64)

Source

pub fn set_bytes(&mut self, index: usize, src: &[u8])

Source

pub fn set_reader_index(&mut self, index: usize)

Source

pub fn reader_index(&self) -> usize

Source

pub fn set_writer_index(&mut self, index: usize)

Source

pub fn writer_index(&self) -> usize

Source

pub fn set_index(&mut self, reader_index: usize, writer_index: usize)

Source

pub fn clear(&mut self)

Source

pub fn remaining(&self) -> usize

Source

pub fn capacity(&self) -> usize

Source

pub fn as_slice(&mut self) -> &mut [u8]

Source

pub fn as_raw_slice(&mut self) -> &mut [u8]

Trait Implementations§

Source§

impl<'a> Debug for BufViewMut<'a>

Source§

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

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

impl Display for BufViewMut<'_>

Source§

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

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

impl<'a> Write for BufViewMut<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for BufViewMut<'a>

§

impl<'a> RefUnwindSafe for BufViewMut<'a>

§

impl<'a> Send for BufViewMut<'a>

§

impl<'a> Sync for BufViewMut<'a>

§

impl<'a> Unpin for BufViewMut<'a>

§

impl<'a> !UnwindSafe for BufViewMut<'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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.