bool-utils 1.0.0

Utility functions for working with boolean values
Documentation
//! Provides utility functions for working with boolean values.

#![deny(missing_docs)]

/// A trait that extends boolean values with additional utility methods.
pub trait BoolUtils {
    /// Toggles the boolean value.
    fn toggle(&mut self);

    /// Selects between two values based on the boolean value.
    fn select<T>(self, on_true: T, on_false: T) -> T;
}

impl BoolUtils for bool {
    fn toggle(&mut self) {
        *self = !*self;
    }

    fn select<T>(self, on_true: T, on_false: T) -> T {
        if self {
            on_true
        } else {
            on_false
        }
    }
}

#[test]
fn test_toggle() {
    let mut flag = true;
    flag.toggle();
    assert!(!flag);
    flag.toggle();
    assert!(flag);
}

#[test]
fn test_select() {
    let flag = true;
    let value = flag.select(10, 20);
    assert_eq!(value, 10);

    let flag = false;
    let value = flag.select(10, 20);
    assert_eq!(value, 20);
}