Skip to main content

miniextendr_api/
optionals.rs

1//! Optional feature integrations with third-party crates.
2//!
3//! This module contains all feature-gated integrations with external crates.
4//! Each submodule is only compiled when its corresponding feature is enabled.
5//!
6//! # Available Features
7//!
8//! | Feature | Module | Description |
9//! |---------|--------|-------------|
10//! | `rayon` | `rayon_bridge` | Parallel computation with R interop |
11//! | `rand` | `rand_impl` | R's RNG wrapped for `rand` crate |
12//! | `rand_distr` | - | Re-exports `rand_distr` distributions |
13//! | `either` | `either_impl` | `Either<L, R>` conversions |
14//! | `ndarray` | `ndarray_impl` | N-dimensional array conversions |
15//! | `nalgebra` | `nalgebra_impl` | Linear algebra type conversions |
16//! | `num-bigint` | `num_bigint_impl` | Big integer support |
17//! | `rust_decimal` | `rust_decimal_impl` | Decimal number support |
18//! | `ordered-float` | `ordered_float_impl` | Ordered floats for sorting |
19//! | `uuid` | `uuid_impl` | UUID conversions |
20//! | `regex` | `regex_impl` | Compiled regex from R strings |
21//! | `indexmap` | `indexmap_impl` | Order-preserving maps |
22//! | `time` | `time_impl` | Date/time conversions |
23//! | `jiff` | `jiff_impl` | Date/time conversions with IANA tz (parallel to `time`) |
24//! | `serde` | `serde_impl` | JSON serialization |
25//! | `num-traits` | `num_traits_impl` | Generic numeric operations |
26//! | `bytes` | `bytes_impl` | Byte buffer operations |
27//! | `num-complex` | `num_complex_impl` | Complex number support |
28//! | `url` | `url_impl` | URL parsing and validation |
29//! | `sha2` | `sha2_impl` | Cryptographic hashing |
30//! | `bitflags` | `bitflags_impl` | Bitflag conversions |
31//! | `bitvec` | `bitvec_impl` | Bit vector conversions |
32//! | `aho-corasick` | `aho_corasick_impl` | Multi-pattern string search |
33//! | `toml` | `toml_impl` | TOML parsing |
34//! | `tabled` | `tabled_impl` | Table formatting |
35//! | `tinyvec` | `tinyvec_impl` | Small-vector optimized types |
36//! | `borsh` | `borsh_impl` | Binary serialization |
37
38// region: Rayon - Parallel computation
39
40/// Rayon integration for parallel computation with R interop.
41///
42/// Provides:
43/// - [`with_r_vec`][rayon_bridge::with_r_vec] - Chunk-based parallel fill into R vectors
44/// - [`with_r_vec_map`][rayon_bridge::with_r_vec_map] - Element-wise parallel fill
45/// - [`par_map`][rayon_bridge::par_map] - Transform input slice → R vector
46/// - [`par_map2`][rayon_bridge::par_map2] - Two-input parallel map → R vector
47/// - [`par_map3`][rayon_bridge::par_map3] - Three-input parallel map → R vector
48/// - [`with_r_matrix`][rayon_bridge::with_r_matrix] - Column-wise parallel matrix fill
49/// - [`with_r_array`][rayon_bridge::with_r_array] - Slab-wise parallel array fill
50/// - [`reduce`][rayon_bridge::reduce] - Parallel reductions returning R scalars
51/// - [`RParallelIterator`][rayon_bridge::RParallelIterator] - Adapter trait for R
52///
53/// Enable with `features = ["rayon"]`.
54#[cfg(feature = "rayon")]
55pub mod rayon_bridge;
56#[cfg(feature = "rayon")]
57pub use rayon_bridge::{RParallelExtend, RParallelIterator};
58// endregion
59
60// region: Rand - Random number generation
61
62/// Integration with the `rand` crate for R's RNG.
63///
64/// Provides:
65/// - [`RRng`][rand_impl::RRng] - Wraps R's RNG, implements `rand::Rng`
66/// - [`RDistributions`][rand_impl::RDistributions] - Direct access to R's native distributions
67/// - [`RRngOps`][rand_impl::RRngOps] - Adapter trait for exposing custom RNGs to R
68///
69/// Enable with `features = ["rand"]`.
70#[cfg(feature = "rand")]
71pub mod rand_impl;
72#[cfg(feature = "rand")]
73pub use rand_impl::{RDistributionOps, RDistributions, RRng, RRngOps};
74
75/// Re-export of `rand_distr` for probability distributions.
76///
77/// Provides distributions like `Normal`, `Exp`, `Uniform`, etc. that work
78/// with [`RRng`]. Enable with `features = ["rand_distr"]`.
79#[cfg(feature = "rand_distr")]
80pub use rand_distr;
81// endregion
82
83// region: Either - Sum type
84
85/// Integration with the `either` crate.
86///
87/// Provides [`TryFromSexp`] and [`IntoR`] for [`Either<L, R>`][either::Either].
88///
89/// Enable with `features = ["either"]`.
90#[cfg(feature = "either")]
91pub mod either_impl;
92#[cfg(feature = "either")]
93pub use either_impl::{Either, Left, Right};
94// endregion
95
96// region: Ndarray - N-dimensional arrays
97
98/// Integration with the `ndarray` crate.
99///
100/// Provides conversions between R vectors/matrices and ndarray types
101/// (`Array1`, `Array2`, `ArrayView1`, `ArrayView2`).
102///
103/// Enable with `features = ["ndarray"]`.
104#[cfg(feature = "ndarray")]
105pub mod ndarray_impl;
106#[cfg(feature = "ndarray")]
107pub use ndarray_impl::{
108    // Shared ownership
109    ArcArray1,
110    ArcArray2,
111    // Owned arrays
112    Array0,
113    Array1,
114    Array2,
115    Array3,
116    Array4,
117    Array5,
118    Array6,
119    ArrayD,
120    // Read-only views
121    ArrayView0,
122    ArrayView1,
123    ArrayView2,
124    ArrayView3,
125    ArrayView4,
126    ArrayView5,
127    ArrayView6,
128    ArrayViewD,
129    // Mutable views
130    ArrayViewMut0,
131    ArrayViewMut1,
132    ArrayViewMut2,
133    ArrayViewMut3,
134    ArrayViewMut4,
135    ArrayViewMut5,
136    ArrayViewMut6,
137    ArrayViewMutD,
138    // Index types
139    Ix0,
140    Ix1,
141    Ix2,
142    Ix3,
143    Ix4,
144    Ix5,
145    Ix6,
146    IxDyn,
147    // Adapter traits
148    RNdArrayOps,
149    RNdIndex,
150    RNdSlice,
151    RNdSlice2D,
152    // Shape builder
153    ShapeBuilder,
154};
155#[cfg(feature = "ndarray")]
156pub use ndarray_impl::{RndMat, RndVec};
157// endregion
158
159// region: Nalgebra - Linear algebra
160
161/// Integration with the `nalgebra` crate.
162///
163/// Provides conversions between R vectors/matrices and nalgebra types
164/// (`DVector`, `DMatrix`, `SVector`, `SMatrix`).
165///
166/// Enable with `features = ["nalgebra"]`.
167#[cfg(feature = "nalgebra")]
168pub mod nalgebra_impl;
169#[cfg(feature = "nalgebra")]
170pub use nalgebra_impl::{
171    DMatrix, DVector, RDMatrix, RDVector, RMatrixOps, RVecStorage, RVectorOps, SMatrix, SVector,
172};
173// endregion
174
175// region: Numeric types
176
177/// Integration with the `num-bigint` crate.
178///
179/// Provides conversions for `BigInt` and `BigUint` via R character vectors.
180///
181/// Enable with `features = ["num-bigint"]`.
182#[cfg(feature = "num-bigint")]
183pub mod num_bigint_impl;
184#[cfg(feature = "num-bigint")]
185pub use num_bigint_impl::{
186    BigInt, BigUint, RBigIntBitOps, RBigIntOps, RBigUintBitOps, RBigUintOps,
187};
188
189/// Integration with the `rust_decimal` crate.
190///
191/// Provides conversions for `Decimal` via R character vectors.
192///
193/// Enable with `features = ["rust_decimal"]`.
194#[cfg(feature = "rust_decimal")]
195pub mod rust_decimal_impl;
196#[cfg(feature = "rust_decimal")]
197pub use rust_decimal_impl::{Decimal, RDecimalOps};
198
199/// Integration with the `ordered-float` crate.
200///
201/// Provides conversions for `OrderedFloat<f64>` and `OrderedFloat<f32>`.
202///
203/// Enable with `features = ["ordered-float"]`.
204#[cfg(feature = "ordered-float")]
205pub mod ordered_float_impl;
206#[cfg(feature = "ordered-float")]
207pub use ordered_float_impl::{OrderedFloat, ROrderedFloatOps};
208
209/// Integration with the `num-complex` crate for complex number operations.
210///
211/// Provides conversions between R complex vectors (`CPLXSXP`) and `Complex<f64>`.
212///
213/// Enable with `features = ["num-complex"]`.
214#[cfg(feature = "num-complex")]
215pub mod num_complex_impl;
216#[cfg(feature = "num-complex")]
217pub use num_complex_impl::{Complex, RComplexOps};
218
219/// Integration with the `num-traits` crate for generic numeric operations.
220///
221/// Provides adapter traits for generic numeric types:
222/// - [`RNum`][num_traits_impl::RNum] - Basic numeric operations
223/// - [`RSigned`][num_traits_impl::RSigned] - Signed number operations
224/// - [`RFloat`][num_traits_impl::RFloat] - Floating-point operations
225///
226/// Enable with `features = ["num-traits"]`.
227#[cfg(feature = "num-traits")]
228pub mod num_traits_impl;
229#[cfg(feature = "num-traits")]
230pub use num_traits_impl::{RFloat, RNum, RSigned};
231// endregion
232
233// region: String/Text types
234
235/// UUID support via the `uuid` crate.
236///
237/// Provides conversions between R character vectors and `Uuid` types.
238///
239/// Enable with `features = ["uuid"]`.
240#[cfg(feature = "uuid")]
241pub mod uuid_impl;
242#[cfg(feature = "uuid")]
243pub use uuid_impl::{RUuidOps, Uuid, uuid_helpers};
244
245/// Regex support via the `regex` crate.
246///
247/// Provides compiled regular expressions from R character patterns.
248///
249/// Enable with `features = ["regex"]`.
250#[cfg(feature = "regex")]
251pub mod regex_impl;
252#[cfg(feature = "regex")]
253pub use regex_impl::{CaptureGroups, RCaptureGroups, RRegexOps, Regex};
254
255/// Integration with the `url` crate for URL parsing and validation.
256///
257/// Provides conversions between R character vectors and `Url` types.
258///
259/// Enable with `features = ["url"]`.
260#[cfg(feature = "url")]
261pub mod url_impl;
262#[cfg(feature = "url")]
263pub use url_impl::{RUrlOps, Url, url_helpers};
264
265/// Integration with the `aho-corasick` crate for multi-pattern string search.
266///
267/// Provides fast multi-pattern search using the Aho-Corasick algorithm.
268///
269/// Enable with `features = ["aho-corasick"]`.
270#[cfg(feature = "aho-corasick")]
271pub mod aho_corasick_impl;
272#[cfg(feature = "aho-corasick")]
273pub use aho_corasick_impl::{
274    AhoCorasick, RAhoCorasickOps, aho_compile, aho_count_matches, aho_find_all, aho_find_all_flat,
275    aho_find_first, aho_is_match, aho_replace_all,
276};
277// endregion
278
279// region: Collections
280
281/// IndexMap support via the `indexmap` crate.
282///
283/// Provides conversions between R named lists and `IndexMap<String, T>`.
284///
285/// Enable with `features = ["indexmap"]`.
286#[cfg(feature = "indexmap")]
287pub mod indexmap_impl;
288#[cfg(feature = "indexmap")]
289pub use indexmap_impl::{IndexMap, RIndexMapOps};
290// endregion
291
292// region: Date/Time
293
294/// Time and date support via the `time` crate.
295///
296/// Provides conversions between R date/time types and `time` crate types.
297///
298/// Enable with `features = ["time"]`.
299#[cfg(feature = "time")]
300pub mod time_impl;
301#[cfg(feature = "time")]
302pub use time_impl::{Date, Duration, OffsetDateTime, RDateTimeFormat, RDuration};
303
304/// Date and time conversions via the `jiff` crate — first-class IANA timezone support.
305///
306/// Coexists with the `time` feature. Enable with `features = ["jiff"]`.
307#[cfg(feature = "jiff")]
308pub mod jiff_impl;
309#[cfg(feature = "jiff")]
310pub use jiff_impl::{
311    Date as JiffDate, DateTime as JiffDateTime, JiffTimestampVec, JiffZonedVec, RDate, RDateTime,
312    RSignedDuration, RSpan, RTime, RTimestamp, RZoned, SignedDuration, Span, Time as JiffTime,
313    Timestamp, Zoned,
314};
315// endregion
316
317// region: Serialization
318
319/// Integration with `serde_json` for JSON string serialization.
320///
321/// Provides adapter traits for serializing/deserializing Rust types to/from JSON strings.
322///
323/// Enable with `features = ["serde_json"]`.
324#[cfg(feature = "serde_json")]
325pub mod serde_impl;
326#[cfg(feature = "serde_json")]
327pub use serde_impl::{
328    FactorHandling, JsonOptions, JsonValue, NaHandling, RDeserialize, RJsonBridge, RJsonValueOps,
329    RSerialize, SpecialFloatHandling, json_from_sexp, json_from_sexp_permissive,
330    json_from_sexp_strict, json_from_sexp_with, json_into_sexp,
331};
332
333/// Integration with the `borsh` crate for binary serialization.
334///
335/// Provides [`Borsh<T>`][borsh_impl::Borsh] wrapper for borsh to/from raw vector conversions.
336///
337/// Enable with `features = ["borsh"]`.
338#[cfg(feature = "borsh")]
339pub mod borsh_impl;
340#[cfg(feature = "borsh")]
341pub use borsh_impl::{Borsh, RBorshOps, borsh_from_raw, borsh_to_raw};
342
343/// Integration with the `toml` crate for TOML value conversions.
344///
345/// Provides conversions between TOML values and R types.
346///
347/// Enable with `features = ["toml"]`.
348#[cfg(feature = "toml")]
349pub mod toml_impl;
350#[cfg(feature = "toml")]
351pub use toml_impl::{RTomlOps, TomlValue, toml_from_str, toml_to_string, toml_to_string_pretty};
352// endregion
353
354// region: Byte/Binary handling
355
356/// Integration with the `bytes` crate for byte buffer operations.
357///
358/// Provides adapter traits for byte buffer types.
359///
360/// Enable with `features = ["bytes"]`.
361#[cfg(feature = "bytes")]
362pub mod bytes_impl;
363#[cfg(feature = "bytes")]
364pub use bytes_impl::{Buf, BufMut, Bytes, BytesMut, RBuf, RBufMut};
365
366/// Integration with the `sha2` crate for cryptographic hashing.
367///
368/// Provides SHA-256 and SHA-512 hashing helpers.
369///
370/// Enable with `features = ["sha2"]`.
371#[cfg(feature = "sha2")]
372pub mod sha2_impl;
373#[cfg(feature = "sha2")]
374pub use sha2_impl::{sha256_bytes, sha256_str, sha512_bytes, sha512_str};
375// endregion
376
377// region: Bit manipulation
378
379/// Integration with the `bitflags` crate.
380///
381/// Provides [`RFlags<T>`][bitflags_impl::RFlags] wrapper for bitflags ↔ integer conversions.
382///
383/// Enable with `features = ["bitflags"]`.
384#[cfg(feature = "bitflags")]
385pub mod bitflags_impl;
386#[cfg(feature = "bitflags")]
387pub use bitflags_impl::{Flags, RFlags};
388
389/// Integration with the `bitvec` crate.
390///
391/// Provides conversions between R logical vectors and `BitVec` types.
392///
393/// Enable with `features = ["bitvec"]`.
394#[cfg(feature = "bitvec")]
395pub mod bitvec_impl;
396#[cfg(feature = "bitvec")]
397pub use bitvec_impl::{BitVec, Lsb0, Msb0, RBitVec};
398// endregion
399
400// region: Formatting
401
402/// Integration with the `tabled` crate for table formatting.
403///
404/// Provides helpers for formatting data as ASCII/Unicode tables.
405///
406/// Enable with `features = ["tabled"]`.
407#[cfg(feature = "tabled")]
408pub mod tabled_impl;
409#[cfg(feature = "tabled")]
410pub use tabled_impl::{
411    Builder, Table, Tabled, builder_to_string, table_from_vecs, table_to_string,
412    table_to_string_opts, table_to_string_styled,
413};
414// endregion
415
416// region: TinyVec - Small vector optimization
417
418/// Integration with the `tinyvec` crate for small-vector optimization.
419///
420/// Provides conversions for `TinyVec<[T; N]>` and `ArrayVec<[T; N]>`:
421/// - `TinyVec`: Growable, stores inline up to N elements, then spills to heap
422/// - `ArrayVec`: Fixed capacity N, never allocates, errors if exceeded
423///
424/// Enable with `features = ["tinyvec"]`.
425#[cfg(feature = "tinyvec")]
426pub mod tinyvec_impl;
427#[cfg(feature = "tinyvec")]
428pub use tinyvec_impl::{Array, ArrayVec, TinyVec};
429// endregion
430
431// region: Arrow - Apache Arrow columnar format
432
433/// Integration with the Apache Arrow columnar format.
434///
435/// Provides zero-copy (where possible) conversions between R vectors/data.frames
436/// and Arrow arrays/RecordBatch.
437///
438/// Enable with `features = ["arrow"]`.
439#[cfg(feature = "arrow")]
440pub mod arrow_impl;
441#[cfg(feature = "arrow")]
442pub use arrow_impl::{
443    Array as ArrowArray, ArrayRef, BooleanArray, DataType, Date32Array, DictionaryArray, Field,
444    Float64Array, Int32Array, RecordBatch, Schema, StringArray, StringDictionaryArray,
445    TimestampSecondArray, UInt8Array,
446};
447// endregion
448
449// region: DataFusion - SQL query engine
450
451/// Integration with Apache DataFusion query engine.
452///
453/// Provides `RSessionContext` for running SQL queries on R data frames.
454/// Uses Tokio internally (block_on) so `#[miniextendr]` functions stay sync.
455///
456/// Enable with `features = ["datafusion"]` (implies `arrow`).
457#[cfg(feature = "datafusion")]
458pub mod datafusion_impl;
459#[cfg(feature = "datafusion")]
460pub use datafusion_impl::{RDataFrame, RSessionContext};
461// endregion
462
463// region: Log - Rust logging to R console
464
465/// Integration with the `log` crate for routing Rust diagnostics to R's console.
466///
467/// Enable with `features = ["log"]`.
468#[cfg(feature = "log")]
469pub mod log_impl;
470// endregion