pub fn import_function<Args, F: ImportableFn<Args> + Send + 'static>(
        self,
        name: impl ToString,
        value: F,
    ) -> Self {
        let env = Env::new(self.garbage.clone(), ());

        if F::has_arg() {
            let f = if F::has_return() {
                let wrapped = move |env: &Env<()>, ptr: u32, len: u32| -> u64 {
                    let mut buffer = env.message_buffer();
                    let r = value
                        .call_with_input(&mut buffer, ptr as usize, len as usize)
                        .unwrap()
                        .map(|p| p.0)
                        .unwrap_or(0);
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                    r
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            } else {
                let wrapped = move |env: &Env<()>, ptr: u32, len: u32| {
                    let mut buffer = env.message_buffer();
                    value
                        .call_with_input(&mut buffer, ptr as usize, len as usize)
                        .unwrap();
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            };
            self.import(name, f)
        } else {
            let f = if F::has_return() {
                let wrapped = move |env: &Env<()>| -> u64 {
                    let mut buffer = env.message_buffer();
                    let r = value
                        .call_without_input(&mut buffer)
                        .unwrap()
                        .map(|p| p.0)
                        .unwrap_or(0);
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                    r
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            } else {
                let wrapped = move |env: &Env<()>| {
                    let mut buffer = env.message_buffer();
                    value.call_without_input(&mut buffer).unwrap();
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            };
            self.import(name, f)
        }
    }
/// Import a function defined in the host into the guest. The function's
/// arguments and return type must all be serializable.
Import a function defined in the host into the guest. The function's
arguments and return type must all be serializable.
Import a function defined in the host into the guest . The function ' s arguments and return type must all be serializable .
pub fn import_function<Args, F: ImportableFn<Args> + Send + 'static>(
        self,
        name: impl ToString,
        value: F,
    ) -> Self {
        let env = Env::new(self.garbage.clone(), ());
        if F::has_arg() {
            let f = if F::has_return() {
                let wrapped = move |env: &Env<()>, ptr: u32, len: u32| -> u64 {
                    let mut buffer = env.message_buffer();
                    let r = value
                        .call_with_input(&mut buffer, ptr as usize, len as usize)
                        .unwrap()
                        .map(|p| p.0)
                        .unwrap_or(0);
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                    r
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            } else {
                let wrapped = move |env: &Env<()>, ptr: u32, len: u32| {
                    let mut buffer = env.message_buffer();
                    value
                        .call_with_input(&mut buffer, ptr as usize, len as usize)
                        .unwrap();
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            };
            self.import(name, f)
        } else {
            let f = if F::has_return() {
                let wrapped = move |env: &Env<()>| -> u64 {
                    let mut buffer = env.message_buffer();
                    let r = value
                        .call_without_input(&mut buffer)
                        .unwrap()
                        .map(|p| p.0)
                        .unwrap_or(0);
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                    r
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            } else {
                let wrapped = move |env: &Env<()>| {
                    let mut buffer = env.message_buffer();
                    value.call_without_input(&mut buffer).unwrap();
                    env.garbage.lock().unwrap().extend(buffer.garbage.drain(..));
                };
                Function::new_native_with_env(&self.store, env, wrapped)
            };
            self.import(name, f)
        }
    }
pub fn import_function < Args , F : ImportableFn < Args > + Send + ' static > ( self , name : impl ToString , value : F , ) -> Self { let env = Env :: new ( self . garbage . clone ( ) , ( ) ) ; if F :: has_arg ( ) { let f = if F :: has_return ( ) { let wrapped = move | env : & Env < ( ) > , ptr : u32 , len : u32 | -> u64 { let mut buffer = env . message_buffer ( ) ; let r = value . call_with_input ( & mut buffer , ptr as usize , len as usize ) . unwrap ( ) . map ( | p | p . 0 ) . unwrap_or ( 0 ) ; env . garbage . lock ( ) . unwrap ( ) . extend ( buffer . garbage . drain ( .. ) ) ; r } ; Function :: new_native_with_env ( & self . store , env , wrapped ) } else { let wrapped = move | env : & Env < ( ) > , ptr : u32 , len : u32 | { let mut buffer = env . message_buffer ( ) ; value . call_with_input ( & mut buffer , ptr as usize , len as usize ) . unwrap ( ) ; env . garbage . lock ( ) . unwrap ( ) . extend ( buffer . garbage . drain ( .. ) ) ; } ; Function :: new_native_with_env ( & self . store , env , wrapped ) } ; self . import ( name , f ) } else { let f = if F :: has_return ( ) { let wrapped = move | env : & Env < ( ) > | -> u64 { let mut buffer = env . message_buffer ( ) ; let r = value . call_without_input ( & mut buffer ) . unwrap ( ) . map ( | p | p . 0 ) . unwrap_or ( 0 ) ; env . garbage . lock ( ) . unwrap ( ) . extend ( buffer . garbage . drain ( .. ) ) ; r } ; Function :: new_native_with_env ( & self . store , env , wrapped ) } else { let wrapped = move | env : & Env < ( ) > | { let mut buffer = env . message_buffer ( ) ; value . call_without_input ( & mut buffer ) . unwrap ( ) ; env . garbage . lock ( ) . unwrap ( ) . extend ( buffer . garbage . drain ( .. ) ) ; } ; Function :: new_native_with_env ( & self . store , env , wrapped ) } ; self . import ( name , f ) } }
Import a function defined in the host into the guest.
Import a function defined in the host into the guest .
self
name
impl ToString
value
F