shepmaster / rust-ffi-omnibus

A collection of examples of using code written in Rust from other languages
http://jakegoulding.com/rust-ffi-omnibus/
Other
493 stars 41 forks source link

Suggestions for Ruby and Rust C-API #106

Open misalcedo opened 3 years ago

misalcedo commented 3 years ago

I have an example repo of a bloom filter to learn about FFI. There I compare FFI gem, to C-extension, to pure ruby. In writing the FFI version your docs were invaluable.

I have suggested improvements for ruby:

  1. ffi_lib can fail to load the library on macos as there seem to be some issues with setting the LD_LIBRARY_PATH when using rake and rspec. I worked around this by telling the FFI gem to search via normal means, but fallback to looking at Ruby's load path if that does not work. I am looking into it, but can't seem to find any existing docs on this behavior.
  2. Instead of exposing the auto-pointer class, create a pointer wrapper class so that the class is fully ruby-like to users (no need to expose the binding module. Also, by attaching the functions to the enclosing module callers can use an object-based API or a c-style api under the same namespace.

See https://github.com/misalcedo/bloom/blob/master/lib/bloom_ffi.rb

Only 1 for rust: Using optionals for any of the method-like functions works really well compared to checking for null pointers and mapping the raw pointer to a reference ourselves.

See https://github.com/misalcedo/bloom/blob/master/src/lib.rs