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

pub struct CtOption<T> {
value: T,
is_some: Choice,
}

The CtOption<T> type represents an optional value similar to the Option<T> type but is intended for use in constant time APIs.

Any given CtOption<T> is either Some or None, but unlike Option<T> these variants are not exposed. The is_some() method is used to determine if the value is Some, and unwrap_or() and unwrap_or_else() methods are provided to access the underlying value. The value can also be obtained with unwrap() but this will panic if it is None.

Functions that are intended to be constant time may not produce valid results for all inputs, such as square root and inversion operations in finite field arithmetic. Returning an Option<T> from these functions makes it difficult for the caller to reason about the result in constant time, and returning an incorrect value burdens the caller and increases the chance of bugs.

## Fields

value: Tis_some: Choice

## Methods

### impl<T> CtOption<T>[src]

#### pub fn new(value: T, is_some: Choice) -> CtOption<T>[src]

This method is used to construct a new CtOption<T> and takes a value of type T, and a Choice that determines whether the optional value should be Some or not. If is_some is false, the value will still be stored but its value is never exposed.

#### pub fn unwrap(self) -> T[src]

This returns the underlying value but panics if it is not Some.

#### pub fn unwrap_or(self, def: T) -> T where    T: ConditionallySelectable, [src]

This returns the underlying value if it is Some or the provided value otherwise.

#### pub fn unwrap_or_else<F>(self, f: F) -> T where    T: ConditionallySelectable,    F: FnOnce() -> T, [src]

This returns the underlying value if it is Some or the value produced by the provided closure otherwise.

#### pub fn is_some(&self) -> Choice[src]

Returns a true Choice if this value is Some.

#### pub fn is_none(&self) -> Choice[src]

Returns a true Choice if this value is None.

#### pub fn map<U, F>(self, f: F) -> CtOption<U> where    T: Default + ConditionallySelectable,    F: FnOnce(T) -> U, [src]

Returns a None value if the option is None, otherwise returns a CtOption enclosing the value of the provided closure. The closure is given the enclosed value or, if the option is None, it is provided a dummy value computed using Default::default().

This operates in constant time, because the provided closure is always called.

#### pub fn and_then<U, F>(self, f: F) -> CtOption<U> where    T: Default + ConditionallySelectable,    F: FnOnce(T) -> CtOption<U>, [src]

Returns a None value if the option is None, otherwise returns the result of the provided closure. The closure is given the enclosed value or, if the option is None, it is provided a dummy value computed using Default::default().

This operates in constant time, because the provided closure is always called.

## Trait Implementations

### impl<T: ConstantTimeEq> ConstantTimeEq for CtOption<T>[src]

#### fn ct_eq(&self, rhs: &CtOption<T>) -> Choice[src]

Two CtOption<T>s are equal if they are both Some and their values are equal, or both None.

### impl<T: ConditionallySelectable> ConditionallySelectable for CtOption<T>[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<T: Clone> Clone for CtOption<T>[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 for T where    U: Into<T>, [src]

#### type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.