Created
May 3, 2021 08:52
-
-
Save willhbr/165967b005994a8f19f8a47a10b62f76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SingleFiberAccess(T) | |
@object : T | |
{% if flag? :debug %} | |
@fiber = Fiber.current | |
{% end %} | |
def initialize(@object) | |
end | |
macro method_missing(call) | |
wrapped.{{ call.method }}({{ call.args }}) | |
end | |
@[AlwaysInline] | |
def wrapped | |
{% if flag? :debug %} | |
if @fiber != Fiber.current | |
raise "wrong thread" | |
end | |
{% end %} | |
@object | |
end | |
delegate to_s, inspect, to: wrapped | |
end | |
thing = SingleFiberAccess(Array(Int32)).new [1, 2, 3] | |
spawn do | |
puts thing | |
end | |
Fiber.yield |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment