Skip to content

Instantly share code, notes, and snippets.

@firejox
Last active June 8, 2020 05:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firejox/a6f2193a5e0820eef7e7e36c937c2330 to your computer and use it in GitHub Desktop.
Save firejox/a6f2193a5e0820eef7e7e36c937c2330 to your computer and use it in GitHub Desktop.
The changes about prevent leaking container_of globaly
# channel.cr
class Channel(T)
struct Sender(T)
@link : Crystal::StaticList
end
Crystal::StaticList.container_of(ptr, Channel::Sender(T), @link)
end
# crystal/static_list.cr
struct Crystal::StaticList
macro container_of(ptr, type, member)
end
end
# channel.cr
class Channel(T)
include Crystal::StaticList::Reflect
private struct Sender(T)
@link : Crystal::StaticList
end
container_of(ptr, Sender(T), @link)
end
# crystal/static_list.cr
struct Crystal::StaticList
module Reflect
private macro container_of(ptr, type, member)
end
end
end
# channel.cr
class Channel(T)
@[Crystal::StaticList::Reflect::Fields(:link)]
private struct Sender(T)
include Crystal::StaticList::Reflect
@link : Crystal::StaticList
end
Sender(T).from_link(ptr)
end
# crystal/static_list.cr
struct Crystal::StaticList
module Reflect
annotation Fields; end
macro included
{% for member in @type.annotation(Crystal::StaticList::Reflect::Fields) %}
@[AlwaysInline]
def self.from_{{ member.id }}(ptr : Crystal::StaticList*)
end
{% end %}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment