ocaml-ctypes icon indicating copy to clipboard operation
ocaml-ctypes copied to clipboard

Binding flag enums

Open ericmarkmartin opened this issue 4 years ago • 2 comments

In #598, there is some discussion of how to correctly bind a c enum that is used as a flag. In it, @yallop helpfully suggests that the enum construct can be used to obtain a view into the correct width c type. Here, he gives as an example the following code snippet:

let enum_flags = enum "flags" [] ~unexpected:(fun x -> x)

upon which we can build a view that correctly maps a list of flags into the enum and vice-versa. I am unsure if @yallop meant to literal pass enum an empty list or not, but it seems to be the case that if one passes enum an empty list the view created by enum will throw a Not_found upon receiving any value, as it will vacuously never match an element on the lhs of a tuple in the passed alist.

Is there a way we can use the enum construct to correctly identify the c type the compiler will use to represent a given enum without running into this issue?

@lpw25

ericmarkmartin avatar Jul 02 '20 22:07 ericmarkmartin

Thank you for the report, @ericmarkmartin. I think you're quite right about this problem.

I have a proposal for an unbundled interface which I think will provide what you need. I'll write it up soon and add a link here.

yallop avatar Jul 10 '20 19:07 yallop

#652 proposes support for using integer types of unknown sizes, which includes support for binding enums as integer types.

I think it's a more suitable basis for using enums as flagsets than the current enum operation.

yallop avatar Jul 15 '20 06:07 yallop