Constant-time selection: choose between two values based on a given [`Choice`]. This crate provides built-in implementations for the following types: - [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`isize`] - [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`] - [`NonZeroI8`], [`NonZeroI16`], [`NonZeroI32`], [`NonZeroI64`], [`NonZeroI128`], [`NonZeroI128`] - [`NonZeroU8`], [`NonZeroU16`], [`No
| 22 | /// - `[T; N]` where `T` impls [`CtSelectArray`], which the previously mentioned types all do, |
| 23 | /// as well as any type which impls [`Clone`] + [`CtAssignSlice`] + [`CtSelect`]. |
| 24 | pub trait CtSelect: Sized { |
| 25 | /// Select between `self` and `other` based on `choice`, returning a copy of the value. |
| 26 | /// |
| 27 | /// # Returns |
| 28 | /// - `self` if `choice` is [`Choice::FALSE`]. |
| 29 | /// - `other` if `choice` is [`Choice::TRUE`]. |
| 30 | #[must_use] |
| 31 | fn ct_select(&self, other: &Self, choice: Choice) -> Self; |
| 32 | |
| 33 | /// Conditionally swap `self` and `other` if `choice` is [`Choice::TRUE`]. |
| 34 | fn ct_swap(&mut self, other: &mut Self, choice: Choice) { |
| 35 | let tmp = self.ct_select(other, choice); |
| 36 | *other = Self::ct_select(other, self, choice); |
| 37 | *self = tmp; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// Implementing this trait enables use of the [`CtSelect`] trait to construct `[T; N]` where `T` |
| 42 | /// is the `Self` type implementing the trait, via a blanket impl. |
nothing calls this directly
no outgoing calls
no test coverage detected