-
-
Notifications
You must be signed in to change notification settings - Fork 56
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Summary
Add stable API methods for accessing Ruby object encodings, equivalent to the C macros ENCODING_GET and ENCODING_GET_INLINED.
Motivation
Encoding handling is critical for text processing in Ruby extensions. Currently, users must call rb_enc_get() which involves additional overhead. Direct access to the encoding index from the object flags would be more efficient.
Proposed API
/// Get the encoding index from a Ruby object (akin to `ENCODING_GET`).
///
/// Works for String, Regexp, and Symbol objects that store encoding
/// information in their flags.
///
/// # 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 an object that has encoding information.
unsafe fn encoding_get(&self, obj: VALUE) -> c_int;Implementation Notes
- Encoding index is stored in the object's flags (bits 16-23 typically)
- Need to handle
ENCODING_INLINE_MAX- larger indices require different handling - May need to check
FL_USHIFTand related flag constants - Reference:
include/ruby/internal/encoding/encoding.h
Checklist
- Add method to
StableApiDefinitiontrait - Implement for each Ruby version (2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 4.0)
- Add C fallback in
compiled.c - Add public macro wrapper in
macros.rs - Add tests comparing Rust vs C implementation
- Add to
show-asmscript for performance verification
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request