I'm wrapping a C function, a getter that returns a `{const void*, size_t}` 
struct pointing into memory managed by the C library. The obvious wrapper proc 
would copy the bytes into a `seq[byte]` and return that.

However, this C library is a high-performance key-value store (a relative of 
LMDB) that gets a lot of its speed by using memory-mapping and avoiding 
copying. So I want to avoid copying the bytes.

The only collection I've found that lets me do this is `openarray`, and I've 
found the functions that let me create one from a `cstring` and a length, but 
the manual says `openarray` can only be used as a parameter. That doesn't seem 
to be enforced, however: I can declare an object type containing an `openarray` 
without errors.

I'm thinking of doing something like this:
    
    
    type Transaction* = ref object
      ...
    type Result* = object
      bytes*: openarray[byte]
      owner: Transaction
    
    proc get(t: Transaction, key: string): Result =
      ...
    
    
    Run

The `owner` field of the `Result` holds onto the `Transaction` object, keeping 
it alive so the `bytes` remain valid. (The C API only guarantees the 
availability of the data during the transaction it was accessed in.)

Is this OK, or something that could cause trouble?

—Jens

Reply via email to