Ruby 3.4.4p34 (2025-05-14 revision a38531fd3f617bf734ef7d6c595325f69985ea1d)
RTypedData Struct Reference

"Typed" user data. More...

#include <rtypeddata.h>

Data Fields

struct RBasic basic
 The part that all ruby objects have in common.
 
const rb_data_type_t *const type
 This field stores various information about how Ruby should handle a data.
 
const VALUE typed_flag
 This has to be always 1.
 
void * data
 Pointer to the actual C level struct that you want to wrap.
 

Detailed Description

"Typed" user data.

By using this, extension libraries can wrap a C struct to make it visible from Ruby. For instance if you have a struct timeval, and you want users to use it,

static inline const rb_data_type_t timeval_type = {
// Note that unspecified fields are 0-filled by default.
.wrap_struct_name = "timeval",
.function = {
.dmark = nullptr, // no need to mark
.dfree = RUBY_TYPED_DEFAULT_FREE, // use ruby_xfree()
.dsize = [](auto) {
return sizeof(struct timeval);
},
},
};
extern "C" void
Init_timeval(void)
{
auto klass = rb_define_class("YourName", rb_cObject);
rb_define_alloc_func(klass, [](auto klass) {
struct timeval *t;
klass, struct timeval, &timeval_type, t);
if (auto i = gettimeofday(t, nullptr); i == -1) {
rb_sys_fail("gettimeofday(3)");
}
else {
return ret;
}
});
}
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:980
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:79
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:197
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:497

Definition at line 350 of file rtypeddata.h.

Field Documentation

◆ basic

struct RBasic RTypedData::basic

The part that all ruby objects have in common.

Definition at line 353 of file rtypeddata.h.

◆ data

void* RTypedData::data

Pointer to the actual C level struct that you want to wrap.

Definition at line 370 of file rtypeddata.h.

◆ type

const rb_data_type_t* const RTypedData::type

This field stores various information about how Ruby should handle a data.

This roughly resembles a Ruby level class (apart from method definition etc.)

Definition at line 360 of file rtypeddata.h.

Referenced by rb_data_typed_object_alloc(), and rb_data_typed_object_make().

◆ typed_flag

const VALUE RTypedData::typed_flag

This has to be always 1.

Definition at line 367 of file rtypeddata.h.


The documentation for this struct was generated from the following file: