extendr_api/wrapper/
mod.rs1use crate::robj::{GetSexp, Rinternals};
5use crate::*;
6use extendr_ffi::{R_xlen_t, Rf_xlengthgets};
7
8pub mod altrep;
9pub mod complexes;
10pub mod dataframe;
11pub mod doubles;
12pub mod environment;
13pub mod expr;
14pub mod externalptr;
15pub mod function;
16pub mod integers;
17pub mod lang;
18pub mod list;
19pub mod logicals;
20mod macros;
21pub mod matrix;
22pub mod nullable;
23pub mod pairlist;
24pub mod primitive;
25pub mod promise;
26pub mod raw;
27pub mod rstr;
28pub mod s4;
29pub mod strings;
30pub mod symbol;
31pub mod wrapper_macros;
32
33pub use self::rstr::Rstr;
34#[cfg(use_r_altlist)]
35pub use altrep::AltListImpl;
36pub use altrep::{
37 AltComplexImpl, AltIntegerImpl, AltLogicalImpl, AltRawImpl, AltRealImpl, AltStringImpl, Altrep,
38 AltrepImpl,
39};
40pub use complexes::Complexes;
41pub use dataframe::{Dataframe, IntoDataFrameRow};
42pub use doubles::Doubles;
43pub use environment::{EnvIter, Environment};
44pub use expr::Expressions;
45pub use externalptr::ExternalPtr;
46pub use function::Function;
47pub use integers::Integers;
48pub use lang::Language;
49pub use list::{FromList, List, ListIter};
50pub use logicals::Logicals;
51pub use matrix::{
52 ColIter, MatrixConversions, RArray, RColumn, RMatrix, RMatrix3D, RMatrix4D, RMatrix5D, RowIter,
53};
54pub use nullable::Nullable;
55pub use pairlist::{Pairlist, PairlistIter};
56pub use primitive::Primitive;
57pub use promise::Promise;
58pub use raw::Raw;
59pub use s4::S4;
60pub use strings::Strings;
61pub use symbol::Symbol;
62pub use wrapper_macros::*;
63
64fn collect_vector<T, U>(
65 sexptype: SEXPTYPE,
66 data: impl Fn(SEXP) -> *mut U,
67 iter: impl Iterator<Item = T>,
68 convert: impl Fn(T) -> U,
69) -> Robj {
70 unsafe {
71 let mut capacity = iter.size_hint().0;
72 let mut robj = Robj::alloc_vector(sexptype, capacity);
73 let mut ptr = data(robj.get());
74 let mut n = 0;
75 for v in iter {
76 if n == capacity {
77 capacity = (capacity * 2).max(1);
78 robj = Robj::from_sexp(Rf_xlengthgets(robj.get(), capacity as R_xlen_t));
79 ptr = data(robj.get());
80 }
81 ptr.add(n).write(convert(v));
82 n += 1;
83 }
84 if n < capacity {
85 robj = Robj::from_sexp(Rf_xlengthgets(robj.get(), n as R_xlen_t));
86 }
87 robj
88 }
89}