[ocaml-ctypes] Unsigned int smaller than 32 bits

Jeremy Yallop yallop at gmail.com
Sun Dec 22 02:12:51 GMT 2013


On 22 December 2013 01:47, Daniel Bünzli <daniel.buenzli at erratique.ch> wrote:
> Is there any particular reason why signed int smaller than 32 bits are accessed as ocaml ints but unsigned ints smaller than 32 bits are not ?

Well, there's a rationale for uint16_t being a distinct type, since
the C (signed) int type can be as small as 16 bits.  I don't see why
uint8_t needs to be a distinct type, though.

> P.S. Already tired of writing:
>
> let int_as_uint8_t =
>   view ~read:Unsigned.UInt8.to_int ~write:Unsigned.UInt8.of_int uint8_t

Yes, this is pretty annoying.  Perhaps we should have multiple views
for a single C type in the Ctypes interface; something like this:

   module Uint8 :
   sig
     val t : uint8_t typ   (* access C uint8_t values using the OCaml
uint8_t type *)
     val int : int typ       (* access C uint8_t values using the
OCaml int type *)
   end

Then Ctypes.Uint8.t would behave like the current Ctypes.uint8_t, and
Ctypes.Uint8.int would behave like your int_as_uint8_t (except that it
could be made a little more efficient).

Feedback welcome.  If this approach is reasonable then it could also
might also lead to resolutions for the following issues:

    Expose arithmetic operations for POSIX arithmetic types
    https://github.com/ocamllabs/ocaml-ctypes/issues/87

    ptrdiff_t ?
    https://github.com/ocamllabs/ocaml-ctypes/issues/117

Jeremy


More information about the Ctypes mailing list