Crate fluent_comparisons[][src]

This crate is for you if you have ever been annoyed at writing repetitive conditions like this

if x < a && y < a && z < a {
// ... do something
}

and wished you could replace that code by something more expressive and less repetitive. Now you can rewrite the code as

use fluent_comparisons::all_of;

if all_of!({x,y,z} < a) {
// ... do something
}

Brief Description and Key Advantages

The crate provides the macros any_of, all_of and none_of to facilitate writing expressive multicomparisons. In addition to providing an intuitive syntax, the macros compile to the same assembly as the handwritten code (check it on godbolt.org).

A further benefit is lazy evaluation from left to right as seen in the next snippet:

use fluent_comparisons::any_of;

// if cheap_calc(arg1) <=5, then the expensive calculation
// is never performed
let b = any_of!({cheap_calc(arg1), expensive_calc(arg2)}<=5);
// whereas if we did this, the expensive calculation would be
// performed regardless of the result of cheap_calculation(arg1)
let b = [cheap_calc(arg1), expensive_calc(arg2)].iter().any(|val|val<=&5);

Usage

Refer to the items in the documentation below to learn about the usage of the macros.

Macros

all_of

Compare all values in a set to a common right hand side and decide whether the comparison returns true for all of the values in the set.

any_of

Compare all values in a set to a common right hand side and decide whether the comparison returns true for any of the values in the set.

none_of

Compare all values in a set to a common right hand side and decide whether the comparison returns true for none of the values in the set.