| 1 | //! Traits for abstracting over Boolean types. |
| 2 | //! |
| 3 | //! These traits are mainly useful for allowing SIMD values, where bit masks are |
| 4 | //! typically used instead of `bool`. |
| 5 | |
| 6 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#, BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#, BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#, Notrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/Not#}; |
| 7 | |
| 8 | #[cfg(feature = "wide")] |
| 9 | mod widerust-analyzer cargo palette 0.7.6 bool_mask/wide/; |
| 10 | |
| 11 | /// Associates a Boolean type to the implementing type. |
| 12 | /// |
| 13 | /// This is primarily used in traits and functions that can accept SIMD values |
| 14 | /// and return a Boolean result. SIMD values use masks to select different values for |
| 15 | /// each lane and `HasBoolMask::Mask` can be used to know which type that mask |
| 16 | /// has. |
| 17 | pub trait HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# { |
| 18 | /// The mask type to use for selecting `Self` values. |
| 19 | type Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#: BoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/BoolMask#; |
| 20 | } |
| 21 | |
| 22 | impl<T> HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# for &'_ T |
| 23 | where |
| 24 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#, |
| 25 | { |
| 26 | type Maskrust-analyzer cargo palette 0.7.6 bool_mask/impl#[`&T`][HasBoolMask]Mask# = T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#; |
| 27 | } |
| 28 | |
| 29 | impl<T> HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# for &'_ mut T |
| 30 | where |
| 31 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#, |
| 32 | { |
| 33 | type Maskrust-analyzer cargo palette 0.7.6 bool_mask/impl#[`&mut T`][HasBoolMask]Mask# = T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#; |
| 34 | } |
| 35 | |
| 36 | impl<T> HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# for [T] |
| 37 | where |
| 38 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#, |
| 39 | { |
| 40 | type Maskrust-analyzer cargo palette 0.7.6 bool_mask/impl#[`[T]`][HasBoolMask]Mask# = T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#; |
| 41 | } |
| 42 | |
| 43 | impl<T, const N: usize> HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# for [T; N] |
| 44 | where |
| 45 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#, |
| 46 | { |
| 47 | type Maskrust-analyzer cargo palette 0.7.6 bool_mask/impl#[`[T; N]`][HasBoolMask]Mask# = T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#; |
| 48 | } |
| 49 | |
| 50 | macro_rules! impl_has_bool_maskrust-analyzer cargo palette 0.7.6 bool_mask/impl_has_bool_mask! { |
| 51 | ($($ty:ident),+) => { |
| 52 | $( |
| 53 | impl HasBoolMask for $ty { |
| 54 | type Mask = bool; |
| 55 | } |
| 56 | )+ |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | impl_has_bool_maskrust-analyzer cargo palette 0.7.6 bool_mask/impl_has_bool_mask!!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, f32, f64); |
| 61 | |
| 62 | /// Basic methods for boolean masks. |
| 63 | pub trait BoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/BoolMask# { |
| 64 | /// Create a new mask where each lane is set to `value`. |
| 65 | #[must_use] |
| 66 | fn from_boolrust-analyzer cargo palette 0.7.6 bool_mask/BoolMask#from_bool().(value: bool) -> Self; |
| 67 | |
| 68 | /// Checks if all lanes in the mask are `true`. |
| 69 | #[must_use] |
| 70 | fn is_truerust-analyzer cargo palette 0.7.6 bool_mask/BoolMask#is_true().(&self) -> bool; |
| 71 | |
| 72 | /// Checks if all lanes in the mask are `false`. |
| 73 | #[must_use] |
| 74 | fn is_falserust-analyzer cargo palette 0.7.6 bool_mask/BoolMask#is_false().(&self) -> bool; |
| 75 | } |
| 76 | |
| 77 | impl BoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/BoolMask# for bool { |
| 78 | #[inline] |
| 79 | fn from_boolrust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][BoolMask]from_bool().(value: bool) -> Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][BoolMask] { |
| 80 | value |
| 81 | } |
| 82 | |
| 83 | #[inline] |
| 84 | fn is_truerust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][BoolMask]is_true().(&self) -> bool { |
| 85 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self |
| 86 | } |
| 87 | |
| 88 | #[inline] |
| 89 | fn is_falserust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][BoolMask]is_false().(&self) -> bool { |
| 90 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// Makes a mask bale to select between two values. |
| 95 | pub trait Selectrust-analyzer cargo palette 0.7.6 bool_mask/Select#<T> |
| 96 | where |
| 97 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#<Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# = Self>, |
| 98 | { |
| 99 | /// Select lanes from `a` when corresponding lanes in `self` are `true`, and |
| 100 | /// select from `b` when `false`. |
| 101 | #[must_use] |
| 102 | fn selectrust-analyzer cargo palette 0.7.6 bool_mask/Select#select().(self, a: T, b: T) -> T; |
| 103 | } |
| 104 | |
| 105 | impl<T> Selectrust-analyzer cargo palette 0.7.6 bool_mask/Select#<T> for bool |
| 106 | where |
| 107 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#<Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][`Select<T>`]>, |
| 108 | { |
| 109 | #[inline(always)] |
| 110 | fn selectrust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][`Select<T>`]select().(self, a: T, b: T) -> T { |
| 111 | if self { |
| 112 | a |
| 113 | } else { |
| 114 | b |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Like [`Select`], but can avoid evaluating the input. |
| 120 | pub trait LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T>: Selectrust-analyzer cargo palette 0.7.6 bool_mask/Select#<T> |
| 121 | where |
| 122 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#<Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# = Self>, |
| 123 | { |
| 124 | /// Select lanes from the output of `a` when corresponding lanes in `self` |
| 125 | /// are `true`, and select from the output of `b` when `false`. May avoid |
| 126 | /// evaluating either option if it's not selected. |
| 127 | #[must_use] |
| 128 | fn lazy_selectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#lazy_select().<A, B>(self, a: A, b: B) -> T |
| 129 | where |
| 130 | A: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T, |
| 131 | B: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T; |
| 132 | } |
| 133 | |
| 134 | impl<T> LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T> for bool |
| 135 | where |
| 136 | T: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#<Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][`LazySelect<T>`]>, |
| 137 | { |
| 138 | #[inline(always)] |
| 139 | fn lazy_selectrust-analyzer cargo palette 0.7.6 bool_mask/impl#[bool][`LazySelect<T>`]lazy_select().<A, B>(self, a: A, b: B) -> T |
| 140 | where |
| 141 | A: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T, |
| 142 | B: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T, |
| 143 | { |
| 144 | if self { |
| 145 | a() |
| 146 | } else { |
| 147 | b() |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// A helper trait that collects bit traits under one name. |
| 153 | pub trait BitOpsrust-analyzer cargo palette 0.7.6 bool_mask/BitOps#: |
| 154 | Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# |
| 155 | + BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#Output# = Self> |
| 156 | + BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#Output# = Self> |
| 157 | + BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#Output# = Self> |
| 158 | + Notrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/Not#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/Not#Output# = Self> |
| 159 | + for<'a> BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#Output# = Self> |
| 160 | + for<'a> BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#Output# = Self> |
| 161 | + for<'a> BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#<&'a Self, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#Output# = Self> |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | impl<T> BitOpsrust-analyzer cargo palette 0.7.6 bool_mask/BitOps# for T where |
| 166 | T: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# |
| 167 | + BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 168 | + BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 169 | + BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 170 | + Notrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/Not#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/Not#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 171 | + for<'a> BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#<&'a Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 172 | + for<'a> BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#<&'a Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 173 | + for<'a> BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#<&'a Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps], Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#Output# = Selfrust-analyzer cargo palette 0.7.6 bool_mask/impl#[T][BitOps]> |
| 174 | { |
| 175 | } |