Struct BytecodeMapped

Source
pub struct BytecodeMapped<Op, Bytes = Vec<u8>> { /* private fields */ }
Expand description

A memory efficient representation of a sequence of operations parsed from bytecode.

Executing certain control flow operations can require the ability to jump back to a previous operation.

One simple solution might be to use a Vec<Op>, however it is important to consider that the size of each element within a Vec<Op> will be equal to the size of the discriminant plus the largest Op variant size (today, this is Push(Word), but this may change as new operations are added). This can have memory requirement implications for programs with large numbers of ops.

To avoid this issue, we instead store the raw “packed” bytecode alongside a list of indices into the bytecode representing the location of each operation.

Implementations§

Source§

impl<Op> BytecodeMapped<Op, Vec<u8>>

Source

pub fn push_op(&mut self, op: Op)
where Op: ToBytes,

Push a single operation onto the bytecode mapping.

Source§

impl<Op, Bytes> BytecodeMapped<Op, Bytes>
where Bytes: Deref<Target = [u8]>,

Source

pub fn try_from_bytes( bytes: Bytes, ) -> Result<BytecodeMapped<Op, Bytes>, Op::Error>
where Op: ToOpcode + TryFromBytes, Op::Opcode: ParseOp<Op = Op> + TryFrom<u8>, Op::Error: From<<Op::Opcode as TryFrom<u8>>::Error> + From<<Op::Opcode as ParseOp>::Error>,

Attempt to construct a BytecodeMapped from an existing slice of bytes.

bytes may be any type that dereferences to a slice of bytes, e.g. &[u8], Arc<[u8]>, Vec<u8>, etc.

Source

pub fn as_slice(&self) -> BytecodeMappedSlice<'_, Op>

Borrow the inner bytecode and op_indices slices and return a BytecodeMappedSlice.

Source

pub fn bytecode(&self) -> &[u8]

The inner slice of bytecode that has been mapped.

Source

pub fn ops_from(&self, start: usize) -> Option<BytecodeMappedSlice<'_, Op>>

Slice the op indices from the given index.

The returned slice represents the remainder of the program from the given op.

Returns None if start is out of range of the op_indices slice.

Source

pub fn op(&self, ix: usize) -> Option<Op>
where Op: TryFromBytes,

The operation at the given index.

Source

pub fn ops(&self) -> impl '_ + Iterator<Item = Op>
where Op: TryFromBytes,

An iterator yielding all mapped operations.

Source§

impl<Op, Bytes> BytecodeMapped<Op, Bytes>

Source

pub fn op_indices(&self) -> &[usize]

The slice of operation indices within the mapped bytecode.

Trait Implementations§

Source§

impl<Op: Clone, Bytes: Clone> Clone for BytecodeMapped<Op, Bytes>

Source§

fn clone(&self) -> BytecodeMapped<Op, Bytes>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Op: Debug, Bytes: Debug> Debug for BytecodeMapped<Op, Bytes>

Source§

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

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

impl<Op> Default for BytecodeMapped<Op>

Manually implement Default to avoid requiring that Op: Default as is assumed by derive(Default).

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<Op> FromIterator<Op> for BytecodeMapped<Op>
where Op: ToBytes,

Source§

fn from_iter<T: IntoIterator<Item = Op>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, Op, Bytes> OpAccess for &'a BytecodeMapped<Op, Bytes>
where Op: TryFromBytes, Bytes: Deref<Target = [u8]>,

Source§

type Op = Op

The operation type being accessed.
Source§

type Error = Infallible

Any error that might occur during access.
Source§

fn op_access(&mut self, index: usize) -> Option<Result<Self::Op, Self::Error>>

Access the operation at the given index. Read more
Source§

impl<Op: PartialEq, Bytes: PartialEq> PartialEq for BytecodeMapped<Op, Bytes>

Source§

fn eq(&self, other: &BytecodeMapped<Op, Bytes>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, Op> TryFrom<&'a [u8]> for BytecodeMapped<Op, &'a [u8]>
where Op: ToOpcode + TryFromBytes, Op::Opcode: ParseOp<Op = Op> + TryFrom<u8>, Op::Error: From<<Op::Opcode as TryFrom<u8>>::Error> + From<<Op::Opcode as ParseOp>::Error>,

Allow for consuming and mapping an existing &[u8].

Source§

type Error = <Op as TryFromBytes>::Error

The type returned in the event of a conversion error.
Source§

fn try_from(bytecode: &'a [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<Op> TryFrom<Vec<u8>> for BytecodeMapped<Op>
where Op: ToOpcode + TryFromBytes, Op::Opcode: ParseOp<Op = Op> + TryFrom<u8>, Op::Error: From<<Op::Opcode as TryFrom<u8>>::Error> + From<<Op::Opcode as ParseOp>::Error>,

Allow for taking ownership over and mapping an existing Vec<u8>.

Source§

type Error = <Op as TryFromBytes>::Error

The type returned in the event of a conversion error.
Source§

fn try_from(bytecode: Vec<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<Op, Bytes> StructuralPartialEq for BytecodeMapped<Op, Bytes>

Auto Trait Implementations§

§

impl<Op, Bytes> Freeze for BytecodeMapped<Op, Bytes>
where Bytes: Freeze,

§

impl<Op, Bytes> RefUnwindSafe for BytecodeMapped<Op, Bytes>
where Bytes: RefUnwindSafe, Op: RefUnwindSafe,

§

impl<Op, Bytes> Send for BytecodeMapped<Op, Bytes>
where Bytes: Send, Op: Send,

§

impl<Op, Bytes> Sync for BytecodeMapped<Op, Bytes>
where Bytes: Sync, Op: Sync,

§

impl<Op, Bytes> Unpin for BytecodeMapped<Op, Bytes>
where Bytes: Unpin, Op: Unpin,

§

impl<Op, Bytes> UnwindSafe for BytecodeMapped<Op, Bytes>
where Bytes: UnwindSafe, Op: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.