1

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 NativeFunctions 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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
deeBo
  • 836
  • 11
  • 24
  • It looks like your question might be answered by the answers of [How to implement PartialEq for an enum?](https://stackoverflow.com/q/36297412/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 19 '20 at 15:51
  • 1
    Specifically, *Sometimes, you want to implement the trait directly*. – Shepmaster May 19 '20 at 15:51
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster May 19 '20 at 15:52
  • Sorry, I think I’ve explained it in a convoluted way, I don’t have an issue with PartialEq on an enum, it’s an issue with PartialEq for a function type signature for which one of the arguments is an &mut reference. I’ll edit my question and include an example when I can get back to my computer. – deeBo May 19 '20 at 15:54
  • That's not the point of the duplicate; please read beyond the title. The duplicate shows you how to implement `PartialEq` yourself when the `derive` version is insufficient. – Shepmaster May 19 '20 at 15:55
  • Ah, my bad, sorry. I was glancing at it on mobile. When I get back to my machine I’ll double check that answer works and mark the question as a dupe if it does – deeBo May 19 '20 at 15:59
  • Yes, thank you, that was the answer. I have flagged this question as a dupe, but I don't have enough rep to mark it definitively. – deeBo May 19 '20 at 16:42

0 Answers0