pub struct NativeRegistry<const N: usize> { /* private fields */ }Expand description
Registry for native functions.
This struct holds the registered native functions and provides lookup functionality for the evaluator.
§Example
use grift_eval::{NativeRegistry, Lisp, ArenaIndex, ArenaResult};
fn my_add<const N: usize>(lisp: &Lisp<N>, args: ArenaIndex) -> ArenaResult<ArenaIndex> {
use grift_eval::FromLisp;
let a = isize::from_lisp(lisp, lisp.car(args)?)?;
let b = isize::from_lisp(lisp, lisp.car(lisp.cdr(args)?)?)?;
lisp.number(a + b)
}
let mut registry: NativeRegistry<1000> = NativeRegistry::new();
registry.register("my-add", my_add);Implementations§
Source§impl<const N: usize> NativeRegistry<N>
impl<const N: usize> NativeRegistry<N>
Sourcepub const fn new() -> NativeRegistry<N>
pub const fn new() -> NativeRegistry<N>
Create a new empty native function registry.
Sourcepub fn register(
&mut self,
name: &'static str,
func: fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>,
)
pub fn register( &mut self, name: &'static str, func: fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>, )
Sourcepub fn lookup(
&self,
name: &str,
) -> Option<fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>>
pub fn lookup( &self, name: &str, ) -> Option<fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>>
Look up a native function by name.
Returns None if the function is not registered.
Sourcepub fn lookup_by_id(
&self,
id: usize,
) -> Option<fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>>
pub fn lookup_by_id( &self, id: usize, ) -> Option<fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>>
Look up a native function by its ID (index).
Returns None if the ID is out of range.
Sourcepub fn name_by_id(&self, id: usize) -> Option<&'static str>
pub fn name_by_id(&self, id: usize) -> Option<&'static str>
Get the name of a native function by its ID.
Trait Implementations§
Source§impl<const N: usize> Default for NativeRegistry<N>
impl<const N: usize> Default for NativeRegistry<N>
Source§fn default() -> NativeRegistry<N>
fn default() -> NativeRegistry<N>
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl<const N: usize> Freeze for NativeRegistry<N>
impl<const N: usize> RefUnwindSafe for NativeRegistry<N>
impl<const N: usize> Send for NativeRegistry<N>
impl<const N: usize> Sync for NativeRegistry<N>
impl<const N: usize> Unpin for NativeRegistry<N>
impl<const N: usize> UnwindSafe for NativeRegistry<N>
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