1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
//! Extension traits for `bool`. use type_identity::TypeIdentity; /// Extension trait for `bool`. #[cfg_attr(feature = "docsrs", doc(cfg(feature = "bools")))] pub trait BoolExt: TypeIdentity<Type = bool> + Sized { /// Returns `Some(some())` if `self` is `true`, otherwise returns `None`. /// /// This method is usable in all versions supported by this library, /// and is equivalent to the [`bool::then`] method, which was stabilized in Rust 1.50. /// /// [`bool::then`]: https://doc.rust-lang.org/std/primitive.bool.html#method.then /// /// # Example /// /// ``` /// use core_extensions::BoolExt; /// /// assert_eq!(true .if_true(|| 100 ), Some(100)); /// assert_eq!(false.if_true(|| 100 ), None); /// /// ``` /// #[inline] fn if_true<T, F>(self, some: F) -> Option<T> where F: FnOnce() -> T, { if self.into_type() { Some(some()) } else { None } } /// Returns `Some(some())` if `self` is `false`, otherwise returns `None`. /// /// # Example /// /// ``` /// use core_extensions::BoolExt; /// /// assert_eq!(false.if_false(|| 100 ), Some(100)); /// assert_eq!(true .if_false(|| 100 ), None); /// /// ``` /// #[inline] fn if_false<T, F>(self, some: F) -> Option<T> where F: FnOnce() -> T, { if self.into_type() { None } else { Some(some()) } } } impl BoolExt for bool {}