RactorShack::ShareableGuaranteed

Sending non-shareable objects to Ractor/Ractor::Port will result in copies of parts of the object graph being cloned in order to guarantee that mutations to those objects in the other Ractor can't have side effects in the Ractor that sent them.

Sometimes, though, one is not expecting data to be silently copied and would rather an error be raised to be informed that the data that was expected to be shareable is, in fact, not shareable. Maybe you intended to move the object to the Ractor but forgot move: true or maybe you forgot to #freeze something in the object graph. It would be nice if instead an error were raised to help you detect and correct such mistakes.

That's what this gem does!

Installation

Typical stuff: add gem "shareable-guaranteed" to your Gemfile or .gemspec file. Or even just gem install shareable-guaranteed if that's your jam.

Usage

You can make Ractor/Ractor::Port explode when you send it a non-shareable object by:

require "ractor_shack/shareable_guaranteed"

Ractor.include RactorShack::ShareableGuaranteed
Ractor::Port.include RactorShack::ShareableGuaranteed

You might want to instead subclass Ractor/Ractor::Port if you're building a system with dependencies that might make use of Ractor:

require "ractor_shack/shareable_guaranteed"

MyRactorSubclass.include RactorShack::ShareableGuaranteed
MyRactorSubclass::Port.include RactorShack::ShareableGuaranteed

If there are certain objects that you don't care if they are cloned, you can configure that.

Let's say you just want instances of String cloned. You can tell ShareableGuaranteed this with:

RactorShack::ShareableGuaranteed.consider_shareable(String)

Which means you'll no longer get a NonShareableSentToRactorError when sending a string to a port that is shareable-guaranteed.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ractor-shack/shareable-guaranteed

If you happen to need help with the gem please reach out!

License

This project is licensed under the MPL-2.0 license. Please see LICENSE.txt for more info.