rusterlium / rustler

Safe Rust bridge for creating Erlang NIF functions
https://docs.rs/crate/rustler
Apache License 2.0
4.32k stars 225 forks source link

Refactor Resources #617

Closed filmor closed 2 months ago

filmor commented 4 months ago

This allows us to eventually deprecate the existing resource! macro and enables a path that fixes #606.

filmor commented 3 months ago

Another option, instead of a derive macro (or additional to one) would be to allow explicitly implementing a Resource trait and registering it using a rustler::resource!() outside of load, which would use the inventory::submit! under the hood. This would allow us to make the down callback for #376 (and dyncall and other potential callbacks) a trait function with an empty default implementation.

If we like the derive style, another alternative would be, to have a derive(Resource) and a derive(MonitorResource), where MonitorResource requires an implementation of a MonitorResource trait with a down_callback function and these would be different code paths with two sets of registrations. I think I'm in favour of that.

/edit: This is how it is implemented now: #[derive(MonitorResource)] + implementing the MonitorResource trait.

filmor commented 3 months ago

Note to self: https://github.com/rusterlium/rustler/issues/84 is also a very interesting approach (completely doing away with the necessary pre-registration for resource types!), I'm just not quite sure how to incorporate monitors into it.