Struct Component

Source
#[non_exhaustive]
pub struct Component<'a> { pub span: Span, pub id: Option<Id<'a>>, pub name: Option<NameAnnotation<'a>>, pub kind: ComponentKind<'a>, }
Available on crate feature component-model only.
Expand description

A parsed WebAssembly component module.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§span: Span

Where this component was defined

§id: Option<Id<'a>>

An optional identifier this component is known by

§name: Option<NameAnnotation<'a>>

An optional @name annotation for this component

§kind: ComponentKind<'a>

What kind of component this was parsed as.

Implementations§

Source§

impl<'a> Component<'a>

Source

pub fn resolve(&mut self) -> Result<(), Error>

Performs a name resolution pass on this Component, resolving all symbolic names to indices.

The WAT format contains a number of shorthands to make it easier to write, such as inline exports, inline imports, inline type definitions, etc. Additionally it allows using symbolic names such as $foo instead of using indices. This module will postprocess an AST to remove all of this syntactic sugar, preparing the AST for binary emission. This is where expansion and name resolution happens.

This function will mutate the AST of this Component and replace all Index arguments with Index::Num. This will also expand inline exports/imports listed on fields and handle various other shorthands of the text format.

If successful the AST was modified to be ready for binary encoding.

§Errors

If an error happens during resolution, such a name resolution error or items are found in the wrong order, then an error is returned.

Source

pub fn encode(&mut self) -> Result<Vec<u8>, Error>

Encodes this Component to its binary form.

This function will take the textual representation in Component and perform all steps necessary to convert it to a binary WebAssembly component, suitable for writing to a *.wasm file. This function may internally modify the Component, for example:

  • Name resolution is performed to ensure that Index::Id isn’t present anywhere in the AST.

  • Inline shorthands such as imports/exports/types are all expanded to be dedicated fields of the component.

  • Component fields may be shuffled around to preserve index ordering from expansions.

After all of this expansion has happened the component will be converted to its binary form and returned as a Vec<u8>. This is then suitable to hand off to other wasm runtimes and such.

§Errors

This function can return an error for name resolution errors and other expansion-related errors.

Trait Implementations§

Source§

impl<'a> Debug for Component<'a>

Source§

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

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

impl<'a> Parse<'a> for Component<'a>

Source§

fn parse(parser: Parser<'a>) -> Result<Self>

Attempts to parse Self from parser, returning an error if it could not be parsed. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Component<'a>

§

impl<'a> RefUnwindSafe for Component<'a>

§

impl<'a> Send for Component<'a>

§

impl<'a> Sync for Component<'a>

§

impl<'a> Unpin for Component<'a>

§

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