In my interpreter for a small toy language, I have an enum representing values:
#[derive(PartialEq)]
pub enum LangValue {
Number(f64),
String(String),
// etc. etc.
NativeFunction(FunctionMapping),
}
The NativeFunction
type ties to calling Rust functions from within the language:
pub type NativeFuncSignature = fn(Vec<LangValue>) -> LangValue;
#[derive(PartialEq)]
pub struct NativeMapping {
pub name: String,
pub arg_count: usize,
pub func: NativeFuncSignature,
}
I find myself needing to pass NativeFuncSignature
a mutable reference to the struct which controls variables in the language, like this:
pub type NativeFuncSignature = fn(Vec<LangValue>, &mut Variables) -> LangValue;
This fails the PartialEq
derivation for NativeMapping
, but it's required because LangValue
derives from PartialEq
for checking if two values in the language are the same (numbers, strings, etc). It's worth noting I'm not really bothered whether comparisons between NativeFunction
s are accurate, but it has to satisfy PartialEq
in order for LangValue
to.
How can I pass a &mut
reference in a function signature while having it comply with PartialEq
?