fluent_comparisons/
lib.rs

1#![cfg_attr(not(test), no_std)]
2//! This crate is for you if you have ever been annoyed at writing repetitive conditions like this
3//! ```rust
4//! # fn test(a:i32,x:i32,y:i32,z:i32) {
5//! if x < a && y < a && z < a {
6//! // ... do something
7//! }
8//! # }
9//! ```
10//! and wished you could replace that code by something more expressive and less repetitive. Now you can rewrite the code as
11//! ```rust
12//!# fn test(a:i32,x:i32,y:i32,z:i32) {
13//! use fluent_comparisons::all_of;
14//!
15//! if all_of!({x,y,z} < a) {
16//! // ... do something
17//! }
18//! # }
19//! ```
20//!
21//! # Examples
22//! The crate provides the macros `any_of`, `all_of` and `none_of` to facilitate writing expressive multicomparisons. The arguments
23//! don't need to be numeric, but can be expressions of any type. Furthermore, a syntax for applying transformations to the set
24//! on the left hand side is provided.
25//!
26//! ```
27//! # use fluent_comparisons::{all_of,none_of,any_of};
28//! // the following assertions hold
29//! assert!(none_of!({1,2,3}>4));
30//! assert!(any_of!({1,2,3}.map(|x|x%2)==0));
31//! ```
32//!
33//! # Brief Description and Key Advantages
34//!
35//! In addition to providing an intuitive syntax, the macros compile to the same assembly as
36//! the handwritten code ([check it on godbolt.org](https://godbolt.org/z/M3494a6Mc)).
37//!
38//! A further benefit is [lazy evaluation](https://doc.rust-lang.org/reference/expressions/operator-expr.html#lazy-boolean-operators) from
39//! left to right as seen in the next snippet:
40//!
41//! ```rust
42//! use fluent_comparisons::any_of;
43//!
44//! # fn cheap_calc(v : usize)-> usize {v}
45//! # fn expensive_calc(v : usize)-> usize {v}
46//! # fn test(arg1: usize, arg2:usize) {
47//! // if cheap_calc(arg1) <=5, then the expensive calculation
48//! // is never performed
49//! let b = any_of!({cheap_calc(arg1), expensive_calc(arg2)}<=5);
50//! // whereas if we did this, the expensive calculation would be
51//! // performed regardless of the result of cheap_calc(arg1)
52//! let b = [cheap_calc(arg1), expensive_calc(arg2)].iter().any(|val|val<=&5);
53//! # }
54//! ```
55//!
56//! And finally, you can rest assured in the warm and fuzzy feeling that this crate is excessively tested.
57//!
58//! ## Usage
59//!
60//! Refer to the items in the documentation below to learn more about the usage of the macros.
61
62pub use fluent_comparisons_macros::any_of;
63
64pub use fluent_comparisons_macros::all_of;
65
66pub use fluent_comparisons_macros::none_of;
67
68#[cfg(test)]
69#[allow(clippy::bool_assert_comparison)]
70mod tests;