Struct ImportObject

Source
pub struct ImportObject {
    pub allow_missing_functions: bool,
    /* private fields */
}
Expand description

All of the import data used when instantiating.

It’s suggested that you use the imports! macro instead of creating an ImportObject by hand.

§Usage:

let import_object = imports! {
    "env" => {
        "foo" => func!(foo),
    },
};

fn foo(_: &mut Ctx, n: i32) -> i32 {
    n
}

Fields§

§allow_missing_functions: bool

Allow missing functions to be generated and instantiation to continue when required functions are not provided.

Implementations§

Source§

impl ImportObject

Source

pub fn new() -> Self

Create a new ImportObject.

Source

pub fn new_with_data<F>(state_creator: F) -> Self
where F: Fn() -> (*mut c_void, fn(*mut c_void)) + 'static + Send + Sync,

Create a new ImportObject which generates data from the provided state creator.

Source

pub fn register<S, N>( &mut self, name: S, namespace: N, ) -> Option<Box<dyn LikeNamespace>>
where S: Into<String>, N: LikeNamespace + Send + 'static,

Register anything that implements LikeNamespace as a namespace.

§Usage:
fn register(instance: Instance, namespace: Namespace) {
    let mut import_object = ImportObject::new();

    import_object.register("namespace0", instance);
    import_object.register("namespace1", namespace);
    // ...
}
Source

pub fn with_namespace<Func, InnerRet>( &self, namespace: &str, f: Func, ) -> Option<InnerRet>
where Func: FnOnce(&(dyn LikeNamespace + Send)) -> InnerRet, InnerRet: Sized,

Apply a function on the namespace if it exists If your function can fail, consider using maybe_with_namespace

Source

pub fn maybe_with_namespace<Func, InnerRet>( &self, namespace: &str, f: Func, ) -> Option<InnerRet>
where Func: FnOnce(&(dyn LikeNamespace + Send)) -> Option<InnerRet>, InnerRet: Sized,

The same as with_namespace but takes a function that may fail

§Usage:
fn get_export(imports: &ImportObject, namespace: &str, name: &str) -> Option<Export> {
    imports.maybe_with_namespace(namespace, |ns| ns.get_export(name))
}
Source

pub fn contains_namespace(&self, name: &str) -> bool

Returns true if the ImportObject contains namespace with the provided name.

Trait Implementations§

Source§

impl Clone for ImportObject

Source§

fn clone(&self) -> ImportObject

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 Extend<(String, String, Export)> for ImportObject

Source§

fn extend<T: IntoIterator<Item = (String, String, Export)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl IntoIterator for ImportObject

Source§

type IntoIter = ImportObjectIterator

Which kind of iterator are we turning this into?
Source§

type Item = (String, String, Export)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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.