Struct optional::Optioned [] [src]

pub struct Optioned<T: Noned + Copy> { /* fields omitted */ }

An Option<T>-like structure that takes only as much space as the enclosed value, at the cost of removing one particular None value from the value domain (see Noned)

Methods

impl<T: Noned + Copy> Optioned<T>
[src]

[src]

Create an Optioned<T> that is some(t).

Panics

panics if the supplied value is the None value

Examples

Optioned::<i32>::some(1); // Optioned(1)
Optioned::<f64>::some(std::f64::NAN); // panic!s

[src]

Create an Optioned<T> that is none().

Examples

Optioned::<u16>::none(); // Optioned(std::u16::MAX)

[src]

Returns true if this Optioned is None, false otherwise.

[src]

Returns true if this Optioned contains a value, false otherwise.

[src]

Unwraps the value, if any, else panics with the given message.

Panics

if self is None

Examples

For Some(_), the corresponding value is returned.

assert_eq!(42u8, Optioned::some(42u8).expect("FAIL"));

On None, it panics with the given message.

Optioned::<u8>::none().expect("FAIL"); // panics with FAIL

[src]

Unwraps the value, if any, else panics with "unwrap called on None".

Panics

if self is None

Examples

For Some(_), the corresponding value is returned.

assert_eq!(42u8, Optioned::some(42u8).unwrap());

On None, it panics with the given message.

Optioned::<u8>::none().unwrap(); // panics

[src]

Returns the contained value, even if None.

Examples

assert_eq!(-128i8, none().unpack());
assert_eq!(1u32, some(1).unpack());

[src]

Returns the contained value or a default.

Examples

assert_eq!(-1i8, some(-1i8).unwrap_or(127i8));
assert_eq!(42u16, none().unwrap_or(42u16));

[src]

Returns the contained value or a calculated default.

Examples

assert_eq!(-1i8, some(-1i8).unwrap_or_else(|| panic!()));
assert_eq!(42u16, none().unwrap_or_else(|| 42u16));

[src]

Maps the Optioned to an Option<U> by applying the function over the contained value, if any.

Examples

assert_eq!(Some(-42), some(42i8).map(|x| -x));
assert_eq!(None, none::<i8>().map(|x| -x));

[src]

Maps the Optioned<T> to an Optioned<U> by applying the function over the contained value, if any. Requires that the result type of the function be Noned + Copy, as other types aren't compatible with Optioned.

Examples

assert_eq!(some(-42), some(42i8).map_t(|x| -x));
assert_eq!(none::<i8>(), none::<i8>().map_t(|x| -x));

[src]

Maps the contained value to a U by applying the function or return a default.

Examples

assert_eq!("1", some(1usize).map_or("Unknown".to_string(), |b| b.to_string()));
assert_eq!("Unknown", none::<usize>().map_or("Unknown".to_string(), |b| b.to_string()));

[src]

Maps a value to a U by applying the function or return a computed default.

Examples

assert_eq!("1", some(1usize).map_or_else(|| "Unknown".to_string(),
    |b| b.to_string()));
assert_eq!("Unknown", none::<usize>().map_or_else(
    || "Unknown".to_string(), |b| b.to_string()));

[src]

Returns this option if it contains a value, otherwise returns the other.

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: Optioned<u32> = none();
let y = none();
assert_eq!(x.or(y), none());

[src]

Returns this option if it contains a value, otherwise calls f and returns the result.

Examples

fn nothing() -> Optioned<u32> { none() }
fn something() -> Optioned<u32> { some(1) }

assert_eq!(some(2).or_else(something), some(2));
assert_eq!(none().or_else(something), some(1));
assert_eq!(none().or_else(nothing), none());

[src]

Returns the None value for type U if this value or other contains their respective None values. Otherwise returns the other Optioned struct.

Examples

let the_other = some::<u32>(42);

assert_eq!(some('a').and(the_other), some(42));
assert_eq!(none::<char>().and(the_other), none::<u32>());
assert_eq!(some('a').and(none::<u32>()), none::<u32>());
assert_eq!(none::<char>().and(none::<u32>()), none::<u32>());

[src]

Returns this Optioned if it contains the the None value, otherwise calls f with the contained value and returns the result as an Optioned<U>.

Examples

fn nothing() -> Optioned<u32> { none() }
fn something() -> Optioned<u32> { some(1) }
fn add_two(val: u32) -> Optioned<u32> {
  wrap( val + 2)
}
 
fn failed_function(val: u32) -> Optioned<u32> {
  none()
}

assert_eq!(some(2).and_then(add_two), some(4));
assert_eq!(none().and_then(add_two), none());
assert_eq!(some(2).and_then(failed_function), none());
assert_eq!(none().and_then(failed_function), none());

[src]

Takes the value out of the Optioned and returns ist as Option<T>, changing self to None.

Examples

let mut x = some(1u8);
assert_eq!(Some(1u8), x.take());
assert!(x.is_none());

Important traits for &'a mut [u8]
[src]

Return a possibly empty slice over the contained value, if any.

Examples

assert_eq!(&[42], some(42u8).as_slice());
assert!(none::<i16>().as_slice().is_empty());

Important traits for OptionedIter<T>
[src]

return an iterator over all contained (that is zero or one) values.

Examples

assert_eq!(None, none::<u64>().iter().next());
assert_eq!(Some(42u64), some(42u64).iter().next());

Trait Implementations

impl<T: Copy + Noned + Copy> Copy for Optioned<T>
[src]

impl<T: Clone + Noned + Copy> Clone for Optioned<T>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T> PartialEq for Optioned<T> where
    T: OptEq + Noned + Copy
[src]

Equality works as usual.

Examples

assert_eq!(some(1u8), some(1u8));
assert_eq!(none::<u32>(), none::<u32>());

[src]

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

1.0.0
[src]

This method tests for !=.

impl<T> Eq for Optioned<T> where
    T: OptEq + Noned + Copy
[src]

impl<T> PartialOrd for Optioned<T> where
    T: PartialEq + OptEq + OptOrd + Noned + Copy
[src]

[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

impl<T> Ord for Optioned<T> where
    T: Eq + OptEq + OptOrd + Noned + Copy
[src]

[src]

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

1.21.0
[src]

Compares and returns the maximum of two values. Read more

1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl<T> Hash for Optioned<T> where
    T: Noned + Copy + Hash
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<T: Noned + Copy + Debug> Debug for Optioned<T>
[src]

[src]

Formats the value using the given formatter. Read more

impl<T: Noned + Copy> Default for Optioned<T>
[src]

[src]

Returns the "default value" for a type. Read more

impl<'a, T: Noned + Copy> From<&'a Option<T>> for Optioned<T>
[src]

[src]

Performs the conversion.

impl<T: Noned + Copy> From<Option<T>> for Optioned<T>
[src]

[src]

Performs the conversion.

impl<T: Noned + Copy> Into<Option<T>> for Optioned<T>
[src]

[src]

Performs the conversion.

impl<T: Noned + Copy> From<T> for Optioned<T>
[src]

[src]

Performs the conversion.

Auto Trait Implementations

impl<T> Send for Optioned<T> where
    T: Send

impl<T> Sync for Optioned<T> where
    T: Sync