caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Malcolm Matalka <mmatalka@gmail.com>
Cc: Caml List <caml-list@inria.fr>, ctypes <ctypes@lists.ocaml.org>
Subject: Re: [Caml-list] ctypes - to string or to bigarray?
Date: Mon, 29 Feb 2016 08:41:33 +0000	[thread overview]
Message-ID: <CAAxsn=HRWGpZvXThWwAn24nhj8DX9Tmb9yey6ua06Ca3iRNbVg@mail.gmail.com> (raw)
In-Reply-To: <86oab0hcjq.fsf@gmail.com>

On 28 February 2016 at 19:15, Malcolm Matalka <mmatalka@gmail.com> wrote:
> I'm writing bindings for a library that is just doing I/O and wondering
> what the suggested best practice is around using string or bigarray.  It
> seems like using Bigarray is probably the most versatile interface.
> People can decide to do the copying themselves or just use Bigarray
> everywhere.

Yes, Bigarray is the more versatile choice, and probably the better default.

> Are there any other things to consider?  Suggestions?

Since ctypes bindings are just OCaml programs, one possibility is to
abstract over the choice of storage.  David Sheets's libsodium
bindings are a good example of this approach; for example, here's a
functor which builds bindings for half a dozen functions based on an
abstract "Sodium_storage.S" signature:

    https://github.com/dsheets/ocaml-sodium/blob/95d26a85/lib_gen/sodium_bindings.ml#L64-L76

Elsewhere in the library are implementations of Sodium_storage.S for
Bigarray and for Bytes:

    https://github.com/dsheets/ocaml-sodium/blob/95d26a85/lib/sodium_storage.ml

One pleasant aspect of this approach is that the abstraction over
storage type doesn't cause any loss of type safety.  You can use all
the typed abstraction facilities of OCaml to construct your binding
descriptions, and the generated C code is checked against the headers
for the bound library by the C compiler.

      parent reply	other threads:[~2016-02-29  8:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-28 19:15 Malcolm Matalka
2016-02-28 19:42 ` Gerd Stolpmann
2016-02-29  8:41 ` Jeremy Yallop [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAxsn=HRWGpZvXThWwAn24nhj8DX9Tmb9yey6ua06Ca3iRNbVg@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ctypes@lists.ocaml.org \
    --cc=mmatalka@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).