Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


1 Answers

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.

like image 71
Max Avatar answered Jan 03 '26 03:01

Max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!