Skip to content

Migrate from Data_Wrap_Struct to TypedData API #77

@cardmagic

Description

@cardmagic

Problem

Ruby 3.2+ emits deprecation warnings when using the legacy Data_Wrap_Struct API:

warning: undefining the allocator of T_DATA class GSL::Vector
warning: undefining the allocator of T_DATA class GSL::Matrix
warning: undefining the allocator of T_DATA class GSL::Rng
...

These warnings appear on every require 'gsl' and when instantiating GSL objects.

Root Cause

The codebase uses the legacy Ruby C API:

  • Data_Wrap_Struct / Data_Make_Struct
  • Data_Get_Struct
  • rb_undef_alloc_func

Ruby recommends migrating to the TypedData API which provides better GC integration, type safety, and memory profiling.

Scope

~3,300 occurrences across the codebase

Required Changes

For each class, the migration involves:

1. Define a rb_data_type_t struct

static const rb_data_type_t gsl_vector_type = {
    .wrap_struct_name = "GSL::Vector",
    .function = {
        .dmark = NULL,  // or mark function if needed
        .dfree = gsl_vector_free,
        .dsize = NULL,  // optional: memory size function
    },
    .flags = RUBY_TYPED_FREE_IMMEDIATELY,
};

2. Replace Data_Wrap_Struct

// BEFORE
Data_Wrap_Struct(klass, mark_func, free_func, ptr);

// AFTER
TypedData_Wrap_Struct(klass, &gsl_vector_type, ptr);

3. Replace Data_Get_Struct

// BEFORE
Data_Get_Struct(obj, gsl_vector, v);

// AFTER
TypedData_Get_Struct(obj, gsl_vector, &gsl_vector_type, v);

4. Remove rb_undef_alloc_func calls

TypedData handles allocation differently; these calls become unnecessary.

Workaround

Users can suppress warnings until migration is complete:

Warning[:deprecated] = false
require 'gsl'
Warning[:deprecated] = true

Or via environment:

RUBYOPT='-W:no-deprecated' ruby script.rb

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions