# [−][src]Struct subtle::Choice

pub struct Choice(u8);

The Choice struct represents a choice for use in conditional assignment.

It is a wrapper around a u8, which should have the value either 1 (true) or 0 (false).

With the nightly feature enabled, the conversion from u8 to Choice passes the value through an optimization barrier, as a best-effort attempt to prevent the compiler from inferring that the Choice value is a boolean. This strategy is based on Tim Maclean's work on rust-timing-shield, which attempts to provide a more comprehensive approach for preventing software side-channels in Rust code.

The Choice struct implements operators for AND, OR, XOR, and NOT, to allow combining Choice values. These operations do not short-circuit.

## Methods

### impl Choice[src]

#### pub fn unwrap_u8(&self) -> u8[src]

Unwrap the Choice wrapper to reveal the underlying u8.

# Note

This function only exists as an escape hatch for the rare case where it's not possible to use one of the subtle-provided trait impls.

To convert a Choice to a bool, use the From implementation instead.

## Trait Implementations

### impl ConditionallySelectable for Choice[src]

#### fn conditional_assign(&mut self, other: &Self, choice: Choice)[src]

Conditionally assign other to self, according to choice. Read more

#### fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)[src]

Conditionally swap self and other if choice == 1; otherwise, reassign both unto themselves. Read more

### impl Not for Choice[src]

#### type Output = Choice

The resulting type after applying the ! operator.

### impl BitAnd<Choice> for Choice[src]

#### type Output = Choice

The resulting type after applying the & operator.

### impl BitOr<Choice> for Choice[src]

#### type Output = Choice

The resulting type after applying the | operator.

### impl BitXor<Choice> for Choice[src]

#### type Output = Choice

The resulting type after applying the ^ operator.

### impl From<Choice> for bool[src]

#### fn from(source: Choice) -> bool[src]

Convert the Choice wrapper into a bool, depending on whether the underlying u8 was a 0 or a 1.

# Note

This function exists to avoid having higher-level cryptographic protocol implementations duplicating this pattern.

The intended use case for this conversion is at the end of a higher-level primitive implementation: for example, in checking a keyed MAC, where the verification should happen in constant-time (and thus use a Choice) but it is safe to return a bool at the end of the verification.

### impl Clone for Choice[src]

#### fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

## Blanket Implementations

### impl<T, U> TryFrom<U> for T where    U: Into<T>, [src]

#### type Error = Infallible

The type returned in the event of a conversion error.

### impl<T, U> TryInto<U> for T where    U: TryFrom<T>, [src]

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

### impl<T> ToOwned for T where    T: Clone, [src]

#### type Owned = T

The resulting type after obtaining ownership.