add Function to Value

This commit is contained in:
numzero 2025-04-19 10:42:30 +03:00
parent fe15752d04
commit 0b5e9a5f44

View File

@ -1,4 +1,4 @@
use std::{cell::RefCell, collections::HashMap, hash::Hash, rc::Rc}; use std::{cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash, rc::Rc};
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ValueInner { pub enum ValueInner {
@ -6,6 +6,7 @@ pub enum ValueInner {
Number(i64), Number(i64),
String(String), String(String),
Table(Table), Table(Table),
Function(Function),
} }
macro_rules! impl_from { macro_rules! impl_from {
@ -75,3 +76,29 @@ impl Table {
} }
} }
} }
#[derive(Clone)]
pub struct Function {
name: &'static str,
inner: Rc<dyn Fn(&[Value]) -> Value>,
}
impl Debug for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<function {}>", self.name)
}
}
impl PartialEq for Function {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.inner.as_ref(), other.inner.as_ref())
}
}
impl Eq for Function {}
impl Hash for Function {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::ptr::hash(self.inner.as_ref(), state)
}
}