Trait subtle::ConditionallySelectable
[−]
[src]
pub trait ConditionallySelectable { fn conditional_select(a: Self, b: Self, choice: Mask) -> Self; }
Select a
if choice == 1
or select b
if choice == 0
, in constant time.
Inputs
a
,b
, andchoice
must be types for which bitwise-AND, and bitwise-OR, bitwise-complement, subtraction, multiplicative identity, copying, partial equality, and partial order comparison are defined.choice
: Ifchoice
is equal to1
thena
is returned. Ifchoice
is equal to0
thenb
is returned.
Warning
The behaviour of this function is undefined if choice
is something other
than a multiplicative identity or additive identity (i.e. 1u8
or 0u8
).
If you somehow manage to design a type which is not a signed integer, and yet implements all the requisite trait bounds for this generic, it's your problem if something breaks.
Examples
This function is implemented via a macro for signed integer types:
let a: i32 = 5; let b: i32 = 13; assert!(i32::conditional_select(a, b, 0) == 13); assert!(i32::conditional_select(a, b, 1) == 5); let c: i64 = 2343249123; let d: i64 = 8723884895; assert!(i64::conditional_select(c, d, 0) == d); assert!(i64::conditional_select(c, d, 1) == c);
Required Methods
fn conditional_select(a: Self, b: Self, choice: Mask) -> Self
Select a
if choice == 1
or select b
if choice == 0
, in constant time.
Inputs
a
,b
, andchoice
must be types for which bitwise-AND, and bitwise-OR, bitwise-complement, subtraction, multiplicative identity, copying, partial equality, and partial order comparison are defined.choice
: Ifchoice
is equal to1
thena
is returned. Ifchoice
is equal to0
thenb
is returned.