BufView

Struct BufView 

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

Wrap a &[u8] buffer as read only.

BufView support many methods to read primitive types from a byte buffer easily, it support to read primitive types as big endian or little endian. BufView 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.

Any more, it support get method, too. It make you random get data from the BufView.

BufView structure

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

Example

use buf_view::BufView;

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

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);

// wrap from vector
let v = vec![0, 1, 2, 3, 4, 5, 6, 7];
let mut buf_view = BufView::wrap(v.as_slice());
assert_eq!(buf_view.read_u8(), 0);
assert_eq!(buf_view.read_u32(), 0x01020304);

// wrap from &str
let s = "01234567";
let mut buf_view = BufView::wrap(s.as_bytes());
assert_eq!(buf_view.read_u8(), 0x30);
assert_eq!(buf_view.read_u32(), 0x31323334);

Implementations§

Source§

impl<'a> BufView<'a>

Source

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

Wrap the buf as BufView, set the reader_index=0 and writer_index=buf.len(), this make the whole buf can read and get by default.

Source

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

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

use buf_view::BufView;

let buf = [0, 1, 2, 3, 4, 5, 6];
let mut buf = BufView::wrap_with(&buf, 1, 5);
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 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) -> &[u8]

Source

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

Trait Implementations§

Source§

impl<'a> Debug for BufView<'a>

Source§

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

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

impl Display for BufView<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for BufView<'a>

§

impl<'a> RefUnwindSafe for BufView<'a>

§

impl<'a> Send for BufView<'a>

§

impl<'a> Sync for BufView<'a>

§

impl<'a> Unpin for BufView<'a>

§

impl<'a> UnwindSafe for BufView<'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.