pub enum ComptimeOption<T> {
None,
Some(T),
}Variants§
Implementations§
Source§impl<T> ComptimeOption<T>
impl<T> ComptimeOption<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);
let x: Option<String> = Some("ownership".to_string());
assert_eq!(x.as_ref().is_some_and(|x| x.len() > 1), true);
println!("still alive {:?}", x);Sourcepub fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool
pub fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool
Returns true if the option is a None or the value inside of it matches a predicate.
§Examples
let x: Option<u32> = Some(2);
assert_eq!(x.is_none_or(|x| x > 1), true);
let x: Option<u32> = Some(0);
assert_eq!(x.is_none_or(|x| x > 1), false);
let x: Option<u32> = None;
assert_eq!(x.is_none_or(|x| x > 1), true);
let x: Option<String> = Some("ownership".to_string());
assert_eq!(x.as_ref().is_none_or(|x| x.len() > 1), true);
println!("still alive {:?}", x);Sourcepub fn as_ref(&self) -> ComptimeOption<&T>
pub fn as_ref(&self) -> ComptimeOption<&T>
Converts from &Option<T> to Option<&T>.
§Examples
Calculates the length of an Option<String> as an Option<usize>
without moving the String. The map method takes the self argument by value,
consuming the original, so this technique uses as_ref to first take an Option to a
reference to the value inside the original.
let text: Option<String> = Some("Hello, world!".to_string());
// First, cast `Option<String>` to `Option<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `text` on the stack.
let text_length: Option<usize> = text.as_ref().map(|s| s.len());
println!("still can print text: {text:?}");Sourcepub fn as_mut(&mut self) -> ComptimeOption<&mut T>
pub fn as_mut(&mut self) -> ComptimeOption<&mut T>
Converts from &mut Option<T> to Option<&mut T>.
§Examples
let mut x = Some(2);
match x.as_mut() {
Some(v) => *v = 42,
None => {},
}
assert_eq!(x, Some(42));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. Panics are meant for unrecoverable errors, and may abort the entire program.
Instead, prefer to use pattern matching and handle the None
case explicitly, or call unwrap_or, unwrap_or_else, or
unwrap_or_default. In functions returning Option, you can use
the ? (try) operator.
§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_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
Sourcepub fn map<U, F>(self, f: F) -> ComptimeOption<U>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, f: F) -> ComptimeOption<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 inspect<F>(self, f: F) -> Self
pub fn inspect<F>(self, f: F) -> Self
Calls a function with a reference to the contained value if Some.
Returns the original option.
§Examples
let list = vec![1, 2, 3];
// prints "got: 2"
let x = list
.get(1)
.inspect(|x| println!("got: {x}"))
.expect("list should be long enough");
// prints nothing
list.get(5).inspect(|x| println!("got: {x}"));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 map_or_else<U, D, F>(self, default: D, f: F) -> U
pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
Computes a default function result (if none), or applies a different function to the contained value (if any).
§Basic examples
let k = 21;
let x = Some("foo");
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
let x: Option<&str> = None;
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);§Handling a Result-based fallback
A somewhat common occurrence when dealing with optional values
in combination with Result<T, E> is the case where one wants to invoke
a fallible fallback if the option is not present. This example
parses a command line argument (if present), or the contents of a file to
an integer. However, unlike accessing the command line argument, reading
the file is fallible, so it must be wrapped with Ok.
let v: u64 = std::env::args()
.nth(1)
.map_or_else(|| std::fs::read_to_string("/etc/someconfig.conf"), Ok)?
.parse()?;Sourcepub fn map_or_default<U, F>(self, f: F) -> U
pub fn map_or_default<U, F>(self, f: F) -> U
Maps an Option<T> to a U by applying function f to the contained
value if the option is Some, otherwise if None, returns the
default value for the type U.
§Examples
let x: Option<&str> = Some("hi");
let y: Option<&str> = None;
assert_eq!(x.map_or_default(|x| x.len()), 2);
assert_eq!(y.map_or_default(|y| y.len()), 0);Sourcepub fn as_deref<'a>(&'a self) -> ComptimeOption<&'a T::Target>
pub fn as_deref<'a>(&'a self) -> ComptimeOption<&'a T::Target>
Converts from Option<T> (or &Option<T>) to Option<&T::Target>.
Leaves the original Option in-place, creating a new one with a reference
to the original one, additionally coercing the contents via Deref.
§Examples
let x: Option<String> = Some("hey".to_owned());
assert_eq!(x.as_deref(), Some("hey"));
let x: Option<String> = None;
assert_eq!(x.as_deref(), None);Sourcepub fn as_deref_mut<'a>(&'a mut self) -> ComptimeOption<&'a mut T::Target>
pub fn as_deref_mut<'a>(&'a mut self) -> ComptimeOption<&'a mut T::Target>
Converts from Option<T> (or &mut Option<T>) to Option<&mut T::Target>.
Leaves the original Option in-place, creating a new one containing a mutable reference to
the inner type’s Deref::Target type.
§Examples
let mut x: Option<String> = Some("hey".to_owned());
assert_eq!(x.as_deref_mut().map(|x| {
x.make_ascii_uppercase();
x
}), Some("HEY".to_owned().as_mut_str()));Sourcepub fn and_then<U, F>(self, f: F) -> ComptimeOption<U>
pub fn and_then<U, F>(self, f: F) -> ComptimeOption<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_else<F>(self, f: F) -> ComptimeOption<T>where
F: FnOnce() -> ComptimeOption<T>,
pub fn or_else<F>(self, f: F) -> ComptimeOption<T>where
F: FnOnce() -> ComptimeOption<T>,
Returns the option if it contains a value, otherwise calls f and
returns the result.
§Examples
fn nobody() -> Option<&'static str> { None }
fn vikings() -> Option<&'static str> { Some("vikings") }
assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians"));
assert_eq!(None.or_else(vikings), Some("vikings"));
assert_eq!(None.or_else(nobody), None);Sourcepub fn zip_with<U, F, R>(
self,
other: ComptimeOption<U>,
f: F,
) -> ComptimeOption<R>
pub fn zip_with<U, F, R>( self, other: ComptimeOption<U>, f: F, ) -> ComptimeOption<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
#[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);Sourcepub fn reduce<U, R, F>(
self,
other: ComptimeOption<U>,
f: F,
) -> ComptimeOption<R>
pub fn reduce<U, R, F>( self, other: ComptimeOption<U>, f: F, ) -> ComptimeOption<R>
Reduces two options into one, using the provided function if both are Some.
If self is Some(s) and other is Some(o), this method returns Some(f(s, o)).
Otherwise, if only one of self and other is Some, that one is returned.
If both self and other are None, None is returned.
§Examples
let s12 = Some(12);
let s17 = Some(17);
let n = None;
let f = |a, b| a + b;
assert_eq!(s12.reduce(s17, f), Some(29));
assert_eq!(s12.reduce(n, f), Some(12));
assert_eq!(n.reduce(s17, f), Some(17));
assert_eq!(n.reduce(n, f), None);Source§impl<T> ComptimeOption<T>
impl<T> ComptimeOption<T>
Sourcepub 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 + IntoRuntime,
pub fn unwrap_or_default(self) -> Twhere
T: Default + IntoRuntime,
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 unsafe fn unwrap_unchecked(self) -> T
pub unsafe fn unwrap_unchecked(self) -> T
Returns the contained Some value, consuming the self value,
without checking that the value is not None.
§Safety
Calling this method on None is undefined behavior.
§Examples
let x = Some("air");
assert_eq!(unsafe { x.unwrap_unchecked() }, "air");let x: Option<&str> = None;
assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); // Undefined behavior!Sourcepub fn and<U>(self, optb: ComptimeOption<U>) -> ComptimeOption<U>where
U: CubeType,
pub fn and<U>(self, optb: ComptimeOption<U>) -> ComptimeOption<U>where
U: CubeType,
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 or(self, optb: ComptimeOption<T>) -> ComptimeOption<T>
pub fn or(self, optb: ComptimeOption<T>) -> ComptimeOption<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: ComptimeOption<T>) -> ComptimeOption<T>
pub fn xor(self, optb: ComptimeOption<T>) -> ComptimeOption<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>(self, other: ComptimeOption<U>) -> ComptimeOption<(T, U)>where
U: CubeType,
pub fn zip<U>(self, other: ComptimeOption<U>) -> ComptimeOption<(T, U)>where
U: CubeType,
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);Source§impl<T, U> ComptimeOption<(T, U)>
impl<T, U> ComptimeOption<(T, U)>
Sourcepub fn unzip(self) -> (ComptimeOption<T>, ComptimeOption<U>)
pub fn unzip(self) -> (ComptimeOption<T>, ComptimeOption<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: CubeType> ComptimeOption<T>
impl<T: CubeType> ComptimeOption<T>
pub fn new_None() -> Self
pub fn __expand_new_None(_: &mut Scope) -> ComptimeOptionExpand<T>
pub fn new_Some(_0: T) -> Self
pub fn __expand_new_Some( _: &mut Scope, _0: <T as CubeType>::ExpandType, ) -> ComptimeOptionExpand<T>
Source§impl<T: CubeType> ComptimeOption<T>
impl<T: CubeType> ComptimeOption<T>
pub fn __expand_Some( scope: &mut Scope, value: T::ExpandType, ) -> ComptimeOptionExpand<T>
Trait Implementations§
Source§impl<T: Clone> Clone for ComptimeOption<T>
impl<T: Clone> Clone for ComptimeOption<T>
Source§fn clone(&self) -> ComptimeOption<T>
fn clone(&self) -> ComptimeOption<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: CubeType> CubeDebug for ComptimeOption<T>
impl<T: CubeType> CubeDebug for ComptimeOption<T>
Source§fn set_debug_name(&self, scope: &mut Scope, name: &'static str)
fn set_debug_name(&self, scope: &mut Scope, name: &'static str)
Source§impl<T: CubeType> CubeType for ComptimeOption<T>
impl<T: CubeType> CubeType for ComptimeOption<T>
type ExpandType = ComptimeOptionExpand<T>
Source§impl<T> Default for ComptimeOption<T>
impl<T> Default for ComptimeOption<T>
Source§fn default() -> ComptimeOption<T>
fn default() -> ComptimeOption<T>
Source§impl<T: LaunchArg> LaunchArg for ComptimeOption<T>
impl<T: LaunchArg> LaunchArg for ComptimeOption<T>
Source§type RuntimeArg<R: Runtime> = ComptimeOptionArgs<T, R>
type RuntimeArg<R: Runtime> = ComptimeOptionArgs<T, R>
Source§type CompilationArg = ComptimeOptionCompilationArg<T>
type CompilationArg = ComptimeOptionCompilationArg<T>
fn register<R: Runtime>( arg: Self::RuntimeArg<R>, launcher: &mut KernelLauncher<R>, ) -> Self::CompilationArg
Source§fn expand(
arg: &Self::CompilationArg,
builder: &mut KernelBuilder,
) -> <Self as CubeType>::ExpandType
fn expand( arg: &Self::CompilationArg, builder: &mut KernelBuilder, ) -> <Self as CubeType>::ExpandType
KernelBuilder.Source§fn expand_output(
arg: &Self::CompilationArg,
builder: &mut KernelBuilder,
) -> <Self as CubeType>::ExpandType
fn expand_output( arg: &Self::CompilationArg, builder: &mut KernelBuilder, ) -> <Self as CubeType>::ExpandType
KernelBuilder.