Skip to main content

Module native

Module native 

Source
Expand description

The native function binding system - Module, NativeFn, and the IntoNativeFn / FromValue / IntoValue trait machinery.

§How it works

Stdlib functions are plain Rust functions. The IntoNativeFn trait (implemented by a macro for up to 10 arguments) wraps them into NativeFn closures that handle arity checking and argument extraction automatically.

Two variants exist:

  • Infallible (IntoNativeFn<(A1, A2, ...)>) - for functions that return R: IntoValue
  • Fallible (IntoNativeFn<(Fallible<(A1, A2, ...)>,)>) - for functions returning Result<R, Error>
  • Spanned (IntoNativeFn<(Spanned<(A1, A2, ...)>,)>) - fallible functions that also receive the call Span

§Example

use rl_lang::interpreter::{evaluator::Evaluator, native::Module};

fn my_fn(_: &mut Evaluator, x: i64, y: i64) -> i64 { x + y }
Module::new("math").with_function("add", my_fn);

Macros§

impl_into_native_fn 🔒
impl_into_native_fn_spanned 🔒

Structs§

Fallible
Marker type for fallible native functions (returning Result<R, Error>).
Module
A named collection of NativeFns, optionally containing sub-Modules.
Spanned
Marker type for spanned fallible native functions (also receiving the call Span).

Traits§

FromValue
Converts a Value into a typed Rust value, or returns a runtime error. Implemented for i64, f64, String, bool, char, Vec<T>, and Value itself.
IntoNativeFn
Wraps a typed Rust function into a NativeFn with automatic arity checking and argument extraction. Implemented by macro for 0–10 arguments.
IntoValue
Converts a typed Rust value into a Value. Implemented for () (→ Null), i64, f64, String, bool, char, Vec<T>, and Value.
ValueType
Extracts the static TypeAnnotation for a Rust type. Used by IntoValue for Vec<T> to set the array’s items_type.

Type Aliases§

NativeFn
A thread-safe, heap-allocated native function callable from rl.