Add backports module for R 4.5 C-API compliance#265
Open
CGMossa wants to merge 1 commit into
Open
Conversation
R's C API is stabilizing: several entry points used to access environments, closures, variables and the data pointer became non-API in R 4.5, replaced by new compliant functions. Code that needs to build against both old and new R has to branch per version. Port extendr-ffi's `backports` module (verbatim, with attribution): a set of small `unsafe fn` wrappers (`get_parent_env`, `get_var`, `get_var_safe`, `get_var_in_frame`, `get_closure_env/body/formals`, `dataptr`, `is_data_frame`) that dispatch to the pre- or post-4.5 entry point. The module declares its own cfg-gated `extern` block, so it does not depend on which symbols the generated bindings happen to include. build.rs now emits `r_4_4` / `r_4_5` cfg flags (with matching `rustc-check-cfg`) for the module to gate on. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TODO: replace this line with your own framing, in your own voice.
AI-written details
Summary
ENCLOS,Rf_findVar,Rf_findVarInFrame,CLOENV,BODY,FORMALS,DATAPTR,Rf_isFrame) became non-API and were replaced by compliant functions (R_ParentEnv,R_getVar,R_getVarEx,R_ClosureEnv,R_ClosureBody,R_ClosureFormals,DATAPTR_RO,Rf_isDataFrame). Code that targets both old and new R has to branch per version.extendr-ffi'sbackportsmodule verbatim (with attribution) intosrc/backports.rs: smallunsafe fnwrappers (get_parent_env,get_var,get_var_safe,get_var_in_frame,get_closure_env/body/formals,dataptr,is_data_frame) that dispatch to the correct entry point per R version.externblock, so it does not depend on which symbols the committed bindings happen to include.build.rsnow emitsr_4_4/r_4_5cfg flags (with matchingrustc-check-cfg) for the module to gate on. These are internal to this crate.Test plan
cargo buildagainst R 4.6.0 (sor_4_5is set): compiles, 0 errors.backports::get_parent_env(R_GlobalEnv)andbackports::is_data_frame(...)from the existing embedded-R test resolves and runs against R 4.6.0.not(r_4_5)branch compiles and links the old entry points).Notes
lib.rs/altrep.rs/graphics.rs/symbols.rsare a hand-curated subset of what libR-sys already generates; the idiomatic enums overlap with the existingRbooleanenum and theconvert_to_rust_enumswork;non_api.rscould follow later behind a feature if wanted.Drafted by Claude (claude-opus-4-8). Reviewed by the author.