ByteCode

Struct ByteCode 

Source
pub struct ByteCode<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> ByteCode<'a>

Source

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]);
Examples found in repository?
examples/debug.rs (line 11)
6fn main() {
7    let mut f = File::open("./examples/puts.mrb").unwrap();
8    let mut buffer = Vec::new();
9    f.read_to_end(&mut buffer).unwrap();
10
11    let mut mrb = ByteCode::new(&buffer);
12
13    let _header = mrb.take(20);
14    dbg!(&mrb);
15}
Source

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

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

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

pub fn reset(&mut self)

Resets the pointer to original state.

§Examples
use bytecode::ByteCode;

let v = vec![0, 1, 2, 3, 4, 5, 6, 7];
let mut bytes = ByteCode::new(&v);
bytes += 5;
assert_eq!(bytes.as_slice(), [5, 6, 7]);
bytes.reset();
assert_eq!(bytes.as_slice(), v);
Source

pub fn is_end(&self) -> bool

Returns true if all elements have been consumed.

§Examples
use bytecode::ByteCode;

let mut bytes = ByteCode::new(&[0, 1, 2, 3, 4, 5, 6, 7]);

bytes += 8;
assert!(bytes.is_end());

bytes -= 6;
assert!(!bytes.is_end());
Source§

impl<'a> ByteCode<'a>

Source

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

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

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

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

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

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]);
Examples found in repository?
examples/debug.rs (line 13)
6fn main() {
7    let mut f = File::open("./examples/puts.mrb").unwrap();
8    let mut buffer = Vec::new();
9    f.read_to_end(&mut buffer).unwrap();
10
11    let mut mrb = ByteCode::new(&buffer);
12
13    let _header = mrb.take(20);
14    dbg!(&mrb);
15}
Source

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

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

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

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>

Source§

fn add_assign(&mut self, rhs: usize)

Move the pointer to the next.

Source§

impl Debug for ByteCode<'_>

Source§

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

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

impl<'a, I: SliceIndex<[u8]>> Index<I> for ByteCode<'a>

Source§

type Output = <I as SliceIndex<[u8]>>::Output

The returned type after indexing.
Source§

fn index(&self, i: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> SubAssign<usize> for ByteCode<'a>

Source§

fn sub_assign(&mut self, rhs: usize)

Move the pointer to the prev.

Auto Trait Implementations§

§

impl<'a> Freeze for ByteCode<'a>

§

impl<'a> RefUnwindSafe for ByteCode<'a>

§

impl<'a> Send for ByteCode<'a>

§

impl<'a> Sync for ByteCode<'a>

§

impl<'a> Unpin for ByteCode<'a>

§

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