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>
impl<'a> BufView<'a>
Sourcepub fn wrap(buf: &'a [u8]) -> Self
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.
Sourcepub fn wrap_with(
buf: &'a [u8],
reader_index: usize,
writer_index: usize,
) -> Self
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);pub fn read_u8(&mut self) -> u8
pub fn read_i8(&mut self) -> i8
pub fn read_u16(&mut self) -> u16
pub fn read_u16_le(&mut self) -> u16
pub fn read_i16(&mut self) -> i16
pub fn read_i16_le(&mut self) -> i16
pub fn read_u32(&mut self) -> u32
pub fn read_u32_le(&mut self) -> u32
pub fn read_i32(&mut self) -> i32
pub fn read_i32_le(&mut self) -> i32
pub fn read_u64(&mut self) -> u64
pub fn read_u64_le(&mut self) -> u64
pub fn read_i64(&mut self) -> i64
pub fn read_i64_le(&mut self) -> i64
pub fn read_u128(&mut self) -> u128
pub fn read_u128_le(&mut self) -> u128
pub fn read_i128(&mut self) -> i128
pub fn read_i128_le(&mut self) -> i128
pub fn read_f32(&mut self) -> f32
pub fn read_f32_le(&mut self) -> f32
pub fn read_f64(&mut self) -> f64
pub fn read_f64_le(&mut self) -> f64
pub fn read_bytes(&mut self, dest: &mut [u8]) -> usize
pub fn get_u8(&mut self, index: usize) -> u8
pub fn get_i8(&mut self, index: usize) -> i8
pub fn get_u16(&mut self, index: usize) -> u16
pub fn get_u16_le(&mut self, index: usize) -> u16
pub fn get_i16(&mut self, index: usize) -> i16
pub fn get_i16_le(&mut self, index: usize) -> i16
pub fn get_u32(&mut self, index: usize) -> u32
pub fn get_u32_le(&mut self, index: usize) -> u32
pub fn get_i32(&mut self, index: usize) -> i32
pub fn get_i32_le(&mut self, index: usize) -> i32
pub fn get_u64(&mut self, index: usize) -> u64
pub fn get_u64_le(&mut self, index: usize) -> u64
pub fn get_i64(&mut self, index: usize) -> i64
pub fn get_i64_le(&mut self, index: usize) -> i64
pub fn get_u128(&mut self, index: usize) -> u128
pub fn get_u128_le(&mut self, index: usize) -> u128
pub fn get_i128(&mut self, index: usize) -> i128
pub fn get_i128_le(&mut self, index: usize) -> i128
pub fn get_f32(&mut self, index: usize) -> f32
pub fn get_f32_le(&mut self, index: usize) -> f32
pub fn get_f64(&mut self, index: usize) -> f64
pub fn get_f64_le(&mut self, index: usize) -> f64
pub fn get_bytes(&mut self, index: usize, dest: &mut [u8]) -> usize
pub fn set_reader_index(&mut self, index: usize)
pub fn reader_index(&self) -> usize
pub fn set_writer_index(&mut self, index: usize)
pub fn writer_index(&self) -> usize
pub fn set_index(&mut self, reader_index: usize, writer_index: usize)
pub fn clear(&mut self)
pub fn remaining(&self) -> usize
pub fn capacity(&self) -> usize
pub fn as_slice(&mut self) -> &[u8] ⓘ
pub fn as_raw_slice(&mut self) -> &[u8] ⓘ
Trait Implementations§
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> 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