Skip to main content

malachite_base/
lib.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9//! This crate contains many utilities that are used by the
10//! [`malachite-nz`](https://docs.rs/malachite-nz/latest/malachite_nz/) and
11//! [`malachite-q`]((https://docs.rs/malachite-q/latest/malachite_q/)) crates. These utilities
12//! include
13//! - Traits that wrap functions from the standard library, like
14//!   [`CheckedAdd`](num::arithmetic::traits::CheckedAdd).
15//! - Traits that give extra functionality to primitive types, like
16//!   [`Gcd`](num::arithmetic::traits::Gcd), [`FloorSqrt`](num::arithmetic::traits::FloorSqrt), and
17//!   [`BitAccess`](num::logic::traits::BitAccess).
18//! - Iterator-producing functions that let you generate values for testing. Here's an example of
19//!   an iterator that produces all pairs of [`u32`]s:
20//!   ```
21//!   use malachite_base::num::exhaustive::exhaustive_unsigneds;
22//!   use malachite_base::tuples::exhaustive::exhaustive_pairs_from_single;
23//!
24//!   let mut pairs = exhaustive_pairs_from_single(exhaustive_unsigneds::<u32>());
25//!   assert_eq!(
26//!       pairs.take(20).collect::<Vec<_>>(),
27//!       &[
28//!           (0, 0), (0, 1), (1, 0), (1, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 0), (2, 1),
29//!           (3, 0), (3, 1), (2, 2), (2, 3), (3, 2), (3, 3), (0, 4), (0, 5), (1, 4), (1, 5)
30//!       ]
31//!   );
32//!   ```
33//! - The [`RoundingMode`](rounding_modes::RoundingMode) enum, which allows you to specify the
34//!   rounding behavior of various functions.
35//! - The [`NiceFloat`](num::float::NiceFloat) wrapper, which provides alternative implementations
36//!   of [`Eq`], [`Ord`], and [`Display`](std::fmt::Display) for floating-point values which are in
37//!   some ways nicer than the defaults.
38//!
39//! # Demos and benchmarks
40//! This crate comes with a `bin` target that can be used for running demos and benchmarks.
41//! - Almost all of the public functions in this crate have an associated demo. Running a demo
42//!   shows you a function's behavior on a large number of inputs. For example, to demo the
43//!   [`mod_pow`](num::arithmetic::traits::ModPow::mod_pow) function on [`u32`]s, you can use the
44//!   following command:
45//!   ```text
46//!   cargo run --features bin_build --release -- -l 10000 -m exhaustive -d demo_mod_pow_u32
47//!   ```
48//!   This command uses the `exhaustive` mode, which generates every possible input, generally
49//!   starting with the simplest input and progressing to more complex ones. Another mode is
50//!   `random`. The `-l` flag specifies how many inputs should be generated.
51//! - You can use a similar command to run benchmarks. The following command benchmarks various
52//!   GCD algorithms for [`u64`]s:
53//!   ```text
54//!   cargo run --features bin_build --release -- -l 1000000 -m random -b \
55//!       benchmark_gcd_algorithms_u64 -o gcd-bench.gp
56//!   ```
57//!   This creates a file called gcd-bench.gp. You can use gnuplot to create an SVG from it like
58//!   so:
59//!   ```text
60//!   gnuplot -e "set terminal svg; l \"gcd-bench.gp\"" > gcd-bench.svg
61//!   ```
62//!
63//! The list of available demos and benchmarks is not documented anywhere; you must find them by
64//! browsing through
65//! [`bin_util/demo_and_bench`](https://github.com/mhogrefe/malachite/tree/master/malachite-base/src/bin_util/demo_and_bench).
66//!
67//! # Features
68//! - `test_build`: A large proportion of the code in this crate is only used for testing. For a
69//!   typical user, building this code would result in an unnecessarily long compilation time and
70//!   an unnecessarily large binary. Much of it is also used for testing
71//!   [`malachite-nz`](https://docs.rs/malachite-nz/latest/malachite_nz/) and
72//!   [`malachite-q`](https://docs.rs/malachite-q/latest/malachite_q/), so it can't just be
73//!   confined to the `tests` directory. My solution is to only build this code when the
74//!   `test_build` feature is enabled. If you want to run unit tests, you must enable `test_build`.
75//!   However, doctests don't require it, since they only test the public interface.
76//! - `bin_build`: This feature is used to build the code for demos and benchmarks, which also
77//!   takes a long time to build. Enabling this feature also enables `test_build`.
78
79#![forbid(unsafe_code)]
80#![allow(
81    unstable_name_collisions,
82    clippy::assertions_on_constants,
83    clippy::cognitive_complexity,
84    clippy::many_single_char_names,
85    clippy::range_plus_one,
86    clippy::suspicious_arithmetic_impl,
87    clippy::suspicious_op_assign_impl,
88    clippy::too_many_arguments,
89    clippy::type_complexity,
90    clippy::upper_case_acronyms,
91    clippy::multiple_bound_locations
92)]
93#![warn(
94    clippy::cast_lossless,
95    clippy::explicit_into_iter_loop,
96    clippy::explicit_iter_loop,
97    clippy::filter_map_next,
98    clippy::large_digit_groups,
99    clippy::manual_filter_map,
100    clippy::manual_find_map,
101    clippy::map_flatten,
102    clippy::map_unwrap_or,
103    clippy::match_same_arms,
104    clippy::missing_const_for_fn,
105    clippy::mut_mut,
106    clippy::needless_borrow,
107    clippy::needless_continue,
108    clippy::needless_pass_by_value,
109    clippy::print_stdout,
110    clippy::redundant_closure_for_method_calls,
111    clippy::single_match_else,
112    clippy::trait_duplication_in_bounds,
113    clippy::type_repetition_in_bounds,
114    clippy::uninlined_format_args,
115    clippy::unused_self,
116    clippy::if_not_else,
117    clippy::manual_assert,
118    clippy::range_plus_one,
119    clippy::redundant_else,
120    clippy::semicolon_if_nothing_returned,
121    clippy::cloned_instead_of_copied,
122    clippy::flat_map_option,
123    clippy::unnecessary_wraps,
124    clippy::unnested_or_patterns,
125    clippy::use_self,
126    clippy::trivially_copy_pass_by_ref
127)]
128#![cfg_attr(
129    not(any(feature = "test_build", feature = "random", feature = "std")),
130    no_std
131)]
132
133#[macro_use]
134extern crate alloc;
135
136#[cfg(feature = "test_build")]
137#[doc(hidden)]
138#[inline]
139pub fn fail_on_untested_path(message: &str) {
140    panic!("Untested path. {message}");
141}
142
143#[cfg(not(feature = "test_build"))]
144#[doc(hidden)]
145#[inline]
146pub const fn fail_on_untested_path(_message: &str) {}
147
148// TODO links for malachite-nz and malachite-q
149
150/// The [`Named`](named::Named) trait, for getting a type's name.
151#[macro_use]
152pub mod named;
153
154#[doc(hidden)]
155#[macro_use]
156pub mod macros;
157
158/// Functions for working with [`bool`]s.
159#[macro_use]
160pub mod bools;
161/// Functions for working with [`char`]s.
162#[macro_use]
163pub mod chars;
164/// Macros and traits related to comparing values.
165pub mod comparison;
166/// Functions and adaptors for iterators.
167pub mod iterators;
168/// [`Never`](nevers::Never), a type that cannot be instantiated.
169pub mod nevers;
170/// Functions for working with primitive integers and floats.
171#[macro_use]
172pub mod num;
173/// Functions for working with [`Ordering`](std::cmp::Ordering)s.
174pub mod options;
175/// Functions for working with [`Option`]s.
176pub mod orderings;
177#[cfg(feature = "random")]
178/// Functions for generating random values.
179pub mod random;
180/// [`RationalSequence`](rational_sequences::RationalSequence), a type representing a sequence that
181/// is finite or eventually repeating, just like the digits of a rational number.
182pub mod rational_sequences;
183/// [`RoundingMode`](rounding_modes::RoundingMode), an enum used to specify rounding behavior.
184pub mod rounding_modes;
185/// Functions for working with [`HashSet`](std::collections::HashSet)s and
186/// [`BTreeSet`](std::collections::BTreeSet)s.
187pub mod sets;
188/// Functions for working with slices.
189#[macro_use]
190pub mod slices;
191/// Functions for working with [`String`]s.
192pub mod strings;
193/// Functions for working with tuples.
194pub mod tuples;
195/// Unions (sum types). These are essentially generic enums.
196///
197/// # unwrap
198/// ```
199/// use malachite_base::union_struct;
200/// use malachite_base::unions::UnionFromStrError;
201/// use std::fmt::{self, Display, Formatter};
202/// use std::str::FromStr;
203///
204/// union_struct!(
205///     (pub(crate)),
206///     Union3,
207///     Union3<T, T, T>,
208///     [A, A, 'A', a],
209///     [B, B, 'B', b],
210///     [C, C, 'C', c]
211/// );
212///
213/// let mut u: Union3<char, char, char>;
214///
215/// u = Union3::A('a');
216/// assert_eq!(u.unwrap(), 'a');
217///
218/// u = Union3::B('b');
219/// assert_eq!(u.unwrap(), 'b');
220///
221/// u = Union3::C('c');
222/// assert_eq!(u.unwrap(), 'c');
223/// ```
224///
225/// # fmt
226/// ```
227/// use malachite_base::union_struct;
228/// use malachite_base::unions::UnionFromStrError;
229/// use std::fmt::{self, Display, Formatter};
230/// use std::str::FromStr;
231///
232/// union_struct!(
233///     (pub(crate)),
234///     Union3,
235///     Union3<T, T, T>,
236///     [A, A, 'A', a],
237///     [B, B, 'B', b],
238///     [C, C, 'C', c]
239/// );
240///
241/// let mut u: Union3<char, u32, bool>;
242///
243/// u = Union3::A('a');
244/// assert_eq!(u.to_string(), "A(a)");
245///
246/// u = Union3::B(5);
247/// assert_eq!(u.to_string(), "B(5)");
248///
249/// u = Union3::C(false);
250/// assert_eq!(u.to_string(), "C(false)");
251/// ```
252///
253/// # from_str
254/// ```
255/// use malachite_base::union_struct;
256/// use malachite_base::unions::UnionFromStrError;
257/// use std::fmt::{self, Display, Formatter};
258/// use std::str::FromStr;
259///
260/// union_struct!(
261///     (pub(crate)),
262///     Union3,
263///     Union3<T, T, T>,
264///     [A, A, 'A', a],
265///     [B, B, 'B', b],
266///     [C, C, 'C', c]
267/// );
268///
269/// let u3: Union3<bool, u32, char> = Union3::from_str("B(5)").unwrap();
270/// assert_eq!(u3, Union3::B(5));
271///
272/// let result: Result<Union3<char, u32, bool>, _> = Union3::from_str("xyz");
273/// assert_eq!(result, Err(UnionFromStrError::Generic("xyz".to_string())));
274///
275/// let result: Result<Union3<char, u32, bool>, _> = Union3::from_str("A(ab)");
276/// if let Err(UnionFromStrError::Specific(Union3::A(_e))) = result {
277/// } else {
278///     panic!("wrong error variant")
279/// }
280/// ```
281pub mod unions;
282/// Functions for working with [`Vec`]s.
283pub mod vecs;
284
285#[cfg(feature = "test_build")]
286pub mod test_util;
287
288pub mod platform;