Rustler return and pass reference from Rust to Elixir and back to Rust

The starting point: The Erlang ODBC client has a “bug” where it only allows 4096 bytes to be returned per cell. The Rust odbc client has no such limitations.

The reasoning: I’d like to avoid Rust for the web layer, since that would result in a lot of training for my peers.

The (probably insane) idea: Use Rustler to provide a custom “odbc driver” that can be used from Elixir.

The problem: I’d need to store the established connections somewhere in Elixir in order to pass them to the query functions and I’m not quite sure if this would work with Rustlers unsafe functions.

So the questions are:

  1. How do I return a Rust reference to a “thing” to Elixir in order to pass it again to Rust later on?
  2. Is there anything that disqualifies this idea categorically (except from the fact that it is probably a huge effort)
2 Likes

Use NIF resource for that, you probably will need to dig in Rustler docs how to do that in Rust.

2 Likes

@hauleth 's suggestion is on point.

If you need an example, you could look at wasmex (GitHub - tessi/wasmex: Execute WebAssembly / WASM from Elixir).

For example, a WebAssembly module instance has an elixir representation with an attached Rust struct. It is passed down in some methods defined here to the Rust-layer.

In Rust-land, we override the Wasmex::Native module with the respective rust functions taking that rust-struct reference.

In instance.rs you can see how to attach structs (instance in my case) to elixir objects (new_from_bytes) and how to extract structs from elixir objects (e.g. function_export_exists).

3 Likes

Rustler 0.22.0-rc makes this a bit easier. It’s not very well documented but I’ve made it work mostly easily.

Could you give me a pointer (pun fully intended :smiley:), Dimitar?
I. e. what exactly is it that the new version will make easier?

Here’s something that should work with minimal changes:

use rustler::resource::ResourceArc;
use rustler::{Encoder, Env, Term};

rustler::atoms! { error, ok, }

type MyRustReturnType = YOUR_RUST_TYPE_HERE;

enum MyResult {
    Success(ResourceArc<MyRustReturnType>),
    Failure(String),
}

impl<'a> Encoder for MyResult {
    fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
        match self {
            MyResult::Success(arc) => (ok(), arc).encode(env),
            MyResult::Failure(msg) => (error(), msg).encode(env),
        }
    }
}

// or DirtyCpu, or just remove the "schedule" option and leave the clause to be simply: `#[rustler::nif]`
#[rustler::nif(schedule = "DirtyIo")]
fn something() -> MyResult {
  match function_that_can_fail() {
    Ok(rust_object) => MyResult::Success(ResourceArc::(rust_object)),
    Err(e) => MyResult::Failure(e.to_string()),
  }
}

You might need to remove the lifetime qualifiers though, can’t remember why my code needed them now. Using Rustler’s ResourceArc is crucial here; thanks to it you will receive the Rust object wrapped in a nice Erlang Reference which in the case of this function you’ll see in your iex console like so (in the case of success):

{:ok, #Reference<0.679634982.4171759622.38993>}

…or in case of failure:

{:error, "error message from Rust here"}

Then on the Elixir side you should take care to have the same something() function that Rustler will wrap and pass through to Rust (this is covered in Rustler’s guide). That’s basically it. Poke me if you need more help, Rustler could be tricky and it seems the maintainers don’t have time for it for a long time now.

4 Likes

Sweet! Thank you so much :slight_smile: (all of you that is ^_^)
I’ll try that once I get to that project again next week. This looks really promising.

1 Like

I am also interested in doing this, I’m experimenting with something and will need to pass a reference to a rust object(? My rust knowledge is poor) to elixir so that I can later call other rust methods to it

1 Like

@dimitarvp I tried to implement the solution you suggested with rustler 0.22.0-rc.0.
I now have the problem that the YOUR_RUST_TYPE_HERE type that I used instead does not implement the ResourceTypeProvider trait and I’m unsure how to impl that correctly… did you have to provide the trait implementation for the type you used in your application?

Doesn’t ring a bell from memory. Do you have a GitHub repo for me to look at?

EDIT: Actually this might be because you have incorrectly implemented Encoder – but not 100% sure.

Unfortunately no because the whole construct is a little complex by now…

But here is the actual code in the rust lib:

use rustler::resource::ResourceArc;
use rustler::{Encoder, Env, Term};

rustler::atoms! { error, ok, }

type DbConnection = odbx::Connection<odbx::AutocommitOn>;

enum DbConnectionResult {
    Success(ResourceArc<DbConnection>),
    Failure(String),
}

// impl ResourceTypeProvider for DbConnectionResult {
//     fn get_type() -> &'static ResourceType<Self> {
//         rustler::resource::ResourceType {}
//     }
// }

impl<'a> Encoder for DbConnectionResult {
    fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
        match self {
            DbConnectionResult::Success(arc) => (ok(), arc).encode(env),
            DbConnectionResult::Failure(msg) => (error(), msg).encode(env),
        }
    }
}

