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.
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.