matsadler / magnus

Ruby bindings for Rust. Write Ruby extension gems in Rust, or call Ruby from Rust.
https://docs.rs/magnus/latest/magnus/
MIT License
682 stars 35 forks source link

Make it possible to transform a RubyUnavailableError to an Error #103

Closed Maaarcocr closed 1 month ago

Maaarcocr commented 8 months ago

why?

Lots of code from before 0.6 was written in such a way that it would return magnus::Error whenever interacting with anything from Ruby. This worked well. Now, any such code (given the changes to values being made not Send and not Sync) may use Opaque or other types that need to get a handle to a Ruby object.

Calling Ruby::get() returns such handle in a Result but to my surprise the Err branch contains something that is not convertible to a magnus::Error.

how?

To fix this, I propose we add a new case to magnus::ErrorType so that we can impl From<RubyUnavailableError> for Error which would make all old code work by just adding a line to get the ruby handle.

other possibilities

another approach would be to let the user code handle this, with a new enum like

enum MyError {
  Error(magnus::Error)
  Unavailable(RubyUnavailableError)
}

but I think it would be neat if this crate had one Error type which is easy to work with and has good DX on its own. But maybe I'm missing why it would be bad to go the way I propose in this pr