Enum hazardflow_designs::std::value::HOption
source · pub enum HOption<T: Copy> {
None,
Some(T),
}Expand description
The Option type.
Variants§
Implementations§
source§impl<T: Copy> HOption<T>
impl<T: Copy> HOption<T>
sourcepub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool
pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool
Returns true if the option is a Some and the value inside of it matches a predicate.
Examples
let x: Option<u32> = Some(2);
assert_eq!(x.is_some_and(|x| x > 1), true);
let x: Option<u32> = Some(0);
assert_eq!(x.is_some_and(|x| x > 1), false);
let x: Option<u32> = None;
assert_eq!(x.is_some_and(|x| x > 1), false);sourcepub fn expect(self, msg: &str) -> T
pub fn expect(self, msg: &str) -> T
Returns the contained Some value, consuming the self value.
Panics
Panics if the value is a None with a custom panic message provided by
msg.
Examples
let x = Some("value");
assert_eq!(x.expect("fruits are healthy"), "value");let x: Option<&str> = None;
x.expect("fruits are healthy"); // panics with `fruits are healthy`Recommended Message Style
We recommend that expect messages are used to describe the reason you
expect the Option should be Some.
let item = slice.get(0)
.expect("slice should not be empty");Hint: If you’re having trouble remembering how to phrase expect error messages remember to focus on the word “should” as in “env variable should be set by blah” or “the given binary should be available and executable by the current user”.
For more detail on expect message styles and the reasoning behind our
recommendation please refer to the section on “Common Message
Styles” in the std::error module docs.
sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Returns the contained Some value, consuming the self value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the None
case explicitly, or call unwrap_or, unwrap_or_else, or
unwrap_or_default.
Panics
Panics if the self value equals None.
Examples
let x = Some("air");
assert_eq!(x.unwrap(), "air");let x: Option<&str> = None;
assert_eq!(x.unwrap(), "air"); // failssourcepub fn unwrap_or(self, default: T) -> T
pub fn unwrap_or(self, default: T) -> T
Returns the contained Some value or a provided default.
Arguments passed to unwrap_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use unwrap_or_else,
which is lazily evaluated.
Examples
assert_eq!(Some("car").unwrap_or("bike"), "car");
assert_eq!(None.unwrap_or("bike"), "bike");sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere
T: Default,
Returns the contained Some value or a default.
Consumes the self argument then, if Some, returns the contained
value, otherwise if None, returns the default value for that
type.
Examples
let x: Option<u32> = None;
let y: Option<u32> = Some(12);
assert_eq!(x.unwrap_or_default(), 0);
assert_eq!(y.unwrap_or_default(), 12);sourcepub fn map<U: Copy, F>(self, f: F) -> HOption<U>where
F: FnOnce(T) -> U,
pub fn map<U: Copy, F>(self, f: F) -> HOption<U>where
F: FnOnce(T) -> U,
Maps an Option<T> to Option<U> by applying a function to a contained value (if Some) or returns None (if None).
Examples
Calculates the length of an Option<String> as an
Option<usize>, consuming the original:
let maybe_some_string = Some(String::from("Hello, World!"));
// `Option::map` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map(|s| s.len());
assert_eq!(maybe_some_len, Some(13));
let x: Option<&str> = None;
assert_eq!(x.map(|s| s.len()), None);sourcepub fn map_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(T) -> U,
pub fn map_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(T) -> U,
Returns the provided default result (if none), or applies a function to the contained value (if any).
Arguments passed to map_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use map_or_else,
which is lazily evaluated.
Examples
let x = Some("foo");
assert_eq!(x.map_or(42, |v| v.len()), 3);
let x: Option<&str> = None;
assert_eq!(x.map_or(42, |v| v.len()), 42);sourcepub fn and<U: Copy>(self, optb: HOption<U>) -> HOption<U>
pub fn and<U: Copy>(self, optb: HOption<U>) -> HOption<U>
Returns None if the option is None, otherwise returns optb.
Arguments passed to and are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use and_then, which is
lazily evaluated.
Examples
let x = Some(2);
let y: Option<&str> = None;
assert_eq!(x.and(y), None);
let x: Option<u32> = None;
let y = Some("foo");
assert_eq!(x.and(y), None);
let x = Some(2);
let y = Some("foo");
assert_eq!(x.and(y), Some("foo"));
let x: Option<u32> = None;
let y: Option<&str> = None;
assert_eq!(x.and(y), None);sourcepub fn and_then<U: Copy, F>(self, f: F) -> HOption<U>
pub fn and_then<U: Copy, F>(self, f: F) -> HOption<U>
Returns None if the option is None, otherwise calls f with the
wrapped value and returns the result.
Some languages call this operation flatmap.
Examples
fn sq_then_to_string(x: u32) -> Option<String> {
x.checked_mul(x).map(|sq| sq.to_string())
}
assert_eq!(Some(2).and_then(sq_then_to_string), Some(4.to_string()));
assert_eq!(Some(1_000_000).and_then(sq_then_to_string), None); // overflowed!
assert_eq!(None.and_then(sq_then_to_string), None);Often used to chain fallible operations that may return None.
let arr_2d = [["A0", "A1"], ["B0", "B1"]];
let item_0_1 = arr_2d.get(0).and_then(|row| row.get(1));
assert_eq!(item_0_1, Some(&"A1"));
let item_2_0 = arr_2d.get(2).and_then(|row| row.get(0));
assert_eq!(item_2_0, None);sourcepub fn filter<P>(self, predicate: P) -> Self
pub fn filter<P>(self, predicate: P) -> Self
Returns None if the option is None, otherwise calls predicate
with the wrapped value and returns:
Some(t)ifpredicatereturnstrue(wheretis the wrapped value), andNoneifpredicatereturnsfalse.
This function works similar to Iterator::filter(). You can imagine
the Option<T> being an iterator over one or zero elements. filter()
lets you decide which elements to keep.
Examples
fn is_even(n: &i32) -> bool {
n % 2 == 0
}
assert_eq!(None.filter(is_even), None);
assert_eq!(Some(3).filter(is_even), None);
assert_eq!(Some(4).filter(is_even), Some(4));sourcepub fn or(self, optb: HOption<T>) -> HOption<T>
pub fn or(self, optb: HOption<T>) -> HOption<T>
Returns the option if it contains a value, otherwise returns optb.
Arguments passed to or are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use or_else, which is
lazily evaluated.
Examples
let x = Some(2);
let y = None;
assert_eq!(x.or(y), Some(2));
let x = None;
let y = Some(100);
assert_eq!(x.or(y), Some(100));
let x = Some(2);
let y = Some(100);
assert_eq!(x.or(y), Some(2));
let x: Option<u32> = None;
let y = None;
assert_eq!(x.or(y), None);sourcepub fn xor(self, optb: HOption<T>) -> HOption<T>
pub fn xor(self, optb: HOption<T>) -> HOption<T>
Returns Some if exactly one of self, optb is Some, otherwise returns None.
Examples
let x = Some(2);
let y: Option<u32> = None;
assert_eq!(x.xor(y), Some(2));
let x: Option<u32> = None;
let y = Some(2);
assert_eq!(x.xor(y), Some(2));
let x = Some(2);
let y = Some(2);
assert_eq!(x.xor(y), None);
let x: Option<u32> = None;
let y: Option<u32> = None;
assert_eq!(x.xor(y), None);sourcepub fn zip<U: Copy>(self, other: HOption<U>) -> HOption<(T, U)>
pub fn zip<U: Copy>(self, other: HOption<U>) -> HOption<(T, U)>
Zips self with another Option.
If self is Some(s) and other is Some(o), this method returns Some((s, o)).
Otherwise, None is returned.
Examples
let x = Some(1);
let y = Some("hi");
let z = None::<u8>;
assert_eq!(x.zip(y), Some((1, "hi")));
assert_eq!(x.zip(z), None);sourcepub fn zip_with<U: Copy, F, R: Copy>(
self,
other: HOption<U>,
f: F
) -> HOption<R>
pub fn zip_with<U: Copy, F, R: Copy>( self, other: HOption<U>, f: F ) -> HOption<R>
Zips self and another Option with function f.
If self is Some(s) and other is Some(o), this method returns Some(f(s, o)).
Otherwise, None is returned.
Examples
#![feature(option_zip)]
#[derive(Debug, PartialEq)]
struct Point {
x: f64,
y: f64,
}
impl Point {
fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
let x = Some(17.5);
let y = Some(42.7);
assert_eq!(x.zip_with(y, Point::new), Some(Point { x: 17.5, y: 42.7 }));
assert_eq!(x.zip_with(None, Point::new), None);source§impl<T: Copy, U: Copy> HOption<(T, U)>
impl<T: Copy, U: Copy> HOption<(T, U)>
sourcepub fn unzip(self) -> (HOption<T>, HOption<U>)
pub fn unzip(self) -> (HOption<T>, HOption<U>)
Unzips an option containing a tuple of two options.
If self is Some((a, b)) this method returns (Some(a), Some(b)).
Otherwise, (None, None) is returned.
Examples
let x = Some((1, "hi"));
let y = None::<(u8, u32)>;
assert_eq!(x.unzip(), (Some(1), Some("hi")));
assert_eq!(y.unzip(), (None, None));source§impl<T: Copy> HOption<HOption<T>>
impl<T: Copy> HOption<HOption<T>>
sourcepub const fn flatten(self) -> HOption<T>
pub const fn flatten(self) -> HOption<T>
Converts from Option<Option<T>> to Option<T>.
Examples
Basic usage:
let x: Option<Option<u32>> = Some(Some(6));
assert_eq!(Some(6), x.flatten());
let x: Option<Option<u32>> = Some(None);
assert_eq!(None, x.flatten());
let x: Option<Option<u32>> = None;
assert_eq!(None, x.flatten());Flattening only removes one level of nesting at a time:
let x: Option<Option<Option<u32>>> = Some(Some(Some(6)));
assert_eq!(Some(Some(6)), x.flatten());
assert_eq!(Some(6), x.flatten().flatten());Trait Implementations§
source§impl<R: Copy> From<Ready<R>> for HOption<R>
impl<R: Copy> From<Ready<R>> for HOption<R>
Transforms Ready<R> to Option<R>.
It is mainly used when the structural hazard (ready bit) has higher priority than data/control hazards.