rustmax/
lib.rs

1#![doc = include_str!("../doc-src/root-docs.md")]
2#![allow(clippy::needless_doctest_main)]
3/* ---------- */
4#![no_std]
5
6/* ---------- */
7
8pub mod prelude {
9    //! The `rmx` prelude.
10
11    /* standard library preludes */
12
13    #[cfg(all(feature = "rmx-rustlib-core", not(feature = "rmx-rustlib-std")))]
14    pub use ::core::prelude::rust_2021::*;
15
16    #[cfg(feature = "rmx-rustlib-std")]
17    pub use ::std::prelude::rust_2021::*;
18
19    /* standard library macros */
20
21    #[cfg(feature = "rmx-rustlib-alloc")]
22    pub use ::alloc::{format, vec};
23
24    #[cfg(feature = "rmx-rustlib-alloc")]
25    pub use crate::extras::fmt;
26
27    /* standard library exports that aren't in its prelude */
28
29    // Ordering is recommended by
30    // `clippy::comparison_chain` and if it's
31    // important enough that the compiler suggests
32    // using it instead of comparison operator syntax,
33    // let's put it in the prelude.
34    #[cfg(feature = "rmx-rustlib-core")]
35    pub use ::core::cmp::Ordering;
36
37    /* other preludes */
38
39    #[cfg(feature = "futures")]
40    pub use ::futures::prelude::*;
41
42    /* common non-std imports */
43
44    #[cfg(feature = "anyhow")]
45    pub use ::anyhow::{Context as _, anyhow, bail};
46
47    #[cfg(feature = "anyhow")]
48    pub use crate::extras::{A, AnyError, AnyResult};
49
50    #[cfg(feature = "cfg-if")]
51    pub use ::cfg_if::cfg_if;
52
53    #[cfg(feature = "extension-trait")]
54    pub use ::extension_trait::extension_trait;
55
56    #[cfg(feature = "log")]
57    pub use ::log::{debug, error, info, trace, warn};
58
59    #[cfg(all(feature = "futures", feature = "rmx-feature-default"))]
60    pub use ::futures::{executor::block_on, future::Either};
61
62    #[cfg(feature = "itertools")]
63    pub use ::itertools::Itertools as _;
64
65    #[cfg(feature = "rand")]
66    pub use ::rand::RngCore as _;
67    #[cfg(feature = "rand")]
68    pub use ::rand::Rng as _;
69    #[cfg(feature = "rand")]
70    pub use ::rand::SeedableRng as _;
71    #[cfg(feature = "rand")]
72    pub use ::rand::Fill as _;
73
74    /* extras */
75
76    pub use crate::extras::default;
77
78    #[cfg(feature = "rmx-rustlib-core")]
79    pub use crate::bug;
80
81    #[cfg(feature = "extension-trait")]
82    pub use crate::extras::RangeExt as _;
83
84    /* powerletters */
85
86    #[cfg(feature = "powerletters")]
87    pub use ::powerletters::*;
88}
89
90pub mod extras {
91    //! Additional tidbits defined by `rmx`.
92
93    /// Like 'unimplemented' but shorter to type.
94    #[cfg(feature = "rmx-rustlib-core")]
95    #[macro_export]
96    macro_rules! bug {
97        () => {
98            core::panic!("unexpected case (bug!)")
99        };
100        ($($arg:tt)+) => {
101            core::panic!("unexpected case (bug!): {}", $crate::format_args!($($arg)+))
102        };
103    }
104
105    #[cfg(feature = "anyhow")]
106    pub use ::anyhow::{Error as AnyError, Result as AnyResult, anyhow as A};
107
108    #[cfg(feature = "rmx-rustlib-alloc")]
109    pub use ::alloc::format as fmt;
110
111    pub fn default<T: Default>() -> T {
112        Default::default()
113    }
114
115    pub fn init() {
116        #[cfg(feature = "env_logger")]
117        fn maybe_init_env_logger() {
118            crate::env_logger::Builder::new()
119                .filter_level(log::LevelFilter::Info)
120                .format_timestamp(None)
121                .parse_default_env()
122                .init();
123        }
124        #[cfg(not(feature = "env_logger"))]
125        fn maybe_init_env_logger() {}
126
127        maybe_init_env_logger();
128    }
129
130    pub fn init_crate_name(crate_name: &str) {
131        #[cfg(feature = "env_logger")]
132        fn maybe_init_env_logger(crate_name: &str) {
133            crate::env_logger::Builder::new()
134                .filter_module(crate_name, log::LevelFilter::Info)
135                .format_timestamp(None)
136                .parse_default_env()
137                .init();
138        }
139        #[cfg(not(feature = "env_logger"))]
140        fn maybe_init_env_logger(_crate_name: &str) {}
141
142        maybe_init_env_logger(crate_name);
143    }
144
145    pub fn recurse<F, R>(f: F) -> R
146    where
147        F: FnOnce() -> R,
148    {
149        // todo could grow stack here
150        f()
151    }
152
153    // todo: define this for generic Range<N>
154    // todo: put this in a crate and elaborate
155    #[cfg(feature = "extension-trait")]
156    #[extension_trait::extension_trait]
157    pub impl RangeExt for core::ops::Range<usize> {
158        fn from_start_len(start: usize, len: usize) -> Option<core::ops::Range<usize>> {
159            Some(start..start.checked_add(len)?)
160        }
161
162        fn subrange(&self, sub: core::ops::Range<usize>) -> Option<core::ops::Range<usize>> {
163            if sub.start >= self.len() || sub.end > self.len() {
164                return None;
165            }
166            let new_start = self.start.checked_add(sub.start);
167            let new_end = self.start.checked_add(sub.end);
168            match (new_start, new_end) {
169                (Some(new_start), Some(new_end)) => Some(new_start..new_end),
170                _ => None,
171            }
172        }
173
174        fn checked_sub(&self, other: usize) -> Option<core::ops::Range<usize>> {
175            let new_start = self.start.checked_sub(other);
176            let new_end = self.end.checked_sub(other);
177            match (new_start, new_end) {
178                (Some(new_start), Some(new_end)) => Some(new_start..new_end),
179                _ => None,
180            }
181        }
182    }
183
184    #[cfg(feature = "rmx-rustlib-std")]
185    pub fn copy_dir_recursive(
186        src: &crate::std::path::Path,
187        dst: &crate::std::path::Path,
188    ) -> crate::std::io::Result<()> {
189        crate::std::fs::create_dir_all(dst)?;
190
191        for entry in crate::std::fs::read_dir(src)? {
192            let entry = entry?;
193            let file_type = entry.file_type()?;
194            let src_path = entry.path();
195            let dst_path = dst.join(entry.file_name());
196
197            if file_type.is_dir() {
198                copy_dir_recursive(&src_path, &dst_path)?;
199            } else {
200                crate::std::fs::copy(&src_path, &dst_path)?;
201            }
202        }
203
204        Ok(())
205    }
206
207    /// Use in constant contexts to assert a type is `Sync + Sync`.p
208    ///
209    /// ```
210    /// # use rustmax as rmx;
211    /// use rmx::extras::assert_send_sync;
212    ///
213    /// struct DbPathGen(());
214    ///
215    /// const _ASSERT_SEND_SYNC: () = assert_send_sync::<DbPathGen>();
216    /// ```
217    pub const fn assert_send_sync<T: Send + Sync>() { }    
218}
219
220/* ---------- */
221
222#[cfg(feature = "rmx-rustlib-core")]
223#[doc(inline)]
224pub extern crate core;
225
226#[cfg(feature = "rmx-rustlib-alloc")]
227#[doc(inline)]
228pub extern crate alloc;
229
230#[cfg(feature = "rmx-rustlib-std")]
231#[doc(inline)]
232pub extern crate std;
233
234#[cfg(feature = "rmx-rustlib-proc_macro")]
235#[doc(inline)]
236pub extern crate proc_macro;
237
238/* ---------- */
239
240#[cfg(feature = "ahash")]
241pub mod ahash {
242    #![doc = include_str!("../doc-src/crate-ahash.md")]
243
244    pub use ::ahash::*;
245}
246
247#[cfg(feature = "anyhow")]
248pub mod anyhow {
249    #![doc = include_str!("../doc-src/crate-anyhow.md")]
250
251    pub use ::anyhow::*;
252}
253
254#[cfg(feature = "axum")]
255pub mod axum {
256    //! Web application framework based on [`tokio`](super::tokio).
257    //!
258    //! See crate [`::axum`].
259
260    pub use ::axum::*;
261}
262
263#[cfg(feature = "backtrace")]
264pub mod backtrace {
265    //! Callstack backtraces on demand.
266    //!
267    //! See crate [`::backtrace`].
268
269    pub use ::backtrace::*;
270}
271
272#[cfg(feature = "base64")]
273pub mod base64 {
274    #![doc = include_str!("../doc-src/crate-base64.md")]
275
276    pub use ::base64::*;
277}
278
279#[cfg(feature = "bindgen")]
280pub mod bindgen {
281    //! Generate Rust bindings to C and C++ libraries.
282    //!
283    //! See crate [`::bindgen`].
284
285    pub use ::bindgen::*;
286}
287
288#[cfg(feature = "bitflags")]
289pub mod bitflags {
290    #![doc = include_str!("../doc-src/crate-bitflags.md")]
291
292    pub use ::bitflags::*;
293}
294
295#[cfg(feature = "blake3")]
296pub mod blake3 {
297    #![doc = include_str!("../doc-src/crate-blake3.md")]
298
299    pub use ::blake3::*;
300}
301
302#[cfg(feature = "bytes")]
303pub mod bytes {
304    #![doc = include_str!("../doc-src/crate-bytes.md")]
305
306    pub use ::bytes::*;
307}
308
309#[cfg(feature = "cc")]
310pub mod cc {
311    //! A basic cross-platform C compiler driver.
312    //!
313    //! See crate [`::cc`].
314
315    pub use ::cc::*;
316}
317
318#[cfg(feature = "cfg-if")]
319pub mod cfg_if {
320    //! A macro for writing conditional compilation as `if` / `else` blocks.
321    //!
322    //! See crate [`::cfg_if`].
323
324    pub use ::cfg_if::*;
325}
326
327#[cfg(feature = "chrono")]
328pub mod chrono {
329    #![doc = include_str!("../doc-src/crate-chrono.md")]
330
331    pub use ::chrono::*;
332}
333
334#[cfg(feature = "clap")]
335pub mod clap {
336    #![doc = include_str!("../doc-src/crate-clap.md")]
337
338    pub use ::clap::*;
339}
340
341
342#[cfg(feature = "ctrlc")]
343pub mod ctrlc {
344    //! Simple handling of CTRL-C for CLI programs.
345    //!
346    //! See crate [`::ctrlc`].
347
348    pub use ::ctrlc::*;
349}
350
351#[cfg(feature = "crossbeam")]
352pub mod crossbeam {
353    #![doc = include_str!("../doc-src/crate-crossbeam.md")]
354
355    pub use ::crossbeam::*;
356}
357
358#[cfg(feature = "cxx")]
359pub mod cxx {
360    //! C++ bridge runtime support; paired with [`cxx_build`](super::cxx_build).
361    //!
362    //! See crate [`::cxx`].
363
364    pub use ::cxx::*;
365}
366
367#[cfg(feature = "cxx-build")]
368pub mod cxx_build {
369    //! C++ bridge generator; paired with [`cxx`](super::cxx).
370    //!
371    //! See crate [`::cxx`].
372
373    pub use ::cxx::*;
374}
375
376#[cfg(feature = "derive_more")]
377pub mod derive_more {
378    #![doc = include_str!("../doc-src/crate-derive_more.md")]
379
380    pub use ::derive_more::*;
381}
382
383#[cfg(feature = "env_logger")]
384pub mod env_logger {
385    #![doc = include_str!("../doc-src/crate-env_logger.md")]
386
387    pub use ::env_logger::*;
388}
389
390#[cfg(feature = "extension-trait")]
391pub mod extension_trait {
392    //! A macro for defining extension methods to external types.
393    //!
394    //! See crate [`::extension_trait`].
395
396    pub use ::extension_trait::*;
397}
398
399#[cfg(feature = "flate2")]
400pub mod flate2 {
401    #![doc = include_str!("../doc-src/crate-flate2.md")]
402
403    pub use ::flate2::*;
404}
405
406#[cfg(feature = "futures")]
407pub mod futures {
408    #![doc = include_str!("../doc-src/crate-futures.md")]
409
410    pub use ::futures::*;
411}
412
413#[cfg(feature = "glob")]
414pub mod glob {
415    #![doc = include_str!("../doc-src/crate-glob.md")]
416
417    pub use ::glob::*;
418}
419
420#[cfg(feature = "hex")]
421pub mod hex {
422    #![doc = include_str!("../doc-src/crate-hex.md")]
423
424    pub use ::hex::*;
425}
426
427#[cfg(feature = "http")]
428pub mod http {
429    //! Shared definitions related to the HTTP protocol.
430    //!
431    //! See crate [`::http`].
432
433    pub use ::http::*;
434}
435
436#[cfg(feature = "hyper")]
437pub mod hyper {
438    //! HTTP, versions 1 and 2.
439    //!
440    //! See crate [`::hyper`].
441
442    pub use ::hyper::*;
443}
444
445#[cfg(feature = "itertools")]
446pub mod itertools {
447    #![doc = include_str!("../doc-src/crate-itertools.md")]
448
449    pub use ::itertools::*;
450}
451
452#[cfg(feature = "jiff")]
453pub mod jiff {
454    #![doc = include_str!("../doc-src/crate-jiff.md")]
455
456    pub use ::jiff::*;
457}
458
459#[cfg(feature = "json5")]
460pub mod json5 {
461    //! JSON5, a superset of JSON with expanded syntax.
462    //!
463    //! See crate [`::json5`].
464
465    pub use ::json5::*;
466}
467
468#[cfg(feature = "libc")]
469pub mod libc {
470    //! Bindings to the C standard library.
471    //!
472    //! See crate [`::libc`].
473
474    pub use ::libc::*;
475}
476
477#[cfg(feature = "log")]
478pub mod log {
479    #![doc = include_str!("../doc-src/crate-log.md")]
480
481    pub use ::log::*;
482}
483
484#[cfg(feature = "mime")]
485pub mod mime {
486    #![doc = include_str!("../doc-src/crate-mime.md")]
487
488    pub use ::mime::*;
489}
490
491#[cfg(feature = "nom")]
492pub mod nom {
493    #![doc = include_str!("../doc-src/crate-nom.md")]
494
495    pub use ::nom::*;
496}
497
498#[cfg(feature = "num-bigint")]
499pub mod num_bigint {
500    #![doc = include_str!("../doc-src/crate-num-bigint.md")]
501
502    pub use ::num_bigint::*;
503}
504
505#[cfg(feature = "num_cpus")]
506pub mod num_cpus {
507    //! Get the number of CPUS on a machine.
508    //!
509    //! See crate [`::num_cpus`].
510
511    pub use ::num_cpus::*;
512}
513
514#[cfg(feature = "num_enum")]
515pub mod num_enum {
516    #![doc = include_str!("../doc-src/crate-num_enum.md")]
517
518    pub use ::num_enum::*;
519}
520
521#[cfg(feature = "powerletters")]
522pub mod powerletters {
523    #![doc = include_str!("../doc-src/crate-powerletters.md")]
524
525    pub use ::powerletters::*;
526}
527
528#[cfg(feature = "proc-macro2")]
529pub mod proc_macro2 {
530    //! A preferred wrapper around the standard [`proc_macro`] crate.
531    //!
532    //! See crate [`::proc_macro2`].
533
534    pub use ::proc_macro2::*;
535}
536
537#[cfg(feature = "proptest")]
538pub mod proptest {
539    #![doc = include_str!("../doc-src/crate-proptest.md")]
540
541    pub use ::proptest::*;
542}
543
544#[cfg(feature = "quote")]
545pub mod quote {
546    //! The `quote!` macro for turning code blocks into source tokens.
547    //!
548    //! See crate [`::quote`].
549
550    pub use ::quote::*;
551}
552
553#[cfg(feature = "rand")]
554pub mod rand {
555    #![doc = include_str!("../doc-src/crate-rand.md")]
556
557    pub use ::rand::*;
558}
559
560#[cfg(feature = "rand_chacha")]
561pub mod rand_chacha {
562    //! The ChaCha cryptographically-secure random number generators.
563    //!
564    //! See crate [`::rand_chacha`].
565
566    pub use ::rand_chacha::*;
567}
568
569#[cfg(feature = "rand_pcg")]
570pub mod rand_pcg {
571    //! The PCG non-cryptographically-secure random number generators.
572    //!
573    //! See crate [`::rand_pcg`].
574
575    pub use ::rand_pcg::*;
576}
577
578#[cfg(feature = "rayon")]
579pub mod rayon {
580    #![doc = include_str!("../doc-src/crate-rayon.md")]
581
582    pub use ::rayon::*;
583}
584
585#[cfg(feature = "regex")]
586pub mod regex {
587    #![doc = include_str!("../doc-src/crate-regex.md")]
588
589    pub use ::regex::*;
590}
591
592#[cfg(feature = "reqwest")]
593pub mod reqwest {
594    //! Simple HTTP requests, synchronous and asynchronous.
595    //!
596    //! See crate [`::reqwest`].
597
598    pub use ::reqwest::*;
599}
600
601
602#[cfg(feature = "rustyline")]
603pub mod rustyline {
604    //! Command-line input reading with history.
605    //!
606    //! See crate [`::rustyline`].
607
608    pub use ::rustyline::*;
609}
610
611#[cfg(feature = "semver")]
612pub mod semver {
613    #![doc = include_str!("../doc-src/crate-semver.md")]
614
615    pub use ::semver::*;
616}
617
618#[cfg(feature = "serde")]
619pub mod serde {
620    #![doc = include_str!("../doc-src/crate-serde.md")]
621
622    pub use ::serde::*;
623}
624
625#[cfg(feature = "serde_json")]
626pub mod serde_json {
627    #![doc = include_str!("../doc-src/crate-serde_json.md")]
628
629    pub use ::serde_json::*;
630}
631
632#[cfg(feature = "sha2")]
633pub mod sha2 {
634    #![doc = include_str!("../doc-src/crate-sha2.md")]
635
636    pub use ::sha2::*;
637}
638
639#[cfg(feature = "socket2")]
640pub mod socket2 {
641    //! Low-level network socket programming beyond [`std::net`].
642    //!
643    //! See crate [`::socket2`].
644
645    pub use ::socket2::*;
646}
647
648#[cfg(feature = "static_assertions")]
649pub mod static_assertions {
650    #![doc = include_str!("../doc-src/crate-static_assertions.md")]
651
652    pub use ::static_assertions::*;
653}
654
655#[cfg(feature = "syn")]
656pub mod syn {
657    //! A Rust parser used by procedural macros.
658    //!
659    //! See crate [`::syn`].
660
661    pub use ::syn::*;
662}
663
664#[cfg(feature = "tempfile")]
665pub mod tempfile {
666    #![doc = include_str!("../doc-src/crate-tempfile.md")]
667
668    pub use ::tempfile::*;
669}
670
671#[cfg(feature = "tera")]
672pub mod tera {
673    //! A text template engine based on Jinja2.
674    //!
675    //! See crate [`::tera`].
676
677    pub use ::tera::*;
678}
679
680#[cfg(feature = "termcolor")]
681pub mod termcolor {
682    //! Cross-platform library for writing colored output to the terminal.
683    //!
684    //! See crate [`::termcolor`].
685
686    pub use ::termcolor::*;
687}
688
689#[cfg(feature = "thiserror")]
690pub mod thiserror {
691    #![doc = include_str!("../doc-src/crate-thiserror.md")]
692
693    pub use ::thiserror::*;
694}
695
696#[cfg(feature = "tokio")]
697pub mod tokio {
698    #![doc = include_str!("../doc-src/crate-tokio.md")]
699
700    pub use ::tokio::*;
701}
702
703#[cfg(feature = "tower")]
704pub mod tower {
705    //! Service request/response abstraction (HTTP middleware)
706    //! for [`tokio`](super::tokio) and [`axum`](super::axum).
707    //!
708    //! See crate [`::tower`].
709
710    pub use ::tower::*;
711}
712
713#[cfg(feature = "toml")]
714pub mod toml {
715    #![doc = include_str!("../doc-src/crate-toml.md")]
716
717    pub use ::toml::*;
718}
719
720#[cfg(feature = "unicode-segmentation")]
721pub mod unicode_segmentation {
722    #![doc = include_str!("../doc-src/crate-unicode-segmentation.md")]
723
724    pub use ::unicode_segmentation::*;
725}
726
727#[cfg(feature = "url")]
728pub mod url {
729    #![doc = include_str!("../doc-src/crate-url.md")]
730
731    pub use ::url::*;
732}
733
734#[cfg(feature = "walkdir")]
735pub mod walkdir {
736    #![doc = include_str!("../doc-src/crate-walkdir.md")]
737
738    pub use ::walkdir::*;
739}
740
741#[cfg(feature = "xshell")]
742pub mod xshell {
743    //! A Swiss-army knife for writing shell-style scripts in Rust.
744    //!
745    //! See crate [`::xshell`].
746
747    pub use ::xshell::*;
748}