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