yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
369 stars 95 forks source link

Binding flag enums #650

Open ericmarkmartin opened 4 years ago

ericmarkmartin commented 4 years ago

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

yallop commented 4 years ago

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 commented 4 years ago

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.