Skip to content

Instantly share code, notes, and snippets.

@willhbr
Created May 3, 2021 08:52
Show Gist options
  • Save willhbr/165967b005994a8f19f8a47a10b62f76 to your computer and use it in GitHub Desktop.
Save willhbr/165967b005994a8f19f8a47a10b62f76 to your computer and use it in GitHub Desktop.
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