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>>
impl<Op> BytecodeMapped<Op, Vec<u8>>
Source§impl<Op, Bytes> BytecodeMapped<Op, Bytes>
impl<Op, Bytes> BytecodeMapped<Op, Bytes>
Sourcepub fn try_from_bytes(
bytes: Bytes,
) -> Result<BytecodeMapped<Op, Bytes>, Op::Error>
pub fn try_from_bytes( bytes: Bytes, ) -> Result<BytecodeMapped<Op, Bytes>, Op::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.
Sourcepub fn as_slice(&self) -> BytecodeMappedSlice<'_, Op>
pub fn as_slice(&self) -> BytecodeMappedSlice<'_, Op>
Borrow the inner bytecode and op_indices slices and return a BytecodeMappedSlice
.
Sourcepub fn ops_from(&self, start: usize) -> Option<BytecodeMappedSlice<'_, Op>>
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.
Sourcepub fn op(&self, ix: usize) -> Option<Op>where
Op: TryFromBytes,
pub fn op(&self, ix: usize) -> Option<Op>where
Op: TryFromBytes,
The operation at the given index.
Sourcepub fn ops(&self) -> impl '_ + Iterator<Item = Op>where
Op: TryFromBytes,
pub fn ops(&self) -> impl '_ + Iterator<Item = Op>where
Op: TryFromBytes,
An iterator yielding all mapped operations.
Source§impl<Op, Bytes> BytecodeMapped<Op, Bytes>
impl<Op, Bytes> BytecodeMapped<Op, Bytes>
Sourcepub fn op_indices(&self) -> &[usize]
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>
impl<Op: Clone, Bytes: Clone> Clone for BytecodeMapped<Op, Bytes>
Source§fn clone(&self) -> BytecodeMapped<Op, Bytes>
fn clone(&self) -> BytecodeMapped<Op, Bytes>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<Op> Default for BytecodeMapped<Op>
Manually implement Default
to avoid requiring that Op: Default
as is
assumed by derive(Default)
.
impl<Op> Default for BytecodeMapped<Op>
Manually implement Default
to avoid requiring that Op: Default
as is
assumed by derive(Default)
.
Source§impl<Op> FromIterator<Op> for BytecodeMapped<Op>where
Op: ToBytes,
impl<Op> FromIterator<Op> for BytecodeMapped<Op>where
Op: ToBytes,
Source§fn from_iter<T: IntoIterator<Item = Op>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = Op>>(iter: T) -> Self
Source§impl<'a, Op, Bytes> OpAccess for &'a BytecodeMapped<Op, Bytes>
impl<'a, Op, Bytes> OpAccess for &'a BytecodeMapped<Op, Bytes>
Source§impl<'a, Op> TryFrom<&'a [u8]> for BytecodeMapped<Op, &'a [u8]>
Allow for consuming and mapping an existing &[u8]
.
impl<'a, Op> TryFrom<&'a [u8]> for BytecodeMapped<Op, &'a [u8]>
Allow for consuming and mapping an existing &[u8]
.
Source§impl<Op> TryFrom<Vec<u8>> for BytecodeMapped<Op>
Allow for taking ownership over and mapping an existing Vec<u8>
.
impl<Op> TryFrom<Vec<u8>> for BytecodeMapped<Op>
Allow for taking ownership over and mapping an existing Vec<u8>
.
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>
impl<Op, Bytes> Sync for BytecodeMapped<Op, Bytes>
impl<Op, Bytes> Unpin for BytecodeMapped<Op, Bytes>
impl<Op, Bytes> UnwindSafe for BytecodeMapped<Op, Bytes>where
Bytes: UnwindSafe,
Op: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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