mikejm

mikejm

How to return {:ok, result} back into Elixir from NifResult<Type> with Rustler and Rust?

To match the Elixir convention, I am trying to return from my Rustler functions {:ok, result} or {:error, error_string}.

I can do the :error case as follows, but can’t solve the :ok case:

fn get_id(index: i64) -> NifResult<String> {
    if index >= 1) {
        return Err(rustler::Error::Term(Box::new("attempt to get index outside range"))); 
    }
    else {
        let hello = "HELLO".to_string();
        return Ok( hello); //HOW TO MAKE THIS RETURN {:ok, "HELLO"} rather than just "HELLO"?
    }
}

With the above code, I am able to get {:error, "attempt to get index outside range"} but I still cannot get {:ok, "HELLO"}.

If I just return Ok(hello); then I just get the result without the {:ok, result} tuple wrapping of it.

I also tried

mod my_atoms {
    rustler::atoms! {
        ok
   }
}

with

        let result = (my_atoms::ok, hello); 
        return Ok(result); 

But this does not work either. I tried ChatGPT out of interest but it is just giving me hallucinations of things that don’t exist (maybe did at one time but don’t now).

It seems logical that if you are going to get {:error, reason} you should also want to get {:ok, result}

How can I do this? Is there some more correct way to do it I am not? Thanks for any help.

Marked As Solved

jswanner

jswanner

Looks like you want to use a Result not a NifResult: Result behaviour difference · Issue #377 · rusterlium/rustler · GitHub

Also Liked

filmor

filmor

It’s probably enough if you ask in one place and wait for more than two hours for an answer. I’d venture a guess that most people that read the issues on the Rustler repo also from time to time hang around here.

You can do what @jswanner suggested. A definite error in your attempt with the tuple is that the atoms! macro generates functions, not objects. You have to call my_atoms::ok() to get the actual :ok atom.

mikejm

mikejm

Correction - typo. Just for reference if anyone else is looking in the future the working function is:

#[rustler::nif]
fn get_id(index: i64) -> Result<String, String> {
    if index >= 1) {
        return Err("hi".to_string()); 
    }
    else {
        return Ok("HELLO".to_string()); 
    }
}

You have to use Result, not NifResult as suggested.

dimitarvp

dimitarvp

I was about to point that out because in the linked PR it was clearly stated that using NifResult does not wrap the result in the :ok or :error atoms.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement