Struct Function

Source
pub struct Function<'a> {
    pub name: String,
    pub body: Builder<'a>,
    pub locals: Vec<ValType>,
    pub type_index: FunctionTypeIndex,
    pub index: u32,
    pub export: Option<String>,
}

Fields§

§name: String§body: Builder<'a>§locals: Vec<ValType>§type_index: FunctionTypeIndex§index: u32§export: Option<String>

Implementations§

Source§

impl<'a> Function<'a>

Source

pub fn push(&mut self, expr: impl Expr<'a>) -> &mut Self

Examples found in repository?
examples/gc.rs (line 85)
71fn main() -> anyhow::Result<()> {
72    let mut module = Module::new();
73    let field = field_type(StorageType::Val(ValType::I32), true);
74    let idx = module.struct_type([field, field]);
75    let t = ValType::Ref(RefType {
76        nullable: false,
77        heap_type: HeapType::Concrete(idx.index()),
78    });
79
80    let mut locals = TypeList::new();
81    let a = locals.push(t);
82    let b = locals.push(t);
83    module
84        .func("add", locals, [], [])
85        .push(Add(a, b, idx))
86        .export("add");
87
88    let mut locals = TypeList::new();
89    let a = locals.push(t);
90    module
91        .func("sum", locals, [ValType::I32], [])
92        .push(Sum(a, idx))
93        .export("sum")
94        .index();
95
96    let mut locals = TypeList::new();
97    let a = locals.push(ValType::I32);
98    let b = locals.push(ValType::I32);
99    module
100        .func("init", locals, [t], [])
101        .push(Init(a, b, idx))
102        .export("init");
103
104    module.save("gc.wasm")?;
105    Ok(())
106}
Source

pub fn export(&mut self, name: impl Into<String>) -> &mut Self

Examples found in repository?
examples/gc.rs (line 86)
71fn main() -> anyhow::Result<()> {
72    let mut module = Module::new();
73    let field = field_type(StorageType::Val(ValType::I32), true);
74    let idx = module.struct_type([field, field]);
75    let t = ValType::Ref(RefType {
76        nullable: false,
77        heap_type: HeapType::Concrete(idx.index()),
78    });
79
80    let mut locals = TypeList::new();
81    let a = locals.push(t);
82    let b = locals.push(t);
83    module
84        .func("add", locals, [], [])
85        .push(Add(a, b, idx))
86        .export("add");
87
88    let mut locals = TypeList::new();
89    let a = locals.push(t);
90    module
91        .func("sum", locals, [ValType::I32], [])
92        .push(Sum(a, idx))
93        .export("sum")
94        .index();
95
96    let mut locals = TypeList::new();
97    let a = locals.push(ValType::I32);
98    let b = locals.push(ValType::I32);
99    module
100        .func("init", locals, [t], [])
101        .push(Init(a, b, idx))
102        .export("init");
103
104    module.save("gc.wasm")?;
105    Ok(())
106}
Source

pub fn builder(&mut self) -> &mut Builder<'a>

Source

pub fn with_builder(&mut self, f: impl FnOnce(&mut Builder<'_>)) -> &mut Self

Source

pub fn index(&self) -> FunctionIndex

Examples found in repository?
examples/gc.rs (line 94)
71fn main() -> anyhow::Result<()> {
72    let mut module = Module::new();
73    let field = field_type(StorageType::Val(ValType::I32), true);
74    let idx = module.struct_type([field, field]);
75    let t = ValType::Ref(RefType {
76        nullable: false,
77        heap_type: HeapType::Concrete(idx.index()),
78    });
79
80    let mut locals = TypeList::new();
81    let a = locals.push(t);
82    let b = locals.push(t);
83    module
84        .func("add", locals, [], [])
85        .push(Add(a, b, idx))
86        .export("add");
87
88    let mut locals = TypeList::new();
89    let a = locals.push(t);
90    module
91        .func("sum", locals, [ValType::I32], [])
92        .push(Sum(a, idx))
93        .export("sum")
94        .index();
95
96    let mut locals = TypeList::new();
97    let a = locals.push(ValType::I32);
98    let b = locals.push(ValType::I32);
99    module
100        .func("init", locals, [t], [])
101        .push(Init(a, b, idx))
102        .export("init");
103
104    module.save("gc.wasm")?;
105    Ok(())
106}

Trait Implementations§

Source§

impl<'a> Clone for Function<'a>

Source§

fn clone(&self) -> Function<'a>

Returns a duplicate 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<'a> Debug for Function<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Function<'a>

§

impl<'a> RefUnwindSafe for Function<'a>

§

impl<'a> Send for Function<'a>

§

impl<'a> Sync for Function<'a>

§

impl<'a> Unpin for Function<'a>

§

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