Struct Module

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

A collection of functions that can be looked up by type.

Implementations§

Source§

impl Module

Source

pub fn new<I>(path: I) -> Self
where I: IntoIterator, I::Item: AsRef<str>,

Construct a new module.

Source

pub fn ty<N>(&mut self, name: N) -> TypeBuilder<'_, N>
where N: IntoIterator, N::Item: AsRef<str>,

Register a type.

This will allow the type to be used within scripts, using the item named here.

Source

pub fn function<Func, Args, N>( &mut self, name: N, f: Func, ) -> Result<(), ContextError>
where Func: Function<Args>, N: IntoIterator, N::Item: AsRef<str>,

Register a function that cannot error internally.

§Examples
use std::collections::VecDeque;

#[derive(Debug, Clone)]
struct StringQueue(VecDeque<String>);

impl StringQueue {
    fn new() -> Self {
        Self(VecDeque::new())
    }
}

stk::decl_external!(StringQueue);

let mut module = stk::Module::default();

module.function(&["bytes"], StringQueue::new)?;
module.function(&["empty"], || Ok::<_, stk::Error>(()))?;
module.function(&["string"], |a: String| Ok::<_, stk::Error>(()))?;
module.function(&["optional"], |a: Option<String>| Ok::<_, stk::Error>(()))?;
Source

pub fn async_function<Func, Args, N>( &mut self, name: N, f: Func, ) -> Result<(), ContextError>
where Func: AsyncFunction<Args>, N: IntoIterator, N::Item: AsRef<str>,

Register a function.

§Examples
let mut module = stk::Module::default();

module.async_function(&["empty"], || async { () })?;
module.async_function(&["empty_fallible"], || async { Ok::<_, stk::Error>(()) })?;
module.async_function(&["string"], |a: String| async { Ok::<_, stk::Error>(()) })?;
module.async_function(&["optional"], |a: Option<String>| async { Ok::<_, stk::Error>(()) })?;
Source

pub fn raw_fn<F, N>(&mut self, name: N, f: F) -> Result<(), ContextError>
where for<'vm> F: 'static + Copy + Fn(&'vm mut Vm, usize) -> Result<(), VmError> + Send + Sync, N: IntoIterator, N::Item: AsRef<str>,

Register a raw function which interacts directly with the virtual machine.

Source

pub fn async_raw_fn<F, O, N>( &mut self, name: N, f: F, ) -> Result<(), ContextError>
where for<'vm> F: 'static + Copy + Fn(&'vm mut Vm, usize) -> O + Send + Sync, O: Future<Output = Result<(), VmError>>, N: IntoIterator, N::Item: AsRef<str>,

Register a raw function which interacts directly with the virtual machine.

Source

pub fn inst_fn<N, Func, Args>( &mut self, name: N, f: Func, ) -> Result<(), ContextError>
where N: IntoInstFnHash, Func: InstFn<Args>,

Register an instance function.

§Examples
use std::collections::VecDeque;

#[derive(Debug, Clone)]
struct StringQueue(VecDeque<String>);

impl StringQueue {
    fn new() -> Self {
        Self(VecDeque::new())
    }

    fn len(&self) -> usize {
        self.0.len()
    }
}

stk::decl_external!(StringQueue);

let mut module = stk::Module::default();

module.ty(&["StringQueue"]).build::<StringQueue>()?;
module.function(&["StringQueue", "bytes"], StringQueue::new)?;
module.inst_fn("len", StringQueue::len)?;
Source

pub fn async_inst_fn<N, Func, Args>( &mut self, name: N, f: Func, ) -> Result<(), ContextError>
where N: IntoInstFnHash, Func: AsyncInstFn<Args>,

Register an instance function.

§Examples
use std::sync::atomic::AtomicU32;
use std::sync::Arc;

stk::decl_external!(MyType);

#[derive(Clone, Debug)]
struct MyType {
    value: Arc<AtomicU32>,
}

impl MyType {
    async fn test(&self) -> stk::Result<()> {
        Ok(())
    }
}

let mut module = stk::Module::default();

module.ty(&["MyType"]).build::<MyType>()?;
module.async_inst_fn("test", MyType::test)?;

Trait Implementations§

Source§

impl Default for Module

Source§

fn default() -> Module

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Module

§

impl !RefUnwindSafe for Module

§

impl !Send for Module

§

impl !Sync for Module

§

impl Unpin for Module

§

impl !UnwindSafe for Module

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> 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V