add Function to Value
This commit is contained in:
parent
fe15752d04
commit
0b5e9a5f44
29
src/types.rs
29
src/types.rs
|
|
@ -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)]
|
||||
pub enum ValueInner {
|
||||
|
|
@ -6,6 +6,7 @@ pub enum ValueInner {
|
|||
Number(i64),
|
||||
String(String),
|
||||
Table(Table),
|
||||
Function(Function),
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user