Skip to main content

miniextendr_api/sys/
altrep.rs

1//! Raw ALTREP C API bindings.
2//!
3//! This module mirrors `R_ext/Altrep.h` and is intentionally low-level. Prefer
4//! the `#[derive(AltrepInteger)]` / `AltrepReal` / `AltrepLogical` /
5//! `AltrepString` / `AltrepRaw` / `AltrepComplex` / `AltrepList` derives — or
6//! the `#[altrep(manual)]` opt-out — over wiring these typedefs by hand.
7//!
8//! See [`crate::altrep_traits`] for the guard-mode taxonomy (`unsafe` /
9//! `rust_unwind` / `r_unwind`) that gates panic and longjmp escape from
10//! callback bodies; [`crate::altrep_bridge`] is the trampoline layer that
11//! enforces those modes.
12//!
13//! # Calling these from a callback
14//!
15//! ALTREP callbacks are invoked by R on its main thread, so the
16//! [`#[r_ffi_checked]`](miniextendr_macros::r_ffi_checked) main-thread
17//! assertion would always pass. Inside a callback body you may therefore call
18//! `*_unchecked` variants of the R API (or the `r_unwind` guard's protected
19//! re-entry helpers) without tripping the **MXL301** lint. See
20//! [`crate::sys`] for the broader checked-vs-unchecked story.
21
22#![allow(non_camel_case_types)]
23use crate::SEXP;
24use crate::sexp_types::{R_xlen_t, Rboolean, Rbyte, Rcomplex, SEXPTYPE};
25use crate::sys::DllInfo;
26
27/// Signature for ALTREP `coerce` method.
28pub type R_altrep_Coerce_method_t =
29    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, rtype: SEXPTYPE) -> SEXP>;
30
31/// Signature for ALTREP extended `unserialize` method.
32pub type R_altrep_UnserializeEX_method_t = ::std::option::Option<
33    unsafe extern "C-unwind" fn(
34        class: SEXP,
35        state: SEXP,
36        attr: SEXP,
37        objf: ::std::os::raw::c_int,
38        levs: ::std::os::raw::c_int,
39    ) -> SEXP,
40>;
41/// Signature for ALTREP `unserialize` method.
42pub type R_altrep_Unserialize_method_t =
43    ::std::option::Option<unsafe extern "C-unwind" fn(class: SEXP, state: SEXP) -> SEXP>;
44/// Signature for ALTREP `serialized_state` method.
45pub type R_altrep_Serialized_state_method_t =
46    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> SEXP>;
47/// Signature for ALTREP extended `duplicate` method.
48pub type R_altrep_DuplicateEX_method_t =
49    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, deep: Rboolean) -> SEXP>;
50/// Signature for ALTREP `duplicate` method.
51pub type R_altrep_Duplicate_method_t =
52    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, deep: Rboolean) -> SEXP>;
53/// Signature for ALTREP `inspect` method.
54pub type R_altrep_Inspect_method_t = ::std::option::Option<
55    unsafe extern "C-unwind" fn(
56        x: SEXP,
57        pre: ::std::os::raw::c_int,
58        deep: ::std::os::raw::c_int,
59        pvec: ::std::os::raw::c_int,
60        inspect_subtree: ::std::option::Option<
61            unsafe extern "C-unwind" fn(
62                x: SEXP,
63                pre: ::std::os::raw::c_int,
64                deep: ::std::os::raw::c_int,
65                pvec: ::std::os::raw::c_int,
66            ),
67        >,
68    ) -> Rboolean,
69>;
70/// Signature for ALTREP `length` method.
71pub type R_altrep_Length_method_t =
72    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> R_xlen_t>;
73/// Signature for ALTVEC `dataptr` method.
74pub type R_altvec_Dataptr_method_t = ::std::option::Option<
75    unsafe extern "C-unwind" fn(x: SEXP, writable: Rboolean) -> *mut ::std::os::raw::c_void,
76>;
77/// Signature for ALTVEC `dataptr_or_null` method.
78pub type R_altvec_Dataptr_or_null_method_t =
79    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> *const ::std::os::raw::c_void>;
80/// Signature for ALTVEC `extract_subset` method.
81pub type R_altvec_Extract_subset_method_t =
82    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, indx: SEXP, call: SEXP) -> SEXP>;
83/// Signature for ALTINTEGER `elt` method.
84pub type R_altinteger_Elt_method_t = ::std::option::Option<
85    unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> ::std::os::raw::c_int,
86>;
87/// Signature for ALTINTEGER `get_region` method.
88pub type R_altinteger_Get_region_method_t = ::std::option::Option<
89    unsafe extern "C-unwind" fn(
90        sx: SEXP,
91        i: R_xlen_t,
92        n: R_xlen_t,
93        buf: *mut ::std::os::raw::c_int,
94    ) -> R_xlen_t,
95>;
96/// Signature for ALTINTEGER `is_sorted` method.
97pub type R_altinteger_Is_sorted_method_t =
98    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
99/// Signature for ALTINTEGER `no_na` method.
100pub type R_altinteger_No_NA_method_t =
101    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
102/// Signature for ALTINTEGER `sum` method.
103pub type R_altinteger_Sum_method_t =
104    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
105/// Signature for ALTINTEGER `min` method.
106pub type R_altinteger_Min_method_t =
107    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
108/// Signature for ALTINTEGER `max` method.
109pub type R_altinteger_Max_method_t =
110    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
111/// Signature for ALTREAL `elt` method.
112pub type R_altreal_Elt_method_t =
113    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> f64>;
114/// Signature for ALTREAL `get_region` method.
115pub type R_altreal_Get_region_method_t = ::std::option::Option<
116    unsafe extern "C-unwind" fn(sx: SEXP, i: R_xlen_t, n: R_xlen_t, buf: *mut f64) -> R_xlen_t,
117>;
118/// Signature for ALTREAL `is_sorted` method.
119pub type R_altreal_Is_sorted_method_t =
120    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
121/// Signature for ALTREAL `no_na` method.
122pub type R_altreal_No_NA_method_t =
123    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
124/// Signature for ALTREAL `sum` method.
125pub type R_altreal_Sum_method_t =
126    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
127/// Signature for ALTREAL `min` method.
128pub type R_altreal_Min_method_t =
129    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
130/// Signature for ALTREAL `max` method.
131pub type R_altreal_Max_method_t =
132    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
133/// Signature for ALTLOGICAL `elt` method.
134pub type R_altlogical_Elt_method_t = ::std::option::Option<
135    unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> ::std::os::raw::c_int,
136>;
137/// Signature for ALTLOGICAL `get_region` method.
138pub type R_altlogical_Get_region_method_t = ::std::option::Option<
139    unsafe extern "C-unwind" fn(
140        sx: SEXP,
141        i: R_xlen_t,
142        n: R_xlen_t,
143        buf: *mut ::std::os::raw::c_int,
144    ) -> R_xlen_t,
145>;
146/// Signature for ALTLOGICAL `is_sorted` method.
147pub type R_altlogical_Is_sorted_method_t =
148    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
149/// Signature for ALTLOGICAL `no_na` method.
150pub type R_altlogical_No_NA_method_t =
151    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
152/// Signature for ALTLOGICAL `sum` method.
153pub type R_altlogical_Sum_method_t =
154    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, narm: Rboolean) -> SEXP>;
155/// Signature for ALTRAW `elt` method.
156pub type R_altraw_Elt_method_t =
157    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> Rbyte>;
158/// Signature for ALTRAW `get_region` method.
159pub type R_altraw_Get_region_method_t = ::std::option::Option<
160    unsafe extern "C-unwind" fn(sx: SEXP, i: R_xlen_t, n: R_xlen_t, buf: *mut Rbyte) -> R_xlen_t,
161>;
162/// Signature for ALTCOMPLEX `elt` method.
163pub type R_altcomplex_Elt_method_t =
164    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> Rcomplex>;
165/// Signature for ALTCOMPLEX `get_region` method.
166pub type R_altcomplex_Get_region_method_t = ::std::option::Option<
167    unsafe extern "C-unwind" fn(sx: SEXP, i: R_xlen_t, n: R_xlen_t, buf: *mut Rcomplex) -> R_xlen_t,
168>;
169/// Signature for ALTSTRING `elt` method.
170pub type R_altstring_Elt_method_t =
171    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> SEXP>;
172/// Signature for ALTSTRING `set_elt` method.
173pub type R_altstring_Set_elt_method_t =
174    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t, v: SEXP)>;
175/// Signature for ALTSTRING `is_sorted` method.
176pub type R_altstring_Is_sorted_method_t =
177    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
178/// Signature for ALTSTRING `no_na` method.
179pub type R_altstring_No_NA_method_t =
180    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP) -> ::std::os::raw::c_int>;
181/// Signature for ALTLIST `elt` method.
182pub type R_altlist_Elt_method_t =
183    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t) -> SEXP>;
184/// Signature for ALTLIST `set_elt` method.
185pub type R_altlist_Set_elt_method_t =
186    ::std::option::Option<unsafe extern "C-unwind" fn(x: SEXP, i: R_xlen_t, v: SEXP)>;
187#[derive(Clone, Copy)]
188#[repr(C)]
189/// Opaque ALTREP class handle.
190pub struct R_altrep_class_t {
191    /// Underlying class object SEXP.
192    pub ptr: SEXP,
193}
194
195// SAFETY: R_altrep_class_t is only used on R's main thread.
196// The class is created once during package init and stored in a static.
197unsafe impl Send for R_altrep_class_t {}
198unsafe impl Sync for R_altrep_class_t {}
199
200// Imported ALTREP constructor and method-registration symbols.
201#[allow(missing_docs)]
202#[miniextendr_macros::r_ffi_checked]
203unsafe extern "C-unwind" {
204    // ALTREP instance construction (encapsulated by R_altrep_class_t::new_altrep)
205    fn R_new_altrep(aclass: R_altrep_class_t, data1: SEXP, data2: SEXP) -> SEXP;
206
207    // ALTREP class constructors — pub because proc-macro generates calls from user crates
208    pub fn R_make_altstring_class(
209        cname: *const ::std::os::raw::c_char,
210        pname: *const ::std::os::raw::c_char,
211        info: *mut DllInfo,
212    ) -> R_altrep_class_t;
213    pub fn R_make_altinteger_class(
214        cname: *const ::std::os::raw::c_char,
215        pname: *const ::std::os::raw::c_char,
216        info: *mut DllInfo,
217    ) -> R_altrep_class_t;
218    pub fn R_make_altreal_class(
219        cname: *const ::std::os::raw::c_char,
220        pname: *const ::std::os::raw::c_char,
221        info: *mut DllInfo,
222    ) -> R_altrep_class_t;
223    pub fn R_make_altlogical_class(
224        cname: *const ::std::os::raw::c_char,
225        pname: *const ::std::os::raw::c_char,
226        info: *mut DllInfo,
227    ) -> R_altrep_class_t;
228    pub fn R_make_altraw_class(
229        cname: *const ::std::os::raw::c_char,
230        pname: *const ::std::os::raw::c_char,
231        info: *mut DllInfo,
232    ) -> R_altrep_class_t;
233    pub fn R_make_altcomplex_class(
234        cname: *const ::std::os::raw::c_char,
235        pname: *const ::std::os::raw::c_char,
236        info: *mut DllInfo,
237    ) -> R_altrep_class_t;
238    pub fn R_make_altlist_class(
239        cname: *const ::std::os::raw::c_char,
240        pname: *const ::std::os::raw::c_char,
241        info: *mut DllInfo,
242    ) -> R_altrep_class_t;
243    // ALTREP class membership check (encapsulated by R_altrep_class_t::inherits)
244    fn R_altrep_inherits(x: SEXP, aclass: R_altrep_class_t) -> Rboolean;
245
246    // Method setters — private, encapsulated by R_altrep_class_t methods
247    fn R_set_altrep_UnserializeEX_method(
248        cls: R_altrep_class_t,
249        fun: R_altrep_UnserializeEX_method_t,
250    );
251    fn R_set_altrep_Unserialize_method(cls: R_altrep_class_t, fun: R_altrep_Unserialize_method_t);
252    fn R_set_altrep_Serialized_state_method(
253        cls: R_altrep_class_t,
254        fun: R_altrep_Serialized_state_method_t,
255    );
256    fn R_set_altrep_DuplicateEX_method(cls: R_altrep_class_t, fun: R_altrep_DuplicateEX_method_t);
257    fn R_set_altrep_Duplicate_method(cls: R_altrep_class_t, fun: R_altrep_Duplicate_method_t);
258    fn R_set_altrep_Coerce_method(cls: R_altrep_class_t, fun: R_altrep_Coerce_method_t);
259    fn R_set_altrep_Inspect_method(cls: R_altrep_class_t, fun: R_altrep_Inspect_method_t);
260    fn R_set_altrep_Length_method(cls: R_altrep_class_t, fun: R_altrep_Length_method_t);
261    fn R_set_altvec_Dataptr_method(cls: R_altrep_class_t, fun: R_altvec_Dataptr_method_t);
262    fn R_set_altvec_Dataptr_or_null_method(
263        cls: R_altrep_class_t,
264        fun: R_altvec_Dataptr_or_null_method_t,
265    );
266    fn R_set_altvec_Extract_subset_method(
267        cls: R_altrep_class_t,
268        fun: R_altvec_Extract_subset_method_t,
269    );
270    fn R_set_altinteger_Elt_method(cls: R_altrep_class_t, fun: R_altinteger_Elt_method_t);
271    fn R_set_altinteger_Get_region_method(
272        cls: R_altrep_class_t,
273        fun: R_altinteger_Get_region_method_t,
274    );
275    fn R_set_altinteger_Is_sorted_method(
276        cls: R_altrep_class_t,
277        fun: R_altinteger_Is_sorted_method_t,
278    );
279    fn R_set_altinteger_No_NA_method(cls: R_altrep_class_t, fun: R_altinteger_No_NA_method_t);
280    fn R_set_altinteger_Sum_method(cls: R_altrep_class_t, fun: R_altinteger_Sum_method_t);
281    fn R_set_altinteger_Min_method(cls: R_altrep_class_t, fun: R_altinteger_Min_method_t);
282    fn R_set_altinteger_Max_method(cls: R_altrep_class_t, fun: R_altinteger_Max_method_t);
283    fn R_set_altreal_Elt_method(cls: R_altrep_class_t, fun: R_altreal_Elt_method_t);
284    fn R_set_altreal_Get_region_method(cls: R_altrep_class_t, fun: R_altreal_Get_region_method_t);
285    fn R_set_altreal_Is_sorted_method(cls: R_altrep_class_t, fun: R_altreal_Is_sorted_method_t);
286    fn R_set_altreal_No_NA_method(cls: R_altrep_class_t, fun: R_altreal_No_NA_method_t);
287    fn R_set_altreal_Sum_method(cls: R_altrep_class_t, fun: R_altreal_Sum_method_t);
288    fn R_set_altreal_Min_method(cls: R_altrep_class_t, fun: R_altreal_Min_method_t);
289    fn R_set_altreal_Max_method(cls: R_altrep_class_t, fun: R_altreal_Max_method_t);
290    fn R_set_altlogical_Elt_method(cls: R_altrep_class_t, fun: R_altlogical_Elt_method_t);
291    fn R_set_altlogical_Get_region_method(
292        cls: R_altrep_class_t,
293        fun: R_altlogical_Get_region_method_t,
294    );
295    fn R_set_altlogical_Is_sorted_method(
296        cls: R_altrep_class_t,
297        fun: R_altlogical_Is_sorted_method_t,
298    );
299    fn R_set_altlogical_No_NA_method(cls: R_altrep_class_t, fun: R_altlogical_No_NA_method_t);
300    fn R_set_altlogical_Sum_method(cls: R_altrep_class_t, fun: R_altlogical_Sum_method_t);
301    fn R_set_altraw_Elt_method(cls: R_altrep_class_t, fun: R_altraw_Elt_method_t);
302    fn R_set_altraw_Get_region_method(cls: R_altrep_class_t, fun: R_altraw_Get_region_method_t);
303    fn R_set_altcomplex_Elt_method(cls: R_altrep_class_t, fun: R_altcomplex_Elt_method_t);
304    fn R_set_altcomplex_Get_region_method(
305        cls: R_altrep_class_t,
306        fun: R_altcomplex_Get_region_method_t,
307    );
308    fn R_set_altstring_Elt_method(cls: R_altrep_class_t, fun: R_altstring_Elt_method_t);
309    fn R_set_altstring_Set_elt_method(cls: R_altrep_class_t, fun: R_altstring_Set_elt_method_t);
310    fn R_set_altstring_Is_sorted_method(cls: R_altrep_class_t, fun: R_altstring_Is_sorted_method_t);
311    fn R_set_altstring_No_NA_method(cls: R_altrep_class_t, fun: R_altstring_No_NA_method_t);
312    fn R_set_altlist_Elt_method(cls: R_altrep_class_t, fun: R_altlist_Elt_method_t);
313    fn R_set_altlist_Set_elt_method(cls: R_altrep_class_t, fun: R_altlist_Set_elt_method_t);
314}
315
316impl R_altrep_class_t {
317    /// Create from a raw SEXP pointer.
318    ///
319    /// Rust equivalent of C macro `R_SUBTYPE_INIT(x)`.
320    #[inline(always)]
321    pub const fn from_sexp(ptr: SEXP) -> Self {
322        Self { ptr }
323    }
324
325    /// Get the underlying SEXP.
326    ///
327    /// Rust equivalent of C macro `R_SEXP(x)`.
328    #[inline(always)]
329    pub fn as_sexp(self) -> SEXP {
330        self.ptr
331    }
332
333    /// Create a new ALTREP instance with data1 and data2 slots.
334    ///
335    /// # Safety
336    /// Must be called on R's main thread. `data1` and `data2` must be valid SEXPs.
337    #[inline]
338    pub unsafe fn new_altrep(self, data1: SEXP, data2: SEXP) -> SEXP {
339        unsafe { R_new_altrep(self, data1, data2) }
340    }
341
342    /// Create a new ALTREP instance (no thread check).
343    ///
344    /// # Safety
345    /// Must be called on R's main thread.
346    #[inline]
347    pub unsafe fn new_altrep_unchecked(self, data1: SEXP, data2: SEXP) -> SEXP {
348        unsafe { R_new_altrep_unchecked(self, data1, data2) }
349    }
350
351    /// Check if `x` is an instance of this ALTREP class.
352    ///
353    /// # Safety
354    /// Must be called on R's main thread. `x` must be a valid SEXP.
355    #[inline]
356    pub unsafe fn inherits(self, x: SEXP) -> bool {
357        unsafe { R_altrep_inherits(x, self) != Rboolean::FALSE }
358    }
359
360    // region: Base ALTREP method setters
361
362    /// Set the Length method.
363    /// # Safety
364    /// Must be called during R initialization.
365    #[inline]
366    pub unsafe fn set_length_method(self, fun: R_altrep_Length_method_t) {
367        unsafe { R_set_altrep_Length_method(self, fun) }
368    }
369
370    /// Set the Serialized_state method.
371    /// # Safety
372    /// Must be called during R initialization.
373    #[inline]
374    pub unsafe fn set_serialized_state_method(self, fun: R_altrep_Serialized_state_method_t) {
375        unsafe { R_set_altrep_Serialized_state_method(self, fun) }
376    }
377
378    /// Set the Unserialize method.
379    /// # Safety
380    /// Must be called during R initialization.
381    #[inline]
382    pub unsafe fn set_unserialize_method(self, fun: R_altrep_Unserialize_method_t) {
383        unsafe { R_set_altrep_Unserialize_method(self, fun) }
384    }
385
386    /// Set the UnserializeEX method.
387    /// # Safety
388    /// Must be called during R initialization.
389    #[inline]
390    pub unsafe fn set_unserialize_ex_method(self, fun: R_altrep_UnserializeEX_method_t) {
391        unsafe { R_set_altrep_UnserializeEX_method(self, fun) }
392    }
393
394    /// Set the Duplicate method.
395    /// # Safety
396    /// Must be called during R initialization.
397    #[inline]
398    pub unsafe fn set_duplicate_method(self, fun: R_altrep_Duplicate_method_t) {
399        unsafe { R_set_altrep_Duplicate_method(self, fun) }
400    }
401
402    /// Set the DuplicateEX method.
403    /// # Safety
404    /// Must be called during R initialization.
405    #[inline]
406    pub unsafe fn set_duplicate_ex_method(self, fun: R_altrep_DuplicateEX_method_t) {
407        unsafe { R_set_altrep_DuplicateEX_method(self, fun) }
408    }
409
410    /// Set the Coerce method.
411    /// # Safety
412    /// Must be called during R initialization.
413    #[inline]
414    pub unsafe fn set_coerce_method(self, fun: R_altrep_Coerce_method_t) {
415        unsafe { R_set_altrep_Coerce_method(self, fun) }
416    }
417
418    /// Set the Inspect method.
419    /// # Safety
420    /// Must be called during R initialization.
421    #[inline]
422    pub unsafe fn set_inspect_method(self, fun: R_altrep_Inspect_method_t) {
423        unsafe { R_set_altrep_Inspect_method(self, fun) }
424    }
425
426    // endregion
427
428    // region: Vector-level method setters
429
430    /// Set the Dataptr method.
431    /// # Safety
432    /// Must be called during R initialization.
433    #[inline]
434    pub unsafe fn set_dataptr_method(self, fun: R_altvec_Dataptr_method_t) {
435        unsafe { R_set_altvec_Dataptr_method(self, fun) }
436    }
437
438    /// Set the Dataptr_or_null method.
439    /// # Safety
440    /// Must be called during R initialization.
441    #[inline]
442    pub unsafe fn set_dataptr_or_null_method(self, fun: R_altvec_Dataptr_or_null_method_t) {
443        unsafe { R_set_altvec_Dataptr_or_null_method(self, fun) }
444    }
445
446    /// Set the Extract_subset method.
447    /// # Safety
448    /// Must be called during R initialization.
449    #[inline]
450    pub unsafe fn set_extract_subset_method(self, fun: R_altvec_Extract_subset_method_t) {
451        unsafe { R_set_altvec_Extract_subset_method(self, fun) }
452    }
453
454    // endregion
455
456    // region: Integer method setters
457
458    /// Set the integer Elt method.
459    /// # Safety
460    /// Must be called during R initialization.
461    #[inline]
462    pub unsafe fn set_integer_elt_method(self, fun: R_altinteger_Elt_method_t) {
463        unsafe { R_set_altinteger_Elt_method(self, fun) }
464    }
465
466    /// Set the integer Get_region method.
467    /// # Safety
468    /// Must be called during R initialization.
469    #[inline]
470    pub unsafe fn set_integer_get_region_method(self, fun: R_altinteger_Get_region_method_t) {
471        unsafe { R_set_altinteger_Get_region_method(self, fun) }
472    }
473
474    /// Set the integer Is_sorted method.
475    /// # Safety
476    /// Must be called during R initialization.
477    #[inline]
478    pub unsafe fn set_integer_is_sorted_method(self, fun: R_altinteger_Is_sorted_method_t) {
479        unsafe { R_set_altinteger_Is_sorted_method(self, fun) }
480    }
481
482    /// Set the integer No_NA method.
483    /// # Safety
484    /// Must be called during R initialization.
485    #[inline]
486    pub unsafe fn set_integer_no_na_method(self, fun: R_altinteger_No_NA_method_t) {
487        unsafe { R_set_altinteger_No_NA_method(self, fun) }
488    }
489
490    /// Set the integer Sum method.
491    /// # Safety
492    /// Must be called during R initialization.
493    #[inline]
494    pub unsafe fn set_integer_sum_method(self, fun: R_altinteger_Sum_method_t) {
495        unsafe { R_set_altinteger_Sum_method(self, fun) }
496    }
497
498    /// Set the integer Min method.
499    /// # Safety
500    /// Must be called during R initialization.
501    #[inline]
502    pub unsafe fn set_integer_min_method(self, fun: R_altinteger_Min_method_t) {
503        unsafe { R_set_altinteger_Min_method(self, fun) }
504    }
505
506    /// Set the integer Max method.
507    /// # Safety
508    /// Must be called during R initialization.
509    #[inline]
510    pub unsafe fn set_integer_max_method(self, fun: R_altinteger_Max_method_t) {
511        unsafe { R_set_altinteger_Max_method(self, fun) }
512    }
513
514    // endregion
515
516    // region: Real method setters
517
518    /// Set the real Elt method.
519    /// # Safety
520    /// Must be called during R initialization.
521    #[inline]
522    pub unsafe fn set_real_elt_method(self, fun: R_altreal_Elt_method_t) {
523        unsafe { R_set_altreal_Elt_method(self, fun) }
524    }
525
526    /// Set the real Get_region method.
527    /// # Safety
528    /// Must be called during R initialization.
529    #[inline]
530    pub unsafe fn set_real_get_region_method(self, fun: R_altreal_Get_region_method_t) {
531        unsafe { R_set_altreal_Get_region_method(self, fun) }
532    }
533
534    /// Set the real Is_sorted method.
535    /// # Safety
536    /// Must be called during R initialization.
537    #[inline]
538    pub unsafe fn set_real_is_sorted_method(self, fun: R_altreal_Is_sorted_method_t) {
539        unsafe { R_set_altreal_Is_sorted_method(self, fun) }
540    }
541
542    /// Set the real No_NA method.
543    /// # Safety
544    /// Must be called during R initialization.
545    #[inline]
546    pub unsafe fn set_real_no_na_method(self, fun: R_altreal_No_NA_method_t) {
547        unsafe { R_set_altreal_No_NA_method(self, fun) }
548    }
549
550    /// Set the real Sum method.
551    /// # Safety
552    /// Must be called during R initialization.
553    #[inline]
554    pub unsafe fn set_real_sum_method(self, fun: R_altreal_Sum_method_t) {
555        unsafe { R_set_altreal_Sum_method(self, fun) }
556    }
557
558    /// Set the real Min method.
559    /// # Safety
560    /// Must be called during R initialization.
561    #[inline]
562    pub unsafe fn set_real_min_method(self, fun: R_altreal_Min_method_t) {
563        unsafe { R_set_altreal_Min_method(self, fun) }
564    }
565
566    /// Set the real Max method.
567    /// # Safety
568    /// Must be called during R initialization.
569    #[inline]
570    pub unsafe fn set_real_max_method(self, fun: R_altreal_Max_method_t) {
571        unsafe { R_set_altreal_Max_method(self, fun) }
572    }
573
574    // endregion
575
576    // region: Logical method setters
577
578    /// Set the logical Elt method.
579    /// # Safety
580    /// Must be called during R initialization.
581    #[inline]
582    pub unsafe fn set_logical_elt_method(self, fun: R_altlogical_Elt_method_t) {
583        unsafe { R_set_altlogical_Elt_method(self, fun) }
584    }
585
586    /// Set the logical Get_region method.
587    /// # Safety
588    /// Must be called during R initialization.
589    #[inline]
590    pub unsafe fn set_logical_get_region_method(self, fun: R_altlogical_Get_region_method_t) {
591        unsafe { R_set_altlogical_Get_region_method(self, fun) }
592    }
593
594    /// Set the logical Is_sorted method.
595    /// # Safety
596    /// Must be called during R initialization.
597    #[inline]
598    pub unsafe fn set_logical_is_sorted_method(self, fun: R_altlogical_Is_sorted_method_t) {
599        unsafe { R_set_altlogical_Is_sorted_method(self, fun) }
600    }
601
602    /// Set the logical No_NA method.
603    /// # Safety
604    /// Must be called during R initialization.
605    #[inline]
606    pub unsafe fn set_logical_no_na_method(self, fun: R_altlogical_No_NA_method_t) {
607        unsafe { R_set_altlogical_No_NA_method(self, fun) }
608    }
609
610    /// Set the logical Sum method.
611    /// # Safety
612    /// Must be called during R initialization.
613    #[inline]
614    pub unsafe fn set_logical_sum_method(self, fun: R_altlogical_Sum_method_t) {
615        unsafe { R_set_altlogical_Sum_method(self, fun) }
616    }
617
618    // endregion
619
620    // region: Raw method setters
621
622    /// Set the raw Elt method.
623    /// # Safety
624    /// Must be called during R initialization.
625    #[inline]
626    pub unsafe fn set_raw_elt_method(self, fun: R_altraw_Elt_method_t) {
627        unsafe { R_set_altraw_Elt_method(self, fun) }
628    }
629
630    /// Set the raw Get_region method.
631    /// # Safety
632    /// Must be called during R initialization.
633    #[inline]
634    pub unsafe fn set_raw_get_region_method(self, fun: R_altraw_Get_region_method_t) {
635        unsafe { R_set_altraw_Get_region_method(self, fun) }
636    }
637
638    // endregion
639
640    // region: Complex method setters
641
642    /// Set the complex Elt method.
643    /// # Safety
644    /// Must be called during R initialization.
645    #[inline]
646    pub unsafe fn set_complex_elt_method(self, fun: R_altcomplex_Elt_method_t) {
647        unsafe { R_set_altcomplex_Elt_method(self, fun) }
648    }
649
650    /// Set the complex Get_region method.
651    /// # Safety
652    /// Must be called during R initialization.
653    #[inline]
654    pub unsafe fn set_complex_get_region_method(self, fun: R_altcomplex_Get_region_method_t) {
655        unsafe { R_set_altcomplex_Get_region_method(self, fun) }
656    }
657
658    // endregion
659
660    // region: String method setters
661
662    /// Set the string Elt method.
663    /// # Safety
664    /// Must be called during R initialization.
665    #[inline]
666    pub unsafe fn set_string_elt_method(self, fun: R_altstring_Elt_method_t) {
667        unsafe { R_set_altstring_Elt_method(self, fun) }
668    }
669
670    /// Set the string Set_elt method.
671    /// # Safety
672    /// Must be called during R initialization.
673    #[inline]
674    pub unsafe fn set_string_set_elt_method(self, fun: R_altstring_Set_elt_method_t) {
675        unsafe { R_set_altstring_Set_elt_method(self, fun) }
676    }
677
678    /// Set the string Is_sorted method.
679    /// # Safety
680    /// Must be called during R initialization.
681    #[inline]
682    pub unsafe fn set_string_is_sorted_method(self, fun: R_altstring_Is_sorted_method_t) {
683        unsafe { R_set_altstring_Is_sorted_method(self, fun) }
684    }
685
686    /// Set the string No_NA method.
687    /// # Safety
688    /// Must be called during R initialization.
689    #[inline]
690    pub unsafe fn set_string_no_na_method(self, fun: R_altstring_No_NA_method_t) {
691        unsafe { R_set_altstring_No_NA_method(self, fun) }
692    }
693
694    // endregion
695
696    // region: List method setters
697
698    /// Set the list Elt method.
699    /// # Safety
700    /// Must be called during R initialization.
701    #[inline]
702    pub unsafe fn set_list_elt_method(self, fun: R_altlist_Elt_method_t) {
703        unsafe { R_set_altlist_Elt_method(self, fun) }
704    }
705
706    /// Set the list Set_elt method.
707    /// # Safety
708    /// Must be called during R initialization.
709    #[inline]
710    pub unsafe fn set_list_set_elt_method(self, fun: R_altlist_Set_elt_method_t) {
711        unsafe { R_set_altlist_Set_elt_method(self, fun) }
712    }
713
714    // endregion
715}
716
717// region: ALTREP Helper Functions (Rust equivalents of R's ALTREP macros)
718
719/// Extracts the `ptr` field from `R_altrep_class_t`.
720///
721/// Rust equivalent of the C macro `R_SEXP(x)` which expands to `(x).ptr`.
722#[inline(always)]
723pub fn sexp(class: R_altrep_class_t) -> SEXP {
724    class.ptr
725}
726
727/// Creates an `R_altrep_class_t` from a SEXP pointer.
728///
729/// Rust equivalent of the C macro `R_SUBTYPE_INIT(x)` which expands to `{ x }`.
730#[inline(always)]
731pub const fn subtype_init(ptr: SEXP) -> R_altrep_class_t {
732    R_altrep_class_t { ptr }
733}
734
735// endregion