pub struct Component { /* private fields */ }
Available on crate feature component-model only.
Expand description

A compiled WebAssembly Component.

Implementations§

source§

impl Component

source

pub fn new(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Component>

Available on crate features cranelift or winch only.

Compiles a new WebAssembly component from the in-memory wasm image provided.

source

pub fn from_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Component>

Available on crate features cranelift or winch only.

Compiles a new WebAssembly component from a wasm file on disk pointed to by file.

source

pub fn from_binary(engine: &Engine, binary: &[u8]) -> Result<Component>

Available on crate features cranelift or winch only.

Compiles a new WebAssembly component from the in-memory wasm image provided.

source

pub unsafe fn deserialize( engine: &Engine, bytes: impl AsRef<[u8]> ) -> Result<Component>

Same as Module::deserialize, but for components.

Note that the file referenced here must contain contents previously produced by Engine::precompile_component or Component::serialize.

For more information see the Module::deserialize method.

source

pub unsafe fn deserialize_file( engine: &Engine, path: impl AsRef<Path> ) -> Result<Component>

Same as Module::deserialize_file, but for components.

For more information see the Component::deserialize and Module::deserialize_file methods.

source

pub fn serialize(&self) -> Result<Vec<u8>>

Same as Module::serialize, except for a component.

Note that the artifact produced here must be passed to Component::deserialize and is not compatible for use with Module.

source

pub fn resources_required(&self) -> Option<ResourcesRequired>

Returns a summary of the resources required to instantiate this Component.

Note that when a component imports and instantiates another component or core module, we cannot determine ahead of time how many resources instantiating this component will require, and therefore this method will return None in these scenarios.

Potential uses of the returned information:

  • Determining whether your pooling allocator configuration supports instantiating this component.

  • Deciding how many of which Component you want to instantiate within a fixed amount of resources, e.g. determining whether to create 5 instances of component X or 10 instances of component Y.

Example
use wasmtime::{Config, Engine, component::Component};

let mut config = Config::new();
config.wasm_multi_memory(true);
config.wasm_component_model(true);
let engine = Engine::new(&config)?;

let component = Component::new(&engine, &r#"
    (component
        ;; Define a core module that uses two memories.
        (core module $m
            (memory 1)
            (memory 6)
        )

        ;; Instantiate that core module three times.
        (core instance $i1 (instantiate (module $m)))
        (core instance $i2 (instantiate (module $m)))
        (core instance $i3 (instantiate (module $m)))
    )
"#)?;

let resources = component.resources_required()
    .expect("this component does not import any core modules or instances");

// Instantiating the component will require allocating two memories per
// core instance, and there are three instances, so six total memories.
assert_eq!(resources.num_memories, 6);
assert_eq!(resources.max_initial_memory_size, Some(6));

// The component doesn't need any tables.
assert_eq!(resources.num_tables, 0);
assert_eq!(resources.max_initial_table_size, None);

Trait Implementations§

source§

impl Clone for Component

source§

fn clone(&self) -> Component

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

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.