#[rustler::nif]
pub fn connect() -> DbConnectionResult {
    // Connect to database using connection string
    let connection_string = "DSN=SYA";
    match odbx::Odbc::connect(&connection_string) {
        Ok(conn) => DbConnectionResult::Success(ResourceArc::new(conn)),
        Err(e) => DbConnectionResult::Failure(String::from("Some error")),
    }
}

#[rustler::nif]
fn add(a: i64, b: i64) -> i64 {
    a + b
}

rustler::init!("Elixir.ExOdbc", [add, connect]);

odbx:: is a package I wrote to patch and adjust several rust odbc packages (which is what makes this whole thing a little complicated…)

The compiler is complaining about the Success(ResourceArc<DbConnection>), since Connection<AutocommitOn> does not implement the trait ResourceTypeProvider

Then I think you should also write an Encoder trait implementation for AutocommitOn (or the enum it belongs to)?

Basically: Rustler needs your Rust objects be serializable with Rustler means. This means you have to recursively and manually add (de)serializers for every type you want to move inbetween the BEAM and the Rust code.

1 Like

Aaah. Yeah that might be the cause - good call. I’ll try that.


The problem now is that I cannot impl Encoder for AutocommitOn since it is not part of the current crate. Will invastigate further :slight_smile:

You can make a wrapper of that enum on your own and then write a serializer for it:

pub struct AutocommitWrapper(pub Autocommit); // `Autocommit` == your foreign enum type name.

// Implement Rustler `Encoder` for `AutocommitWrapper` here...
1 Like

@mmmrrr did you manage to solve your problem? I have similar one. I mean I have struct from third party library and I want to return reference to it to the Elixir’s side but can’t make it work

Check my reply above. Wrap it in your own struct first.

Yeah I know, I tried it before I wrote this post but it doesn’t work.
My simplified code at this moment

use rustler::resource::ResourceArc;
use rustler::{Env, Encoder, Term};

rustler::atoms! {
    ok,
    error
}

type MyRustReturnType = MyStruct;

enum MyResult {
    Success(ResourceArc<MyRustReturnType>),
    Failure(String)
}

impl Encoder for MyResult {
    fn encode(&self, env: Env) -> Term {
        match self {
            MyResult::Success(arc) => (ok(), arc).encode(env),
            MyResult::Failure(msg) => (error(), msg).encode(env)
        }
    }
}

pub struct MyStruct {
    pub a: i64
}


#[rustler::nif]
fn add(a: i64, b: i64) -> MyResult {
    MyResult::Success(ResourceArc::new(MyStruct{a: a+b}))
}

The problem is with Success(ResourceArc<MyRustReturnType>) as it doesn’t implement ResourceTypeProvider :c

Sorry, I think I am not following. What’s stopping you from doing impl Encoder for MyRustReturnType?

Thanks for so fast reply. The problem is in defining enum MyResult. I cannot define Success(ResourceArc<MyRustReturnType>). I am getting

   --> src/lib.rs:12:13
    |
12  |     Success(ResourceArc<MyRustReturnType>),
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ResourceTypeProvider` is not implemented for `MyStruct`
    | 
   ::: /home/michal/.cargo/registry/src/github.com-1ecc6299db9ec823/rustler-0.22.0-rc.0/src/resource.rs:118:8
    |
118 |     T: ResourceTypeProvider,
    |        -------------------- required by this bound in `ResourceArc`

And I am wondering if I should try to implement this as in the source code documentation there is that in moste cases user doesn’t have to worry about ResourceTypeProvider and my example is quite simple.