-
Notifications
You must be signed in to change notification settings - Fork 225
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
Any document on type conversions between Rust and Elixir? #363
Comments
Hi there! To answer some of your questions: BEAM -> Rust
There is however one other scenario: What if you want to be able to handle multiple kinds of terms? Or what if you want to write your own other kind of decoder for your custom Rust datastructure? Rust -> BEAMThis works similarly as above, but here we uset the Encoder trait implementations. Passing arbitrary Rust terms to the BEAMTo be able to work with types not directly supported by Elixir/Erlang (one of the common reasons to write a NIF), we can return a ResourceArc.
Converting your Rust structure to an Elixir structure is definitely possible, but only useful if you want the structure to be edited on the Elixir side. Dealing with bitstrings/binaries in RustThere is extra supports for binaries given by Erlang's NIF wrappers, and Rustler exposes these through the Binary type and related functionality. On the Rust side, as you can already see from the documentation, binaries are usually an Storing arbitrary Elixir terms in RustNow this is where it gets tricky: We need to copy or convert a
However, there is one trick: While we cannot convert the terms to Rust-specific terms, we can create an 'owned environment' and copy the terms (without knowing what exactly they are) to there. You can see a simple implementation I made for this here. There probably is room for improvement. About
|
We actually have started working on some better docs at https://rustler-web.onrender.com/docs. Specifically there is a very early WIP type mapping cheat sheet at https://rustler-web.onrender.com/docs/cheat-sheet. @Qqwy Really nice summary, maybe it would fit into our docs site as an article which goes though type conversions? You are free to open a PR with what you wrote here if you want to. |
A question about #[derive(Debug, NifStruct)]
#[module = "AddStruct"]
struct AddStruct {
lhs: i32,
rhs: i32,
} Is the |
That is left up to the user. |
Thanks I figured in the end. But I was a little confused by this wording in the docs
https://docs.rs/rustler/0.23.0/rustler/derive.NifStruct.html I can make a PR for that little line to make it a bit clearer. |
I'm writing a NIF to expose some Rust library to Elixir world. Rustler is awesome, I can tell. I'm just beginning Rust, and I've been able to expose what I want easily in a day. Totally awesome!
I'm struggling to find what is the equivalent types between the two. Some types may be obvious, like integer to i32 or some other integer types.
Specifically, I can't find how to decode bitstrings (non-UTF-8 strings). A
Vec<char>
orVec<u8>
, or maybe both depending on what I expect from Elixir? There is no clearance, as I take it (at least from my perspective, maybe I'm wrong).Also, how to decode structs? Maps?
Also, I want to return some opaque type from Rust, like a resource that only my library knows what's inside. Can it be done? Or should I use some kind of map/struct/record?
I also saw serde being mentioned for this kind of encoding/decoding job. Is it a necessity? Or can I do it without it?
I may sound like a noob. That's totally correct!
The text was updated successfully, but these errors were encountered: