pub struct Module { /* private fields */ }
Expand description
A collection of functions that can be looked up by type.
Implementations§
Source§impl Module
impl Module
Sourcepub fn ty<N>(&mut self, name: N) -> TypeBuilder<'_, N>
pub fn ty<N>(&mut self, name: N) -> TypeBuilder<'_, N>
Register a type.
This will allow the type to be used within scripts, using the item named here.
Sourcepub fn function<Func, Args, N>(
&mut self,
name: N,
f: Func,
) -> Result<(), ContextError>
pub fn function<Func, Args, N>( &mut self, name: N, f: Func, ) -> Result<(), ContextError>
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>(()))?;
Sourcepub fn async_function<Func, Args, N>(
&mut self,
name: N,
f: Func,
) -> Result<(), ContextError>
pub fn async_function<Func, Args, N>( &mut self, name: N, f: Func, ) -> Result<(), ContextError>
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>(()) })?;
Sourcepub fn raw_fn<F, N>(&mut self, name: N, f: F) -> Result<(), ContextError>
pub fn raw_fn<F, N>(&mut self, name: N, f: F) -> Result<(), ContextError>
Register a raw function which interacts directly with the virtual machine.
Sourcepub fn async_raw_fn<F, O, N>(
&mut self,
name: N,
f: F,
) -> Result<(), ContextError>
pub fn async_raw_fn<F, O, N>( &mut self, name: N, f: F, ) -> Result<(), ContextError>
Register a raw function which interacts directly with the virtual machine.
Sourcepub fn inst_fn<N, Func, Args>(
&mut self,
name: N,
f: Func,
) -> Result<(), ContextError>where
N: IntoInstFnHash,
Func: InstFn<Args>,
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)?;
Sourcepub fn async_inst_fn<N, Func, Args>(
&mut self,
name: N,
f: Func,
) -> Result<(), ContextError>where
N: IntoInstFnHash,
Func: AsyncInstFn<Args>,
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more