mirage/ocaml-cstruct

equal/compare for cenums?

Closed this issue · 7 comments

It would be nice to automatically get equal or compare functions for types created from cenums.

avsm commented

is this for satisfying a particular function signature? (i.e. do you need just compare or is equal also definitely needed). Since they are just integers, polymorphic comparison is normally fine, but generating explicit comparators is definitely a good thing.

In short, stuff potentially from cenums -> record -> Alcotest.check .

avsm commented

This sounds like having compare is enough -- I'm a little wary of generating too many functions from the ppx.

Sure, compare is probably fine.

N.B: supposing that you want to compare by id, polymorphic comparison gives the wrong results for cases like this:

[%%cenum
type reversed = 
  | ONE_R [@id 2]
  | TWO_R [@id 1]
  [@@uint8_t]
] 

I'll have the ppx handle this, but that could be a source of bugs.

avsm commented

good catch!

avsm commented

fixed, will be in cstruct 5.0+