Ruby: RTypedData Struct Reference

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

Data Fields
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 * | type
| This field stores various information about how Ruby should handle a data.
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;
auto ret = TypedData_Make_Struct(
klass, struct timeval, &timeval_type, t);
if (auto i = gettimeofday(t, nullptr); i == -1) {
rb_sys_fail("gettimeofday(3)");
}
else {
return ret;
}
});
}
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:923
rb_sys_fail
void rb_sys_fail(const char *mesg)
Converts a C errno into a Ruby exception, then raises it.
Definition error.c:3274
rb_define_alloc_func
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:79
TypedData_Make_Struct
#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:489
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:190
timeval
Definition missing.h:63

Definition at line 340 of file rtypeddata.h.

## Field Documentation

## ◆ basic

struct RBasic RTypedData::basic

The part that all ruby objects have in common.

Definition at line 343 of file rtypeddata.h.

## ◆ data

void* RTypedData::data

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

Definition at line 362 of file rtypeddata.h.

## ◆ type

const rb_data_type_t* 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 350 of file rtypeddata.h.

## ◆ typed_flag

VALUE RTypedData::typed_flag

This has to be always 1.

Definition at line 359 of file rtypeddata.h.

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

- include/ruby/internal/core/rtypeddata.h