I’m curious how to model a binding to a C API that uses void*s to represent uninterpreted data blobs. Specifically, I’m working with a key-value store (similar to Berkeley DB) that stores keys and values as blobs. Here’s one function:
fdb_status fdb_get_kv(fdb_kvs_handle *handle,
const void *key, size_t keylen,
void **value_out, size_t *valuelen_out);
This function allocates the value blob on the heap. value_out is an ‘out’ parameter that on return will point to the value, and valuelen_out will contain the length of the value. The caller is responsible for freeing the value.
In Swift 2, the corresponding wrapper method would return the value, but as what type? NSData is a fairly obvious choice, especially since I can initialize it with bytesNoCopy: and avoid having to copy the value. But NSData doesn’t seem like a very Swift-y type, and I’m also thinking ahead to cross-platform code that probably won’t have Foundation types available.
So maybe an Array[UInt8]? But how do I create one of those from an UnsafeMutablePointer<Void>, which is what value_out comes out as in Swift? (And is it possible to create one that will adopt the heap block without copying?)
(An obvious question is “what will the caller interpret the value as?” At this level the API is agnostic. Some callers might store a UTF-8 string, some might store JPEG data, some might store a structured binary encoding like a protocol buffer.)
—Jens