pub struct ByteCode<'a> { /* private fields */ }Implementations§
Source§impl<'a> ByteCode<'a>
impl<'a> ByteCode<'a>
Sourcepub fn new(slice: &'a [u8]) -> Self
pub fn new(slice: &'a [u8]) -> Self
Creates a new ByteCode.
§Examples
use bytecode::ByteCode;
let bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);Sourcepub fn as_slice(&self) -> &'a [u8] ⓘ
pub fn as_slice(&self) -> &'a [u8] ⓘ
Extracts a current remaining slice.
Equivalent to &bytes[..].
§Examples
use bytecode::ByteCode;
let bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
let slice = bytes.as_slice();Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements.
Note that consumed elements are also counted.
§Examples
use bytecode::ByteCode;
let bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
assert_eq!(bytes.len(), 8);Sourcepub fn pos(&self) -> usize
pub fn pos(&self) -> usize
Returns the pointer position.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
bytes += 5;
assert_eq!(bytes.pos(), 5);Source§impl<'a> ByteCode<'a>
impl<'a> ByteCode<'a>
Sourcepub fn peek(&'a self, num: usize) -> &'a [u8] ⓘ
pub fn peek(&'a self, num: usize) -> &'a [u8] ⓘ
Returns a reference to subslice corresponding to the given size.
§Examples
use bytecode::ByteCode;
let bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
assert_eq!(bytes.peek(3), [0, 1, 2]);Sourcepub fn starts_with(&self, v: &[u8]) -> bool
pub fn starts_with(&self, v: &[u8]) -> bool
Returns true if given subslice is a prefix of the slice.
§Examples
use bytecode::ByteCode;
let bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
assert!(bytes.starts_with(&[0, 1, 2]));Sourcepub fn next(&mut self)
pub fn next(&mut self)
Move the pointer to the next. Note that nothing is returned.
Equivalent to bytes += 1.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
bytes += 3;Sourcepub fn prev(&mut self)
pub fn prev(&mut self)
Move the pointer to the prev. Note that nothing is returned.
Equivalent to bytes -= 1.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
bytes += 5;
bytes -= 3;Sourcepub fn skip(&mut self, num: usize)
pub fn skip(&mut self, num: usize)
Move the pointer forward by the given number.
Equivalent to bytes += num.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
bytes.skip(3);Sourcepub fn take(&mut self, num: usize) -> Vec<u8> ⓘ
pub fn take(&mut self, num: usize) -> Vec<u8> ⓘ
Returns a vector containing a copy of subslice corresponding to the given size. Moves the pointer forward by the length of subslice.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);
assert_eq!(bytes.take(3), [0, 1, 2]);Sourcepub fn take_into_u8(&mut self) -> u8
pub fn take_into_u8(&mut self) -> u8
Returns the first byte. Moves the pointer forward 1.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
assert_eq!(bytes.take_into_u8(), u8::MAX);Sourcepub fn take_into_u16(&mut self) -> u16
pub fn take_into_u16(&mut self) -> u16
Returns the first 2 elements of the slice converted into u16.
Moves the pointer forward 2.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
assert_eq!(bytes.take_into_u16(), u16::MAX);Sourcepub fn take_into_u32(&mut self) -> u32
pub fn take_into_u32(&mut self) -> u32
Returns the first 4 elements of the slice converted into u32.
Moves the pointer forward 4.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00]);
assert_eq!(bytes.take_into_u32(), u32::MAX);Sourcepub fn take_into_string(&mut self, num: usize) -> String
pub fn take_into_string(&mut self, num: usize) -> String
Returns the string consisting of the given number of bytes from the beginning of the slice. Moves the pointer forward by given number.
§Examples
use bytecode::ByteCode;
let mut bytes = ByteCode::new(&[0x66, 0x6f, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00]);
assert_eq!(bytes.take_into_string(3), "foo".to_owned());Trait Implementations§
Source§impl<'a> AddAssign<usize> for ByteCode<'a>
impl<'a> AddAssign<usize> for ByteCode<'a>
Source§fn add_assign(&mut self, rhs: usize)
fn add_assign(&mut self, rhs: usize)
Move the pointer to the next.