dimitarvp
Rustler: how to return an `usize` variable?
Poking @hansihe and @scrogson if that’s okay. Everybody else’s input is appreciated as well.
I need to return a Rust usize variable and I am trying to do it like so:
let affected = ...; // a function that returns `usize`.
Ok((atoms::ok(), ResourceArc::new(affected)).encode(env))
I am getting this error (and a few more related to it):
error[E0277]: the trait bound `usize: rustler::resource::ResourceTypeProvider` is not satisfied
--> src/lib.rs:101:63
|
101 | Ok(affected) => Ok((atoms::ok(), ResourceArc::new(affected)).encode(env)),
| ^^^^^^^^ the trait `rustler::resource::ResourceTypeProvider` is not implemented for `usize`
|
= note: required by `rustler::ResourceArc::<T>::new`
I am at rustler 0.21.0.
Can you point me in the right direction?
Marked As Solved
scrogson
As I responded on IRC…resources only work with structs and must be accompanied with the rustler::resource_struct_init! macro (in 0.21) or rustler::resource! (in 0.22).
In order to make this work, you’ll need to make a new type which wraps the usize like so:
struct Affected(usize)
Also Liked
scrogson
Yeah a usize and other primitives don’t really fit the use-case for resources
. A resource is used to keep some rust data-structure alive in between NIF calls.
dimitarvp
Curiously enough, I was able to just skip ResourceArc and I get no compilation error now.








