Enum spaceapi::optional::Optional [] [src]

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(T)Absent

Methods

impl<T> Optional<T>
[src]

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: FnOnce() -> T>(self, f: F) -> 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: FnOnce(T) -> U>(self, f: F) -> Optional<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: FnOnce(T) -> U>(self, def: U, f: F) -> U

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

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

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

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

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

fn and_then<U, F: FnOnce(T) -> Optional<U>>(self, f: F) -> 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: Hash> Hash for Optional<T>
[src]

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

Feeds this value into the state given, updating the hasher as necessary.

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0

Feeds a slice of this type into the state provided.

impl<T: Debug> Debug for Optional<T>
[src]

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

Formats the value using the given formatter.

impl<T: Ord> Ord for Optional<T>
[src]

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

This method returns an Ordering between self and other. Read more

impl<T: Eq> Eq for Optional<T>
[src]

impl<T: PartialOrd> PartialOrd for Optional<T>
[src]

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

This method returns an ordering between self and other values if one exists. Read more

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

This method tests less than (for self and other) and is used by the < operator. Read more

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

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

This method tests greater than (for self and other) and is used by the > operator. Read more

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T: PartialEq> PartialEq for Optional<T>
[src]

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

This method tests for self and other values to be equal, and is used by ==. Read more

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

This method tests for !=.

impl<T: Copy> Copy for Optional<T>
[src]

impl<T: Clone> Clone for Optional<T>
[src]

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

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more

impl<T> Into<Option<T>> for Optional<T>
[src]

fn into(self) -> Option<T>

Convert Optional into Option

Examples

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

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