Skip to content

Commit e4bb734

Browse files
committed
✨ Fix Config::AttrTypeCoercion for Ractor sharing
1 parent c2170e2 commit e4bb734

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/net/imap/config/attr_type_coercion.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ def self.included(mod)
2626
end
2727
private_class_method :included
2828

29-
Types = Hash.new do |h, type| type => Proc | nil; type end
30-
Types[:boolean] = Boolean = -> {!!_1}
31-
Types[Integer] = ->{Integer(_1)}
29+
def self.safe(...) = Ractor.make_shareable nil.instance_eval(...).freeze
30+
private_class_method :safe
31+
32+
Types = Hash.new do |h, type| type => Proc | nil; safe{type} end
33+
Types[:boolean] = Boolean = safe{-> {!!_1}}
34+
Types[Integer] = safe{->{Integer(_1)}}
3235

3336
def self.attr_accessor(attr, type: nil)
3437
type = Types[type] or return
@@ -37,12 +40,12 @@ def self.attr_accessor(attr, type: nil)
3740
end
3841

3942
Enum = ->(*enum) {
40-
enum = enum.dup.freeze
43+
enum = safe{enum}
4144
expected = -"one of #{enum.map(&:inspect).join(", ")}"
42-
->val {
45+
safe{->val {
4346
return val if enum.include?(val)
4447
raise ArgumentError, "expected %s, got %p" % [expected, val]
45-
}
48+
}}
4649
}
4750

4851
end

0 commit comments

Comments
 (0)