Skip to content

Commit

Permalink
Use any-fn crate (#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Jan 21, 2025
1 parent 75b7031 commit ee6d822
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 165 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ readme.workspace = true
repository.workspace = true

[features]
alloc = []
alloc = ["dep:any-fn"]

[dependencies]
any-fn = { version = "0.4.1", optional = true }
heapless = { version = "0.8.0", default-features = false }
stak-vm = { version = "0.7.21", path = "../vm" }

Expand Down
11 changes: 4 additions & 7 deletions native/src/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
//! Native functions dynamically defined.
mod error;
mod function;

pub use self::{
error::DynamicError,
function::{DynamicFunction, IntoDynamicFunction},
};
pub use self::error::DynamicError;
use alloc::boxed::Box;
use any_fn::AnyFn;
use core::{any::Any, cell::RefCell};
use heapless::Vec;
use stak_vm::{Error, Memory, Number, PrimitiveSet, Type};
Expand All @@ -16,13 +13,13 @@ const MAXIMUM_ARGUMENT_COUNT: usize = 16;

/// A dynamic primitive set equipped with native functions in Rust.
pub struct DynamicPrimitiveSet<'a, const N: usize> {
functions: &'a mut [DynamicFunction<'a>],
functions: &'a mut [AnyFn<'a>],
objects: [Option<RefCell<Box<dyn Any>>>; N],
}

impl<'a, const N: usize> DynamicPrimitiveSet<'a, N> {
/// Creates a primitive set.
pub fn new(functions: &'a mut [DynamicFunction<'a>]) -> Self {
pub fn new(functions: &'a mut [AnyFn<'a>]) -> Self {
Self {
functions,
// TODO Garbage-collect foreign objects.
Expand Down
17 changes: 12 additions & 5 deletions native/src/dynamic/error.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
use any_fn::AnyFnError;
use core::{
error,
error::Error,
fmt::{self, Display, Formatter},
};

/// An error.
#[derive(Debug)]
pub enum DynamicError {
/// A object downcast error.
Downcast,
/// An `AnyFn` error.
AnyFn(AnyFnError),
/// A object index error.
ObjectIndex,
/// A virtual machine error.
Vm(stak_vm::Error),
}

impl From<AnyFnError> for DynamicError {
fn from(error: AnyFnError) -> Self {
Self::AnyFn(error)
}
}

impl From<stak_vm::Error> for DynamicError {
fn from(error: stak_vm::Error) -> Self {
Self::Vm(error)
}
}

impl error::Error for DynamicError {}
impl Error for DynamicError {}

impl Display for DynamicError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Self::Downcast => write!(formatter, "cannot downcast object"),
Self::AnyFn(error) => write!(formatter, "{error}"),
Self::ObjectIndex => write!(formatter, "invalid object index"),
Self::Vm(error) => write!(formatter, "{error}"),
}
Expand Down
152 changes: 0 additions & 152 deletions native/src/dynamic/function.rs

This file was deleted.

0 comments on commit ee6d822

Please sign in to comment.