Open yallop opened 4 years ago
It's similar to: https://fdopen.github.io/ppx_cstubs/opaque/#integers-of-unknown-size-and-signedness
But I've added an explicit function for integers of unknown signedness, instead of implicit conversions from a signed type to an unsigned interface.
I also use custom modules for int8/int16. Otherwise max_int
is wrong and the overflow semantic might be surprising.
Aims and background
This PR adds support for using integer types of unknown properties (size, alignment requirements, signedness).
For example, POSIX specifies that
off_t
is a signed integer type, but does not specify its size. And it specifies thatdev_t
is an integer type, but does not specify its size or signedness. This PR makes it possible to bind and use both types nonetheless.Similarly, the C standard allows implementations to choose the properties of
enum
types. Implementations sometimes also include extensions (such as GCC's-fshort-enums
) that allow users to vary those properties. That implementation freedom can make it difficult to use enums from ctypes (cf. #650). This PR makes it easier to bind and use such types in ctypes code.Design
This PR adds two new functions,
signed
andunsigned
, to theCtypes.TYPE
signature:Users can apply these functions in type-binding functions to bind integer types of unknown properties:
and unpack the bindings to build modules with information about those types:
The signatures for
Dev_t
andFlags
include all the standard integer operations (fromSigned.S
orUnsigned.S
) as well as atyp
value that makes it possible to use the new types to create values, bind functions, etc.Limitations
Since the type binding interface currently requires that functors such as
Types
above are applicative, it is not possible to unpackDev_t
andFlags
inside the functor.