pub struct Table(/* private fields */);
runtime
only.Expand description
A WebAssembly table
, or an array of values.
Like Memory
a table is an indexed array of values, but
unlike Memory
it’s an array of WebAssembly reference type
values rather than bytes. One of the most common usages of a table is a
function table for wasm modules (a funcref
table), where each element has
the ValType::FuncRef
type.
A Table
“belongs” to the store that it was originally created within
(either via Table::new
or via instantiating a
Module
). Operations on a Table
only work with the
store it belongs to, and if another store is passed in by accident then
methods will panic.
Implementations§
source§impl Table
impl Table
sourcepub fn new(store: impl AsContextMut, ty: TableType, init: Ref) -> Result<Table>
pub fn new(store: impl AsContextMut, ty: TableType, init: Ref) -> Result<Table>
Creates a new Table
with the given parameters.
store
- the owner of the resultingTable
ty
- the type of this table, containing both the element type as well as the initial size and maximum size, if any.init
- the initial value to fill all table entries with, if the table starts with an initial size.
§Errors
Returns an error if init
does not match the element type of the table,
or if init
does not belong to the store
provided.
§Panics
This function will panic when used with a Store
which has a ResourceLimiterAsync
(see also: Store::limiter_async
.
When using an async resource limiter, use Table::new_async
instead.
§Examples
let engine = Engine::default();
let mut store = Store::new(&engine, ());
let ty = TableType::new(RefType::FUNCREF, 2, None);
let table = Table::new(&mut store, ty, Ref::Func(None))?;
let module = Module::new(
&engine,
"(module
(table (import \"\" \"\") 2 funcref)
(func $f (result i32)
i32.const 10)
(elem (i32.const 0) $f)
)"
)?;
let instance = Instance::new(&mut store, &module, &[table.into()])?;
// ...
sourcepub async fn new_async<T>(
store: impl AsContextMut<Data = T>,
ty: TableType,
init: Ref,
) -> Result<Table>where
T: Send,
Available on crate feature async
only.
pub async fn new_async<T>(
store: impl AsContextMut<Data = T>,
ty: TableType,
init: Ref,
) -> Result<Table>where
T: Send,
async
only.Async variant of Table::new
. You must use this variant with
Store
s which have a
ResourceLimiterAsync
.
§Panics
This function will panic when used with a non-async
Store
sourcepub fn ty(&self, store: impl AsContext) -> TableType
pub fn ty(&self, store: impl AsContext) -> TableType
Returns the underlying type of this table, including its element type as well as the maximum/minimum lower bounds.
§Panics
Panics if store
does not own this table.
sourcepub fn get(&self, store: impl AsContextMut, index: u64) -> Option<Ref>
pub fn get(&self, store: impl AsContextMut, index: u64) -> Option<Ref>
Returns the table element value at index
.
Returns None
if index
is out of bounds.
§Panics
Panics if store
does not own this table.
sourcepub fn grow(
&self,
store: impl AsContextMut,
delta: u64,
init: Ref,
) -> Result<u64>
pub fn grow( &self, store: impl AsContextMut, delta: u64, init: Ref, ) -> Result<u64>
Grows the size of this table by delta
more elements, initialization
all new elements to init
.
Returns the previous size of this table if successful.
§Errors
Returns an error if the table cannot be grown by delta
, for example
if it would cause the table to exceed its maximum size. Also returns an
error if init
is not of the right type or if init
does not belong to
store
.
§Panics
Panics if store
does not own this table.
This function will panic when used with a Store
which has a ResourceLimiterAsync
(see also: Store::limiter_async
).
When using an async resource limiter, use Table::grow_async
instead.
sourcepub async fn grow_async<T>(
&self,
store: impl AsContextMut<Data = T>,
delta: u64,
init: Ref,
) -> Result<u64>where
T: Send,
Available on crate feature async
only.
pub async fn grow_async<T>(
&self,
store: impl AsContextMut<Data = T>,
delta: u64,
init: Ref,
) -> Result<u64>where
T: Send,
async
only.Async variant of Table::grow
. Required when using a
ResourceLimiterAsync
.
§Panics
This function will panic when used with a non-async
Store
.
sourcepub fn copy(
store: impl AsContextMut,
dst_table: &Table,
dst_index: u64,
src_table: &Table,
src_index: u64,
len: u64,
) -> Result<()>
pub fn copy( store: impl AsContextMut, dst_table: &Table, dst_index: u64, src_table: &Table, src_index: u64, len: u64, ) -> Result<()>
Copy len
elements from src_table[src_index..]
into
dst_table[dst_index..]
.
§Errors
Returns an error if the range is out of bounds of either the source or destination tables, or if the source table’s element type does not match the destination table’s element type.
§Panics
Panics if store
does not own either dst_table
or src_table
.
Trait Implementations§
impl Copy for Table
Auto Trait Implementations§
impl Freeze for Table
impl RefUnwindSafe for Table
impl Send for Table
impl Sync for Table
impl Unpin for Table
impl UnwindSafe for Table
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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