[Libguestfs] [libnbd PATCH 0/3] Improve type-safety of ocaml/golang getters

Eric Blake eblake at redhat.com
Mon Sep 7 12:37:48 UTC 2020


On 9/7/20 2:37 AM, Richard W.M. Jones wrote:
> On Sat, Sep 05, 2020 at 08:40:57PM -0500, Eric Blake wrote:
>> Natural fallout after my recent testsuite additions that fixed a
>> couple of ocaml bugs in the setters.  However, on at least the OCaml
>> code, I'm not sure what we should do if a newer libnbd ever returns a
>> bit that an older NBD.mli was not expecting at the time the OCaml
>> compiler ran (see below).  I'm also not sure if there is a more
>> efficient way to avoid outputting Val_FOO() converters for types not
>> referenced in REnum/RFlags than my hack of __attribute__((unused)).

With a bit more experimentation, I finally figured out how to avoid that 
hack:

+  if List.exists (
+         function
+         | _, { ret = REnum { enum_prefix = prefix } } ->
+            (prefix = enum_prefix)
+         | _ -> false
+       ) handle_calls then (
+    pr "/* Convert int to OCaml %s.t. */\n" enum_prefix;
...

>>     Maybe we want
>> the generator to produce a full list of 31 variants per 'flags' type,
>> using placeholders for all bits not currently in use, to make it
>> easier to receive an unknown bit from the getter and turn around to
>> re-supply it to the setter?  In such a setup, libnbd would still be
>> rejecting input of out-of-range bits in relation to what libnbd knew
>> at its compilation time.
> 
> While placeholders could be used, a more natural way is to extend the
> flags type (in OCaml) to:
> 
>    type t =
>    | FIXED_NEWSTYLE
>    | NO_ZEROES
>    | UNKNOWN_FLAG of int
> 
> where the unknown (at the time of compilation) flags would be
> encoded as UNKNOWN_FLAG + the bit index of the unknown flag.  eg:
> 
>    [ FIXED_NEWSTYLE; UNKNOWN_FLAG 2 ] => 1 | (1<<2)
>    (1<<3) | 2                         => [ UNKNOWN_FLAG 3; NO_ZEROES ]
> 
> Even though we distribute the OCaml bindings with libnbd it's
> conceivable this could be useful because OCaml bindings are generally
> statically linked into the final binary so you could get a newer
> libnbd.so.0 / old binary with old OCaml bindings scenario.

Yes, that sounds nicer.  I'm experimenting with augmenting flags types 
along those lines now.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




More information about the Libguestfs mailing list