Macro swift_check::one_of

source ·
macro_rules! one_of {
    ($l_i:ident: $left:expr $(,)?) => { ... };
    ($l_i:ident: $left:expr, $r_i:ident: $right:expr $(,)?) => { ... };
    ($l_i:ident: $left:expr, $r_i:ident: $right:expr, $($rest:ident: $cond:expr),* $(,)?) => { ... };
}
Expand description

Ensure only one of the conditions are true

§Arguments

  • cond_name: condition, … - The conditions to check, only allowing one to hold

§Example

use swift_check::{one_of, for_all_ensure, eq, range};

let input = b"123456789";
let char_or_num = for_all_ensure(
    input, one_of!(f: range!(b'0'..=b'9'), s: range!(b'a'..=b'z'), t: range!(b'A'..=b'Z'))
);
assert!(char_or_num);

let should_fail = for_all_ensure(
    input,
    one_of!(first: range!(b'0'..=b'9'), second: range!(b'0'..=b'9'), third: range!(b'0'..=b'9'))
);
assert!(!should_fail)

Why do I need to specify identifiers for the conditions?

one_of! is not as simple as all! or any! which can utilize a single uniform bit op to get the result, a simple xor would cause odd numbers of successes to yield true. To avoid computing the conditions more than once the output is stored under the specified ident, and then we xor and nand this output, then and the results of these and then boom, we have ensured only one condition held.