Skip to content

feat(stable-api): Add RDATA_PTR for legacy wrapped data access #666

@ianks

Description

@ianks

Summary

Add stable API method for accessing wrapped data from legacy RData objects, equivalent to the C macro RDATA_PTR / DATA_PTR.

Motivation

While RTypedData is the modern recommended approach, many existing C extensions still use the legacy RData / Data_Wrap_Struct API. Rust extensions that need to interoperate with these existing objects need a stable way to access the wrapped pointer.

Proposed API

/// Get the data pointer from an RData object (akin to `DATA_PTR` / `RDATA_PTR`).
///
/// This is for legacy Data objects created with `Data_Wrap_Struct`.
/// For modern typed data, use `rtypeddata_get_data` instead.
///
/// # Safety
/// This function is unsafe because it dereferences a raw pointer to get
/// access to underlying Ruby data. The caller must ensure that the pointer
/// is valid and points to a T_DATA object.
unsafe fn rdata_ptr(&self, obj: VALUE) -> *mut c_void;

Implementation Notes

  • The RData struct has a data field containing the wrapped pointer
  • Need to verify object is actually T_DATA (not T_TYPEDDATA)
  • Reference: include/ruby/internal/core/rdata.h

Checklist

  • Add method to StableApiDefinition trait
  • Implement for each Ruby version
  • Add C fallback in compiled.c
  • Add public macro wrapper in macros.rs
  • Add tests
  • Document that RTypedData should be preferred for new code

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions