rustler icon indicating copy to clipboard operation
rustler copied to clipboard

Add ATO Resources

Open goertzenator opened this issue 7 years ago • 3 comments

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.

goertzenator avatar Feb 17 '17 15:02 goertzenator

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

jorendorff avatar Feb 18 '17 23:02 jorendorff

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.

jorendorff avatar Feb 18 '17 23:02 jorendorff

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?

hansihe avatar Feb 21 '17 15:02 hansihe