Struct optional::Optioned [] [src]

pub struct Optioned<T: Noned + Copy> {
    // some 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]

fn some(t: T) -> Self

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

fn none() -> Self

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

# Examples

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

fn is_none(&self) -> bool

Returns true if this Optioned is None, false otherwise.

fn is_some(&self) -> bool

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

fn expect(&self, msg: &str) -> T

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

fn unwrap(&self) -> T

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

fn unpack(&self) -> T

Returns the contained value, even if None.

# Examples

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

fn unwrap_or(&self, def: T) -> T

Returns the contained value or a default.

# Examples

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

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

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));

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

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

# Examples ```

use optional::{some, none};

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

fn map_t<U, F>(self, f: F) -> Optioned<U> where F: FnOnce(T) -> U, U: Noned + Copy

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 ```

use optional::{some, none};

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

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

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

# Examples ```

use optional::{some, none};

assert_eq!("1", some(1usize).map_or("Unknown", |b| b.into())); assert_eq!("Unknown", none::().map_or("Unknown", |b| b.into())); ```

fn map_or_else<U, D, F>(self, default: D, f: F) -> U where D: FnOnce() -> U, F: FnOnce(T) -> U

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

# Examples ```

use optional::{some, none};

assert_eq!("1", some(1usize).map_or_else(|| "Unknown", |b| b.into())); assert_eq!("Unknown", none::().map_or_else( || "Unknown", |b| b.into())); ```

fn take(&mut self) -> Option<T>

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());

fn as_slice(&self) -> &[T]

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

# Examples ```

use optional::{some, none};

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

fn iter(&self) -> OptionedIter<T>

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: Clone + Noned + Copy> Clone for Optioned<T>
[src]

fn clone(&self) -> Optioned<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: Copy + Noned + Copy> Copy for Optioned<T>
[src]

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>());

fn eq(&self, other: &Self) -> bool

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

fn ne(&self, other: &Rhs) -> bool
1.0.0

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]

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool
1.0.0

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

fn le(&self, other: &Rhs) -> bool
1.0.0

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

fn gt(&self, other: &Rhs) -> bool
1.0.0

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

fn ge(&self, other: &Rhs) -> bool
1.0.0

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]

fn cmp(&self, other: &Self) -> Ordering

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

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

fn hash<H>(&self, state: &mut H) where H: Hasher

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: Noned + Copy + Debug> Debug for Optioned<T>
[src]

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

Formats the value using the given formatter.

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

fn default() -> Optioned<T>

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

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

fn from(o: &Option<T>) -> Optioned<T>

Performs the conversion.

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

fn from(o: Option<T>) -> Optioned<T>

Performs the conversion.

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

fn into(self) -> Option<T>

Performs the conversion.