Skip to content

Instantly share code, notes, and snippets.

@kernigh
Created December 18, 2022 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernigh/95ec3a4b28e9774b963afc3d008a7cdd to your computer and use it in GitHub Desktop.
Save kernigh/95ec3a4b28e9774b963afc3d008a7cdd to your computer and use it in GitHub Desktop.
#eql? and #hash delegation for CRuby
#include <ruby.h>
static void
deli_mark(void *ptr)
{
rb_gc_mark((VALUE)ptr);
}
static const rb_data_type_t deli_type = {
"Type",
{deli_mark,},
0, 0,
RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE
deli_alloc(VALUE klass)
{
return TypedData_Wrap_Struct(klass, &deli_type, (void *)Qnil);
}
static VALUE
deli_initialize(VALUE self, VALUE i)
{
RTYPEDDATA(self)->data = (void *)i;
return Qnil;
}
static VALUE
deli_eqlp(VALUE self, VALUE o)
{
o = (VALUE)rb_check_typeddata(o, &deli_type);
return rb_eql((VALUE)(RTYPEDDATA(self)->data), o);
}
static VALUE
deli_hash(VALUE self)
{
return rb_hash((VALUE)(RTYPEDDATA(self)->data));
}
void
Init_deli(void)
{
VALUE cDeli;
cDeli = rb_define_class("Deli", rb_cObject);
rb_define_alloc_func(cDeli, deli_alloc);
rb_define_method(cDeli, "initialize", deli_initialize, 1);
rb_define_method(cDeli, "eql?", deli_eqlp, 1);
rb_define_method(cDeli, "hash", deli_hash, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment