-
To match the Elixir convention, I am trying to return from my Rustler functions I can do the
With the above code, I am able to get If I just I also tried
with
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 How can I do this? Is there some more correct way to do it I am not? Thanks for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
The generated atoms are functions, so you'd need to use |
Beta Was this translation helpful? Give feedback.
-
I would expect this to work: let result = (my_atoms::ok(), hello);
return Ok(result); What does the compiler report for that? |
Beta Was this translation helpful? Give feedback.
-
You need to return `Ok((my_atoms::ok(), "hello".to_string()))` (return a tuple).
Am 17. Oktober 2024 19:50:04 MESZ schrieb jonmdev ***@***.***>:
…Doesn't work. You get:
```
error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied
--> src/lib.rs:300:16
|
300 | return Ok(my_atoms::ok(), "hello".to_string());
| ^^ -------------- unexpected argument #1 of type `rustler::Atom`
|
note: tuple variant defined here
--> /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/result.rs:531:5
help: remove the extra argument
|
300 - return Ok(my_atoms::ok(), "hello".to_string());
300 + return Ok("hello".to_string());
|
```
You can try it yourself with this code:
```
use rustler::{NifResult };
mod my_atoms {
rustler::atoms! {
ok
}
}
#[rustler::nif]
fn get_ok() -> NifResult<String> {
return Ok(my_atoms::ok(),"hello".to_string()); //doesn't work
}
```
`NifResult` cannot seem to return `{:ok, result}`. You can only get this with `Result` and the method posted above. This was moved to "discussion" for unknown reasons. I think it is a bug. Should I report it again as a bug?
--
Reply to this email directly or view it on GitHub:
#660 (reply in thread)
You are receiving this because you commented.
Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
To make that work, you need to adapt the return type of your function to `NifResult<(Atom, String), String>` (as this returns a tuple for OK now). I am currently on mobile and can't give a more complete example.
Maybe you can simply use Result (see the [encoder](https://github.com/rusterlium/rustler/blob/c2b0a7729a3a04333204107173eedd838f48d44e/rustler/src/types/mod.rs#L122))
An example can be found [in the tests](https://github.com/rusterlium/rustler/blob/c2b0a7729a3a04333204107173eedd838f48d44e/rustler_tests/native/rustler_test/src/test_tuple.rs#L22).
|
Beta Was this translation helpful? Give feedback.
It's not a bug, you have to adjust the return type if you are returning a different type. The compiler tells you. So, change
NifResult<String>
toNifResult<(Atom, String>
. Using a bareResult
is likely the better option, though.