Skip to main content

ServiceModuleResolver

Struct ServiceModuleResolver 

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

Host-owned resolver for script-visible require.

Resolution is performed on demand with the requested path and the current chunk’s source name. The host must return a stable canonical identity when it labels the resolver deterministic. Because the canonical identity is a resolver result, the resolver is called before every cache lookup; a host with expensive I/O or compilation should keep its own source cache. A cache hit skips module execution and discards the newly resolved program.

A module may return zero or one value. Successful results are cached within the calling function’s module cohort; failures can be retried. Module code running in a guest coroutine may yield and resume before its result is cached. Recursive loading is an error, as is requesting an identity that a different coroutine is still loading.

Ordinary require keeps completed writes when a module fails. An enclosing Chunk::install still rolls back guest state when the installation fails; external Rust effects remain outside that rollback.

use rustia::{ModuleSource, RustiaBuilder, ServiceModuleResolver};

let resolver = ServiceModuleResolver::from_fn(|name, _referrer| {
    match name {
        "answer" => Ok(ModuleSource::new("answer", "return 42")),
        _ => Err(rustia::Error::embed(format!("unknown module {name}"))),
    }
});
let mut lua = RustiaBuilder::deterministic().module_resolver(resolver).build()?;
assert_eq!(lua.load("return require('answer')").eval::<i64>()?, 42);

Implementations§

Source§

impl ServiceModuleResolver

Source

pub fn from_fn( resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static, ) -> Self

Host-supplied replay-stable resolver. The determinism assertion is trusted, not verified.

Source

pub fn from_nondeterministic_fn( resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static, ) -> Self

Host-supplied live resolver explicitly labeled nondeterministic.

Trait Implementations§

Source§

impl Clone for ServiceModuleResolver

Source§

fn clone(&self) -> ServiceModuleResolver

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 Debug for ServiceModuleResolver

Source§

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

Formats the value using the given formatter. 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> 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.