rustler
rustler copied to clipboard
Add ATO Resources
I've been churning through many ideas on my Ruster project lately, and one that worked out really well and I think would be useful for Rustler is "Any Trait Object Resources" (ATO Resources)
In a nutshell, with a single resource type registration you can use any Rust type as a resource. The advantages are:
- No need to pre-register types. You just start using them and they work.
- No resource traits so no need for a wrapper type to get around trait orphan rules. For example
Cell<String>
can be used directly as a resource type.
So, if this fits in for Rustler I invite you to borrow the code; I will happily answer any questions about it. I'll also point out that the upcoming enif_select()
function in OTP-20 will likely force a revisit of resources.
Cool. I considered something like this for Rustler, but didn't think @hansihe would like the slight extra allocation overhead or the extra runtime overhead of the type check. I couldn't figure out how to avoid either one.
(It seems like Any
should have its own drop-in-place method—Rust "knows" in some sense how to drop an Any
trait object, since Box<Any>
works—but it doesn't.)
Oh, it looks like your code avoids all but a word of allocation overhead. That seems close enough. It's just the runtime check then; and that will be a well-predicted branch.
This is a very cool idea, and it would improve usability a lot.
If we decide to add this, would it make sense to keep the existing way of doing it or should we get rid of it?