Skip to main content

NativeRegistry

Struct NativeRegistry 

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

Source

pub const fn new() -> NativeRegistry<N>

Create a new empty native function registry.

Source

pub fn register( &mut self, name: &'static str, func: fn(&Lisp<N>, ArenaIndex) -> Result<ArenaIndex, ArenaError>, )

Register a native function.

§Panics

Panics if the registry is full (MAX_NATIVE_FUNCTIONS exceeded).

Source

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.

Source

pub fn names(&self) -> impl Iterator<Item = &'static str>

Get all registered function names.

Source

pub fn len(&self) -> usize

Get the number of registered functions.

Source

pub fn is_empty(&self) -> bool

Check if the registry is empty.

Source

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.

Source

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>

Source§

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