Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have a FFI chapter? #328

Open
budziq opened this issue Oct 12, 2017 · 9 comments
Open

have a FFI chapter? #328

budziq opened this issue Oct 12, 2017 · 9 comments

Comments

@budziq
Copy link
Collaborator

budziq commented Oct 12, 2017

We already have a budding build tools chapter mostly established to showcase build time crates like cc. My feeling is that we could go further with this formula and expand it to (or suplement it with) a full blown chapter dedicated to various rust ffi stories (calling rust, calling from rust, generating bindings).

My main concern is that the ffi stuff might not yeld well to the short snippet problem/solution approach of the cookbook, rather requiring a more narrative driven form (please see few nice ffi guides/tutorials below).

Otherwise it might be a relatively good topic for a standalone book as there is certainly enough material!

Sorry for the pings guys but I wanted to pick your brains on the matter! And I certainly do not want to step on anyone's toes or double the effort.

@Michael-F-Bryan
Copy link
Contributor

It'd probably take more than a couple snippets to explain how to do FFI without shooting yourself in the foot. I originally started my little guide to help jot down the things I learned while integrating a Rust DLL into a large legacy application at work and (at the time) couldn't find any resources which explain FFI further than a trivial hello world.

Off the top of my head here are a couple topics you may want to mention:

  • The build system and getting your code to actually run
    • the cc crate
    • Bindgen and cbindgen
  • Error handling - KISS
  • Passing around Plain Old Data types (ints, simple structs, etc)
  • Strings, Arrays, and handling generics
  • Best practices and anti-patterns (i.e. mem::transmute() is almost never the answer)
  • Memory management and cleaning up resources properly
  • Asynchronous tasks (e.g. spin a thread off in the background and get the result back once it's done)

I'd be more than happy to help out on this! Just this week I had to revisit that Rust module and ended up rewriting the interface code because the way it did things was quite convoluted and didn't handle errors or cancellation well.

@budziq
Copy link
Collaborator Author

budziq commented Oct 12, 2017

Thanks! I think that we'll need to decide first if cookbook is a good place for such topic. Or if it is even possible to provide ffi related snippets without all the narrative (ie. avoid making it a loaded footgun).

@fitzgen
Copy link

fitzgen commented Oct 12, 2017

Agreed that a comprehensive creating-a-safe-wrapper-around-FFI example might be a bit long.

It would be nice if we identified something that was small enough to fit though.

Also exposing an API to another language vs using and wrapping a C/C++ library are probably two different cook book entries. The tasks both involve FFI but are different in all other ways.

@Michael-F-Bryan
Copy link
Contributor

Hmm, is there anything special the readme/getting started guide for cc, pyo3, or bindgen couldn't show you, yet would still fit into a cookbook recipe?

@budziq
Copy link
Collaborator Author

budziq commented Oct 12, 2017

Hmm, is there anything special the readme/getting started guide

When discussing this with @aturon, he came up with ideas of cookbook being companion to RBE and a kind of a signpost to vetted crates ecosystem giving the sense extended standard library. So in this regard a small measure of duplication (as long as we are solving specific real life problems) would still be acceptable.

@Michael-F-Bryan
Copy link
Contributor

I've done a lot of work rewriting chunks of my FFI guide. It's more of an extended tutorial than a collection of self-contained snippets, but what are your thoughts on pointing to something like that given the complexity and large number of footguns commonly associated with FFI?

@budziq
Copy link
Collaborator Author

budziq commented Nov 4, 2017

@Michael-F-Bryan as you've noted there is a above average density of footguns in ffi problem space and not really yielding to the short self-contained snippets idea.
I think that we first and foremost need a comprehensive resource / guide like your book (I really like it!) and cookbook could contain some clearcut examples (We could steal some of the examples from exercises near the end of your book ;) ).

TBH we need to think more about the cookbook inclusion policy and interlinking between the resources I'd love some input on this matter!

@Michael-F-Bryan
Copy link
Contributor

TBH we need to think more about the cookbook inclusion policy and interlinking between the resources I'd love some input on this matter!

You raise a good point there. We don't want to start pointing to 3rd party resources (mine included!) which aren't up to the high standards we hold for the rest of the Rust project.

cookbook could contain some clearcut examples (We could steal some of the examples from exercises near the end of your book ;) )

If you feel they're relevant then help yourself. Those examples are probably not what you want to include though because they're all examples of UB or footguns, which are then discussed in the "solutions" section. Deliberately incorrect code is probably not something you want to include in the cookbook 😜

@budziq
Copy link
Collaborator Author

budziq commented Nov 5, 2017

they're all examples of UB or footguns, which are then discussed in the "solutions" section.

Ha, serves me right! I've just visually skimmed through the last chapter without reading a single word or a code line :F

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants