Enum spaceapi_server::api::optional::Optional []

pub enum Optional<T> {
    Value(T),
    Absent,
}

An Optional can contain Optional::Value<T> or Optional::Absent. It is similar to an Option, but Optional::Absent means it will be omitted when serialized, while None will be serialized to null.

Variants

Value
Absent

Methods

impl<T> Optional<T>

fn unwrap(self) -> T

Moves the value v out of the Optional<T> if it is Value(v).

Panics

Panics if the self value equals Absent.

Safety note

In general, because this function may panic, its use is discouraged. Instead, prefer to use pattern matching and handle the Absent case explicitly.

Examples

let x = Value("air");
assert_eq!(x.unwrap(), "air");
let x: Optional<&str> = Absent;
assert_eq!(x.unwrap(), "air"); // fails

fn unwrap_or_else<F>(self, f: F) -> T where F: FnOnce() -> T

Returns the contained value or computes it from a closure.

Examples

let k = 10;
assert_eq!(Value(4).unwrap_or_else(|| 2 * k), 4);
assert_eq!(Absent.unwrap_or_else(|| 2 * k), 20);

fn map<U, F>(self, f: F) -> Optional<U> where F: FnOnce(T) -> U

Maps an Optional<T> to Optional<U> by applying a function to a contained value

Examples

Convert an Optional<String> into an Optional<usize>, consuming the original:

let num_as_str: Optional<String> = Value("10".to_string());
// `Optional::map` takes self *by value*, consuming `num_as_str`
let num_as_int: Optional<usize> = num_as_str.map(|n| n.len());

fn map_or<U, F>(self, def: U, f: F) -> U where F: FnOnce(T) -> U

Applies a function to the contained value or returns a default. see std::option::Option<T>::map_or

fn as_mut(&'r mut self) -> Optional<&'r mut T>

Converts from Optional<T> to Optional<&mut T>

fn as_ref(&'r self) -> Optional<&'r T>

Converts from Optional<T> to Optional<&T>

fn and_then<U, F>(self, f: F) -> Optional<U> where F: FnOnce(T) -> Optional<U>

Returns Absent if the optional is Absent, otherwise calls f with the wrapped value and returns the result.

Some languages call this operation flatmap.

Examples

fn sq(x: u32) -> Optional<u32> { Value(x * x) }
fn nope(_: u32) -> Optional<u32> { Absent }

assert_eq!(Value(2).and_then(sq).and_then(sq), Value(16));
assert_eq!(Value(2).and_then(sq).and_then(nope), Absent);
assert_eq!(Value(2).and_then(nope).and_then(sq), Absent);
assert_eq!(Absent.and_then(sq).and_then(sq), Absent);

fn is_absent(&self) -> bool

Returns true if the optional is a Absent value

Examples

let x: Optional<u32> = Value(2);
assert_eq!(x.is_absent(), false);

let x: Optional<u32> = Absent;
assert_eq!(x.is_absent(), true);

Trait Implementations

impl<T> Into<Option<T>> for Optional<T>

fn into(self) -> Option<T>

Derived Implementations

impl<T> Hash for Optional<T> where T: Hash

fn hash<__H>(&self, __arg_0: &mut __H) where __H: Hasher

impl<T> Debug for Optional<T> where T: Debug

fn fmt(&self, __arg_0: &mut Formatter) -> Result<(), Error>

impl<T> Ord for Optional<T> where T: Ord

fn cmp(&self, __arg_0: &Optional<T>) -> Ordering

impl<T> Eq for Optional<T> where T: Eq

impl<T> PartialOrd<Optional<T>> for Optional<T> where T: PartialOrd<T>

fn partial_cmp(&self, __arg_0: &Optional<T>) -> Option<Ordering>

fn lt(&self, __arg_0: &Optional<T>) -> bool

fn le(&self, __arg_0: &Optional<T>) -> bool

fn gt(&self, __arg_0: &Optional<T>) -> bool

fn ge(&self, __arg_0: &Optional<T>) -> bool

impl<T> PartialEq<Optional<T>> for Optional<T> where T: PartialEq<T>

fn eq(&self, __arg_0: &Optional<T>) -> bool

fn ne(&self, __arg_0: &Optional<T>) -> bool

impl<T> Copy for Optional<T> where T: Copy

impl<T> Clone for Optional<T> where T: Clone

fn clone(&self) -> Optional<T>