Search code examples
crubyruby-c-extension

Difference between Data_Wrap_Struct and TypedData_Wrap_Struct?


I'm wrapping a C struct in a Ruby C extension but I can't find the differente between Data_Wrap_Struct and TypedData_Wrap_Struct in the docs, what's the difference between the two functions?


Solution

  • It's described pretty well in the official documentation.

    The tl;dr is that Data_Wrap_Struct is deprecated and just lets you set the class and the mark/free functions for the wrapped data. TypedData_Wrap_Struct instead lets you set the class and then takes a pointer to a rb_data_type_struct structure that allows for more advanced options to be set for the wrapping:

    • the mark/free functions as before, but also
    • an internal label to identify the wrapped type
    • a function for calculating memory consumption
    • arbitrary data (basically letting you wrap data at a class level)
    • additional flags for garbage collection optimization

    Check my unofficial documentation for a couple examples of how this is used.