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//! | `blake3` | `blake3_impl` | BLAKE3 hashing |
31//! | `md5` | `md5_impl` | MD5 hashing (interop only — broken crypto) |
32//! | `globset` | `globset_impl` | Shell-style glob matching |
33//! | `zstd` | `zstd_impl` | zstd compression |
34//! | `bitflags` | `bitflags_impl` | Bitflag conversions |
35//! | `bitvec` | `bitvec_impl` | Bit vector conversions |
36//! | `aho-corasick` | `aho_corasick_impl` | Multi-pattern string search |
37//! | `toml` | `toml_impl` | TOML parsing |
38//! | `tabled` | `tabled_impl` | Table formatting |
39//! | `tinyvec` | `tinyvec_impl` | Small-vector optimized types |
40//! | `borsh` | `borsh_impl` | Binary serialization |
41//!
42//! # Module template
43//!
44//! Every integration module follows the same regional layout. Use this as
45//! a template when adding a new optional integration:
46//!
47//! ```text
48//! // region: Scalar conversions (TryFromSexp + IntoR for T)
49//! // region: Option conversions (NA support — Option<T>)
50//! // region: Vector conversions (Vec<T>, Box<[T]>, slice views)
51//! // region: Vec<Option<T>> conversions (NA-aware vectors)
52//! // region: R<X>Ops adapter trait (e.g., RUuidOps, RUrlOps —
53//! // convenience methods callable
54//! // from #[miniextendr] code)
55//! // region: Helper functions
56//! // region: Unit tests
57//! ```
58//!
59//! Exceptions: `rayon_bridge.rs` exposes a parallel-iteration API, not a
60//! type-conversion adapter, and does not follow this shape. `serde_impl.rs`
61//! is the JSON adapter (the *native* serde path lives in `crate::serde`).
62//! Array-heavy upstreams (`ndarray_impl`, `arrow_impl`, `nalgebra_impl`,
63//! `jiff_impl`) are larger because their type matrices are larger; the
64//! per-region shape still applies.
65
66// region: Rayon - Parallel computation
67
68/// Rayon integration for parallel computation with R interop.
69///
70/// Provides:
71/// - [`with_r_vec`][rayon_bridge::with_r_vec] - Chunk-based parallel fill into R vectors
72/// - [`with_r_vec_map`][rayon_bridge::with_r_vec_map] - Element-wise parallel fill
73/// - [`par_map`][rayon_bridge::par_map] - Transform input slice → R vector
74/// - [`par_map2`][rayon_bridge::par_map2] - Two-input parallel map → R vector
75/// - [`par_map3`][rayon_bridge::par_map3] - Three-input parallel map → R vector
76/// - [`with_r_matrix`][rayon_bridge::with_r_matrix] - Column-wise parallel matrix fill
77/// - [`with_r_array`][rayon_bridge::with_r_array] - Slab-wise parallel array fill
78/// - [`reduce`][rayon_bridge::reduce] - Parallel reductions returning R scalars
79/// - [`RParallelIterator`] - Adapter trait for R
80///
81/// Enable with `features = ["rayon"]`.
82#[cfg(feature = "rayon")]
83pub mod rayon_bridge;
84#[cfg(feature = "rayon")]
85pub use rayon_bridge::{RParallelExtend, RParallelIterator};
86// endregion
87
88// region: Parallel - thread pool control
89
90/// Thread pool sizing, CRAN compliance (`_R_CHECK_LIMIT_CORES_`), and
91/// cgroup-aware defaults for the rayon bridge. See `docs/RAYON.md`
92/// ("Controlling parallelism from R").
93///
94/// Enable with `features = ["rayon"]`.
95#[cfg(feature = "rayon")]
96pub mod parallel;
97// endregion
98
99// region: Rand - Random number generation
100
101/// Integration with the `rand` crate for R's RNG.
102///
103/// Provides:
104/// - [`RRng`] - Wraps R's RNG, implements `rand::Rng`
105/// - [`RDistributions`] - Direct access to R's native distributions
106/// - [`RRngOps`] - Adapter trait for exposing custom RNGs to R
107///
108/// Enable with `features = ["rand"]`.
109#[cfg(feature = "rand")]
110pub mod rand_impl;
111#[cfg(feature = "rand")]
112pub use rand_impl::{RDistributionOps, RDistributions, RRng, RRngOps};
113
114/// Re-export of `rand` so downstream crates can name its traits
115/// (`RngExt`, `SeedableRng`, …) at the exact version [`RRng`] implements —
116/// a mismatched direct `rand` dependency would fail trait coherence.
117/// Enable with `features = ["rand"]`.
118#[cfg(feature = "rand")]
119pub use rand;
120
121/// Re-export of `rand_distr` for probability distributions.
122///
123/// Provides distributions like `Normal`, `Exp`, `Uniform`, etc. that work
124/// with [`RRng`]. Enable with `features = ["rand_distr"]`.
125#[cfg(feature = "rand_distr")]
126pub use rand_distr;
127// endregion
128
129// region: Either - Sum type
130
131#[cfg(feature = "either")]
132pub mod either_impl;
133#[cfg(feature = "either")]
134pub use either_impl::{Either, Left, Right};
135// endregion
136
137// region: Ndarray - N-dimensional arrays
138
139#[cfg(feature = "ndarray")]
140pub mod ndarray_impl;
141#[cfg(feature = "ndarray")]
142pub use ndarray_impl::{
143 // Shared ownership
144 ArcArray1,
145 ArcArray2,
146 // Owned arrays
147 Array0,
148 Array1,
149 Array2,
150 Array3,
151 Array4,
152 Array5,
153 Array6,
154 ArrayD,
155 // Read-only views
156 ArrayView0,
157 ArrayView1,
158 ArrayView2,
159 ArrayView3,
160 ArrayView4,
161 ArrayView5,
162 ArrayView6,
163 ArrayViewD,
164 // Mutable views
165 ArrayViewMut0,
166 ArrayViewMut1,
167 ArrayViewMut2,
168 ArrayViewMut3,
169 ArrayViewMut4,
170 ArrayViewMut5,
171 ArrayViewMut6,
172 ArrayViewMutD,
173 // Index types
174 Ix0,
175 Ix1,
176 Ix2,
177 Ix3,
178 Ix4,
179 Ix5,
180 Ix6,
181 IxDyn,
182 // Adapter traits
183 RNdArrayOps,
184 RNdIndex,
185 RNdSlice,
186 RNdSlice2D,
187 // Shape builder
188 ShapeBuilder,
189};
190#[cfg(feature = "ndarray")]
191pub use ndarray_impl::{RndMat, RndVec};
192// endregion
193
194// region: Nalgebra - Linear algebra
195
196/// Integration with the `nalgebra` crate.
197///
198/// Provides conversions between R vectors/matrices and nalgebra types
199/// (`DVector`, `DMatrix`, `SVector`, `SMatrix`).
200///
201/// Enable with `features = ["nalgebra"]`.
202#[cfg(feature = "nalgebra")]
203pub mod nalgebra_impl;
204#[cfg(feature = "nalgebra")]
205pub use nalgebra_impl::{
206 DMatrix, DVector, RDMatrix, RDVector, RMatrixOps, RVecStorage, RVectorOps, SMatrix, SVector,
207};
208// endregion
209
210// region: Numeric types
211
212/// Integration with the `num-bigint` crate.
213///
214/// Provides conversions for `BigInt` and `BigUint` via R character vectors.
215///
216/// Enable with `features = ["num-bigint"]`.
217#[cfg(feature = "num-bigint")]
218pub mod num_bigint_impl;
219#[cfg(feature = "num-bigint")]
220pub use num_bigint_impl::{
221 BigInt, BigUint, RBigIntBitOps, RBigIntOps, RBigUintBitOps, RBigUintOps,
222};
223
224/// Integration with the `rust_decimal` crate.
225///
226/// Provides conversions for `Decimal` via R character vectors.
227///
228/// Enable with `features = ["rust_decimal"]`.
229#[cfg(feature = "rust_decimal")]
230pub mod rust_decimal_impl;
231#[cfg(feature = "rust_decimal")]
232pub use rust_decimal_impl::{Decimal, RDecimalOps};
233
234/// Integration with the `ordered-float` crate.
235///
236/// Provides conversions for `OrderedFloat<f64>` and `OrderedFloat<f32>`.
237///
238/// Enable with `features = ["ordered-float"]`.
239#[cfg(feature = "ordered-float")]
240pub mod ordered_float_impl;
241#[cfg(feature = "ordered-float")]
242pub use ordered_float_impl::{OrderedFloat, ROrderedFloatOps};
243
244/// Integration with the `num-complex` crate for complex number operations.
245///
246/// Provides conversions between R complex vectors (`CPLXSXP`) and `Complex<f64>`.
247///
248/// Enable with `features = ["num-complex"]`.
249#[cfg(feature = "num-complex")]
250pub mod num_complex_impl;
251#[cfg(feature = "num-complex")]
252pub use num_complex_impl::{Complex, RComplexOps};
253
254/// Integration with the `num-traits` crate for generic numeric operations.
255///
256/// Provides adapter traits for generic numeric types:
257/// - [`RNum`] - Basic numeric operations
258/// - [`RSigned`] - Signed number operations
259/// - [`RFloat`] - Floating-point operations
260///
261/// Enable with `features = ["num-traits"]`.
262#[cfg(feature = "num-traits")]
263pub mod num_traits_impl;
264#[cfg(feature = "num-traits")]
265pub use num_traits_impl::{RFloat, RNum, RSigned};
266// endregion
267
268// region: String/Text types
269
270/// UUID support via the `uuid` crate.
271///
272/// Provides conversions between R character vectors and `Uuid` types.
273///
274/// Enable with `features = ["uuid"]`.
275#[cfg(feature = "uuid")]
276pub mod uuid_impl;
277#[cfg(feature = "uuid")]
278pub use uuid_impl::{RUuidOps, Uuid, uuid_helpers};
279
280/// Regex support via the `regex` crate.
281///
282/// Provides compiled regular expressions from R character patterns.
283///
284/// Enable with `features = ["regex"]`.
285#[cfg(feature = "regex")]
286pub mod regex_impl;
287#[cfg(feature = "regex")]
288pub use regex_impl::{CaptureGroups, RCaptureGroups, RRegexOps, Regex};
289
290/// Integration with the `url` crate for URL parsing and validation.
291///
292/// Provides conversions between R character vectors and `Url` types.
293///
294/// Enable with `features = ["url"]`.
295#[cfg(feature = "url")]
296pub mod url_impl;
297#[cfg(feature = "url")]
298pub use url_impl::{RUrlOps, Url, url_helpers};
299
300/// Integration with the `aho-corasick` crate for multi-pattern string search.
301///
302/// Provides fast multi-pattern search using the Aho-Corasick algorithm.
303///
304/// Enable with `features = ["aho-corasick"]`.
305#[cfg(feature = "aho-corasick")]
306pub mod aho_corasick_impl;
307#[cfg(feature = "aho-corasick")]
308pub use aho_corasick_impl::{
309 AhoCorasick, RAhoCorasickOps, aho_compile, aho_count_matches, aho_find_all, aho_find_all_flat,
310 aho_find_first, aho_is_match, aho_replace_all,
311};
312
313/// Integration with the `globset` crate for shell-style glob matching.
314///
315/// Provides multi-pattern glob matching with path-aware defaults.
316///
317/// Enable with `features = ["globset"]`.
318#[cfg(feature = "globset")]
319pub mod globset_impl;
320#[cfg(feature = "globset")]
321pub use globset_impl::{
322 Glob, GlobBuilder, GlobOptions, GlobSet, GlobSetBuilder, build_globset, globset_is_match,
323 globset_matches,
324};
325// endregion
326
327// region: Collections
328
329/// IndexMap support via the `indexmap` crate.
330///
331/// Provides conversions between R named lists and `IndexMap<String, T>`.
332///
333/// Enable with `features = ["indexmap"]`.
334#[cfg(feature = "indexmap")]
335pub mod indexmap_impl;
336#[cfg(feature = "indexmap")]
337pub use indexmap_impl::{IndexMap, RIndexMapOps};
338// endregion
339
340// region: Date/Time
341
342// Shared macro for the REALSXP-backed datetime conversion matrix (8 impls per type).
343// Used by time_impl and jiff_impl; not a public export.
344#[cfg(any(feature = "time", feature = "jiff"))]
345mod datetime_realsxp;
346
347/// Time and date support via the `time` crate.
348///
349/// Provides conversions between R date/time types and `time` crate types.
350///
351/// Enable with `features = ["time"]`.
352#[cfg(feature = "time")]
353pub mod time_impl;
354#[cfg(feature = "time")]
355pub use time_impl::{Date, Duration, OffsetDateTime, RDateTimeFormat, RDuration};
356
357/// Date and time conversions via the `jiff` crate — first-class IANA timezone support.
358///
359/// Coexists with the `time` feature. Enable with `features = ["jiff"]`.
360#[cfg(feature = "jiff")]
361pub mod jiff_impl;
362#[cfg(feature = "jiff")]
363pub use jiff_impl::{
364 Date as JiffDate, DateTime as JiffDateTime, JiffTimestampVec, JiffZonedVec, RDate, RDateTime,
365 RSignedDuration, RSpan, RTime, RTimestamp, RZoned, SignedDuration, Span, Time as JiffTime,
366 Timestamp, Zoned,
367};
368// endregion
369
370// region: Serialization
371
372/// Integration with `serde_json` for JSON string serialization.
373///
374/// Provides adapter traits for serializing/deserializing Rust types to/from JSON strings.
375///
376/// Enable with `features = ["serde_json"]`.
377#[cfg(feature = "serde_json")]
378pub mod serde_impl;
379#[cfg(feature = "serde_json")]
380pub use serde_impl::{
381 FactorHandling, JsonOptions, JsonValue, NaHandling, RDeserialize, RJsonBridge, RJsonValueOps,
382 RSerialize, SpecialFloatHandling, json_from_sexp, json_from_sexp_permissive,
383 json_from_sexp_strict, json_from_sexp_with, json_into_sexp,
384};
385
386/// Integration with the `borsh` crate for binary serialization.
387///
388/// Provides [`Borsh<T>`][borsh_impl::Borsh] wrapper for borsh to/from raw vector conversions.
389///
390/// Enable with `features = ["borsh"]`.
391#[cfg(feature = "borsh")]
392pub mod borsh_impl;
393#[cfg(feature = "borsh")]
394pub use borsh_impl::{Borsh, RBorshOps, borsh_from_raw, borsh_to_raw};
395
396/// Integration with the `toml` crate for TOML value conversions.
397///
398/// Provides conversions between TOML values and R types.
399///
400/// Enable with `features = ["toml"]`.
401#[cfg(feature = "toml")]
402pub mod toml_impl;
403#[cfg(feature = "toml")]
404pub use toml_impl::{RTomlOps, TomlValue, toml_from_str, toml_to_string, toml_to_string_pretty};
405// endregion
406
407// region: Byte/Binary handling
408
409/// Integration with the `bytes` crate for byte buffer operations.
410///
411/// Provides adapter traits for byte buffer types.
412///
413/// Enable with `features = ["bytes"]`.
414#[cfg(feature = "bytes")]
415pub mod bytes_impl;
416#[cfg(feature = "bytes")]
417pub use bytes_impl::{Buf, BufMut, Bytes, BytesMut, RBuf, RBufMut};
418
419/// Integration with the `sha2` crate for cryptographic hashing.
420///
421/// Provides SHA-256 and SHA-512 hashing helpers.
422///
423/// Enable with `features = ["sha2"]`.
424#[cfg(feature = "sha2")]
425pub mod sha2_impl;
426#[cfg(feature = "sha2")]
427pub use sha2_impl::{sha256_bytes, sha256_str, sha512_bytes, sha512_str};
428
429/// Integration with the `blake3` crate for fast cryptographic hashing.
430///
431/// Provides BLAKE3 hashing helpers (hex + raw 32-byte digest).
432///
433/// Enable with `features = ["blake3"]`.
434#[cfg(feature = "blake3")]
435pub mod blake3_impl;
436#[cfg(feature = "blake3")]
437pub use blake3_impl::{blake3_bytes, blake3_hex, blake3_str};
438
439/// Integration with the `md5` crate for legacy hashing.
440///
441/// MD5 is cryptographically broken — exposed for interop/checksums only.
442///
443/// Enable with `features = ["md5"]`.
444#[cfg(feature = "md5")]
445pub mod md5_impl;
446#[cfg(feature = "md5")]
447pub use md5_impl::{md5_bytes, md5_hex, md5_str};
448
449/// Integration with the `zstd` crate for whole-buffer compression.
450///
451/// Provides `zstd_compress` / `zstd_decompress` (R raw in / raw out).
452///
453/// Enable with `features = ["zstd"]`.
454#[cfg(feature = "zstd")]
455pub mod zstd_impl;
456#[cfg(feature = "zstd")]
457pub use zstd_impl::{zstd_compress, zstd_decompress};
458// endregion
459
460// region: Bit manipulation
461
462/// Integration with the `bitflags` crate.
463///
464/// Provides [`RFlags<T>`][bitflags_impl::RFlags] wrapper for bitflags ↔ integer conversions.
465///
466/// Enable with `features = ["bitflags"]`.
467#[cfg(feature = "bitflags")]
468pub mod bitflags_impl;
469#[cfg(feature = "bitflags")]
470pub use bitflags_impl::{Flags, RFlags};
471
472/// Integration with the `bitvec` crate.
473///
474/// Provides conversions between R logical vectors and `BitVec` types.
475///
476/// Enable with `features = ["bitvec"]`.
477#[cfg(feature = "bitvec")]
478pub mod bitvec_impl;
479#[cfg(feature = "bitvec")]
480pub use bitvec_impl::{BitVec, Lsb0, Msb0, RBitVec};
481// endregion
482
483// region: Formatting
484
485/// Integration with the `tabled` crate for table formatting.
486///
487/// Provides helpers for formatting data as ASCII/Unicode tables.
488///
489/// Enable with `features = ["tabled"]`.
490#[cfg(feature = "tabled")]
491pub mod tabled_impl;
492#[cfg(feature = "tabled")]
493pub use tabled_impl::{
494 Builder, Table, Tabled, builder_to_string, table_from_vecs, table_to_string,
495 table_to_string_opts, table_to_string_styled,
496};
497// endregion
498
499// region: TinyVec - Small vector optimization
500
501#[cfg(feature = "tinyvec")]
502pub mod tinyvec_impl;
503#[cfg(feature = "tinyvec")]
504pub use tinyvec_impl::{Array, ArrayVec, TinyVec};
505// endregion
506
507// region: Arrow - Apache Arrow columnar format
508
509/// Integration with the Apache Arrow columnar format.
510///
511/// Provides zero-copy (where possible) conversions between R vectors/data.frames
512/// and Arrow arrays/RecordBatch.
513///
514/// Enable with `features = ["arrow"]`.
515#[cfg(feature = "arrow")]
516pub mod arrow_impl;
517#[cfg(feature = "arrow")]
518pub use arrow_impl::{
519 Array as ArrowArray, ArrayRef, BooleanArray, DataType, Date32Array, DictionaryArray, Field,
520 Float64Array, Int32Array, RecordBatch, Schema, StringArray, StringDictionaryArray,
521 TimestampSecondArray, UInt8Array,
522};
523// endregion
524
525// region: DataFusion - SQL query engine
526
527/// Integration with Apache DataFusion query engine.
528///
529/// Provides `RSessionContext` for running SQL queries on R data frames.
530/// Uses Tokio internally (block_on) so `#[miniextendr]` functions stay sync.
531///
532/// Enable with `features = ["datafusion"]` (implies `arrow`).
533#[cfg(feature = "datafusion")]
534pub mod datafusion_impl;
535#[cfg(feature = "datafusion")]
536pub use datafusion_impl::{RDataFrame, RSessionContext};
537// endregion
538
539// region: Log - Rust logging to R console
540
541/// Integration with the `log` crate for routing Rust diagnostics to R's console.
542///
543/// Enable with `features = ["log"]`.
544#[cfg(feature = "log")]
545pub mod log_impl;
546// endregion