::std::

Module option

Expand

Representing (Valid, Data)-pairs

The Option type represents values which are not always present. In hardware terms it is used to represent a (valid, data)-pair. The compiler ensures that the invalid case is dealt with when an option type is used, preventing accidental reading of invalid values.

Valid values are written as Some(value) while invalid values are written None.

The primary way to interact with Option types is via match statements. For example, To transform the underlying data by adding one, you can write:

match option {
    Some(value) => Some(value + 1),
    None => None
}

In many cases it is better to use the methods that the Option type provides to work with it, as it more clearly signals design intent. For examaple, the transformation above can be written using the map function.

option.map(fn |value| value + 1)

See the documentation for the Option<T> type below for more methods that it implements.

Implementations

impl<T> Option<T>
pub fn is_some(self) -> bool

Returns whether the option value is Some(_).

Examples

assert Some(8u8).is_some() == true;
pub fn is_none(self) -> bool

Returns whether the option value is None.

Examples

assert Some(8u8).is_none() == false;
pub fn unwrap_or(self, default: T) -> T

Returns the inner value if present, or the value provided otherwise.

Examples

assert Some(8u8).unwrap_or(67u8) == 8u8;
assert None.unwrap_or(67u8) == 67u8;
pub fn unwrap_or_undef(self) -> T

Returns the inner value if present, or an undefined value otherwise.

The semantics of the undefined value are described in detail in std::undef::undef.

Examples

assert Some(8u8).unwrap_or_undef() == 8u8;
pub fn map<O>(self, f: impl Fn(T) -> O) -> Option<O>

Transforms the contained value using the provided function.

Examples

assert Some(8u8).map(fn |x| x +. 1) == Some(9u8);
assert None.map(fn |x| x == 0u8) == None;
pub pipeline(Depth) pmap<#int Depth, O>(self, clk: clock, f: impl Pipeline<Depth>(T) -> O) -> Option<O>

Transforms the contained value using the provided pipeline.

The behavior of pmap replicates that of map, but spread over several registered stages.

See also Option<T>::map.

pub fn and_then<O>(self, f: impl Fn(T) -> Option<O>) -> Option<O>

Returns None if the option value is None, otherwise maps its content to a new option value.

Examples

let f = fn |x| if x > 10u8 { Some(x +. 8) } else { None };

assert Some(8u8).and_then(f) == None;
assert Some(12u8).and_then(f) == Some(20);
assert None.and_then(f) == None;
pub entity padded_sliding_window<#uint N>(self, clk: clock, rst: bool, default: T) -> Option<[T; N]>

Returns an array containing the N previous Some values starting with default before N values have been observed.

The output is valid in the same clock cycles as the input and the input is the 0th value of the array

input.padded_sliding_window::<2>(clk, rst, 0)
input:  | None | Some(1)      | Some(2)      | None | Some(3)
output: | None | Some([1, 0]) | Some([2, 1]) | None | Some([3, 2])
pub entity chunks<#uint N>(self, clk: clock, rst: bool) -> Option<[T; N]>

Gathers N values and outputs them as an array

Unlike sliding_window, each value seen in the input is only seen once in the output.

The 0th output element is the oldest

There is a one clock cycle latency between input and output

input.chunks::<2>(clk, rst, 0)
input:  | None | Some(1) | Some(2)       | None         | Some(3) | Some(4) | -
output: | None | None    | Some([1, 2])  | None         | None    | Some([3, 4])
impl<T> Default for Option<T>
fn default() -> Self
impl<T: PartialEq> PartialEq for Option<T>
fn eq(self, rhs: Self) -> bool
fn ne(self, rhs: Self) -> bool