Skip to main content

Host

Struct Host 

Source
pub struct Host { /* private fields */ }
Expand description

A Context and a Registry paired up, ready to call functions.

This is the application side of a scripting solution: register your native types and functions, install your scripts, then call in.

The registry is shared through an Arc, so forks and worker threads all see the same definitions. It must be complete before the first call, since a registry in use can no longer be modified.

Implementations§

Source§

impl Host

Source

pub fn new(context: Context, registry: Arc<Registry>) -> Host

Pairs a context with a registry.

Source

pub fn fork(&self) -> Host

Builds a host with a fresh context of the same capacities, sharing this registry.

Source

pub fn push_global(self) -> Result<ID<Host>, Host>

Pushes this host onto the current thread’s global stack and returns its id.

Gives the host back when the stack is already borrowed. Useful for code that cannot pass a host down by argument, such as a native callback.

Source

pub fn pop_global() -> Option<Host>

Takes the topmost host off the current thread’s global stack.

Source

pub fn remove_global(id: ID<Host>) -> Option<Host>

Takes a specific host off the current thread’s global stack.

Source

pub fn with_global<T>(f: impl FnOnce(&mut Host) -> T) -> Option<T>

Runs f with the topmost host of the current thread.

Returns None when the stack is empty or already borrowed.

Source

pub fn context(&mut self) -> &mut Context

Returns the context.

Source

pub fn registry(&self) -> &Registry

Returns the registry.

Source

pub fn context_and_registry(&mut self) -> (&mut Context, &Registry)

Returns context and registry at once, as function bodies need both.

Source

pub fn find_function( &self, name: &str, module_name: &str, type_name: Option<&str>, ) -> Option<Arc<Function>>

Looks a function up by name, module and optionally owning type.

Source

pub fn call_function<O, I>( &mut self, name: &str, module_name: &str, type_name: Option<&str>, ) -> Option<HostFunctionCall<'_, I, O>>

Prepares a call to a function, matching it on argument and result types as well as its name.

Returns None when no function matches. Call HostFunctionCall::run on the result with the arguments.

let (result,) = host
    .call_function::<(i32,), _>("add", "lib", None)
    .unwrap()
    .run((40_i32, 2_i32));

Auto Trait Implementations§

§

impl !RefUnwindSafe for Host

§

impl !UnwindSafe for Host

§

impl Freeze for Host

§

impl Send for Host

§

impl Sync for Host

§

impl Unpin for Host

§

impl UnsafeUnpin for Host

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

Source§

unsafe fn finalize_raw(data: *mut ())

Drops the value stored at data in place. 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.