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() -> Self

Create an empty module for the root path.

Source

pub fn with_item<I>(iter: I) -> Self

Construct a new module for the given item.

Source

pub fn with_crate(name: &str) -> Self

Construct a new module for the given crate.

Source

pub fn with_crate_item<I>(name: &str, iter: I) -> Self

Construct a new module for the given crate.

Source

pub fn ty<T>(&mut self) -> Result<(), ContextError>
where T: Named + TypeOf + InstallWith,

Register a type. Registering a type is mandatory in order to register instance functions using that type.

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

§Examples
use runestick::Any;

#[derive(Any)]
struct MyBytes {
    queue: Vec<String>,
}

impl MyBytes {
    fn len(&self) -> usize {
        self.queue.len()
    }
}

// Register `len` without registering a type.
let mut module = runestick::Module::default();
// Note: cannot do this until we have registered a type.
module.inst_fn("len", MyBytes::len)?;

let mut context = runestick::Context::new();
assert!(context.install(&module).is_err());

// Register `len` properly.
let mut module = runestick::Module::default();

module.ty::<MyBytes>()?;
module.inst_fn("len", MyBytes::len)?;

let mut context = runestick::Context::new();
assert!(context.install(&module).is_ok());
Source

pub fn unit<N>(&mut self, name: N) -> Result<(), ContextError>
where N: AsRef<str>,

Construct type information for the unit type.

Registering this allows the given type to be used in Rune scripts when referring to the unit type.

§Examples

This shows how to register the unit type () as nonstd::unit.

use runestick::Module;

let mut module = Module::with_item(&["nonstd"]);
module.unit("unit")?;
Source

pub fn option<N>(&mut self, name: N) -> Result<(), ContextError>

Construct type information for the Option type.

Registering this allows the given type to be used in Rune scripts when referring to the Option type.

§Examples

This shows how to register the Option as nonstd::option::Option.

use runestick::Module;

let mut module = Module::with_crate_item("nonstd", &["option"]);
module.result(&["Option"])?;
Source

pub fn result<N>(&mut self, name: N) -> Result<(), ContextError>

Construct type information for the internal Result type.

Registering this allows the given type to be used in Rune scripts when referring to the Result type.

§Examples

This shows how to register the Result as nonstd::result::Result.

use runestick::Module;

let mut module = Module::with_crate_item("nonstd", &["result"]);
module.result(&["Result"])?;
Source

pub fn generator_state<N>(&mut self, name: N) -> Result<(), ContextError>

Construct the type information for the GeneratorState type.

Registering this allows the given type to be used in Rune scripts when referring to the GeneratorState type.

§Examples

This shows how to register the GeneratorState as nonstd::generator::GeneratorState.

use runestick::Module;

let mut module = Module::with_crate_item("nonstd", &["generator"]);
module.generator_state(&["GeneratorState"])?;
Source

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

Register a function that cannot error internally.

§Examples
fn add_ten(value: i64) -> i64 {
    value + 10
}

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

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

pub fn constant<N, V>(&mut self, name: N, value: V) -> Result<(), ContextError>

Register a constant value, at a crate, module or associated level.

§Examples

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

module.constant(&["TEN"], 10)?; // a global TEN value
module.constant(&["MyType", "TEN"], 10)?; // looks like an associated value
Source

pub fn macro_<N, M, A, O>(&mut self, name: N, f: M) -> Result<(), ContextError>
where M: 'static + Send + Sync + Copy + Fn(&A) -> Result<O, Error>, A: Any, O: Any, N: IntoIterator, N::Item: IntoComponent,

Register a native macro handler.

Source

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

Register a function.

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

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

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

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: InstFnNameHash, Func: InstFn<Args>,

Register an instance function.

§Examples
use runestick::Any;

#[derive(Any)]
struct MyBytes {
    queue: Vec<String>,
}

impl MyBytes {
    fn new() -> Self {
        Self {
            queue: Vec::new(),
        }
    }

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

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

module.ty::<MyBytes>()?;
module.function(&["MyBytes", "new"], MyBytes::new)?;
module.inst_fn("len", MyBytes::len)?;

let mut context = runestick::Context::new();
context.install(&module)?;
Source

pub fn field_fn<N, Func, Args>( &mut self, protocol: Protocol, name: N, f: Func, ) -> Result<(), ContextError>
where N: InstFnNameHash, Func: InstFn<Args>,

Install a protocol function for the given field.

Source

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

Register an instance function.

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

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

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

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

module.ty::<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.