[][src]Struct galbi::option_box::OptionBox

pub struct OptionBox<T> { /* fields omitted */ }

Implementations

impl<T> OptionBox<T>[src]

pub fn new(x: Option<T>) -> OptionBox<T>[src]

Create a new object.

use galbi::*;

let value = OptionBox::new(Some(10));

pub fn some(x: T) -> OptionBox<T>[src]

Create a new object.

use galbi::*;

let value:OptionBox<i32> = OptionBox::some(10);

pub fn none() -> OptionBox<T>[src]

Create a new object.

use galbi::*;

let value:OptionBox<u32> = OptionBox::none();

pub fn is_some(&self) -> bool[src]

Returns true if the option is a Some value.

use galbi::*;

let x: OptionBox<u32> = OptionBox::some(19);
assert_eq!(x.is_some(), true);

let x: OptionBox<u32> = OptionBox::none();
assert_eq!(x.is_some(), false);

pub fn is_none(&self) -> bool[src]

Returns true if the option is a None value.

use galbi::*;

let x: OptionBox<u32> = OptionBox::some(19);
assert_eq!(x.is_none(), false);

let x: OptionBox<u32> = OptionBox::none();
assert_eq!(x.is_none(), true);

pub fn as_ref(&self) -> Option<&Box<T>>[src]

Converts from &mut OptionBox to Option<&Box>.

use galbi::*;

let text: OptionBox<String> = OptionBox::some("Hello, world!".to_string());
// First, cast `OptionBox<String>` to `Option<&Box<T>>` 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);

pub fn as_mut(&mut self) -> Option<&mut Box<T>>[src]

Converts from &mut OptionBox to Option<&mut Box>.

use galbi::*;

let mut x = OptionBox::some(2);
match x.as_mut() {
   Some(v) => *v = Box::new(42),
   None => {},
}
assert_eq!(x, OptionBox::some(42));

pub fn expect(self, msg: &str) -> Box<T>[src]

Panics if the value is a None with a custom panic message provided by msg.

use galbi::*;

let x = OptionBox::some("foo".to_string());
let value = x.expect("error");
assert_eq!(value, Box::new("foo".to_string()));

pub fn unwrap(self) -> Box<T>[src]

Returns the contained Some value, consuming the self value. Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the None case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.

use galbi::*;

let x = OptionBox::some("foo".to_string());
let value = x.unwrap();
assert_eq!(value, Box::new("foo".to_string()));

pub fn unwrap_or(self, default: Box<T>) -> Box<T>[src]

Returns the contained Some value, consuming the self value. Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the None case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.

use galbi::*;

assert_eq!(OptionBox::some(12).unwrap_or(Box::new(13)), Box::new(12));
assert_eq!(OptionBox::none().unwrap_or(Box::new(13)), Box::new(13));

pub fn unwrap_or_else<F>(self, f: F) -> Box<T> where
    F: FnOnce() -> Box<T>, 
[src]

Returns the contained Some value or computes it from a closure.

use galbi::*;

assert_eq!(OptionBox::some(12).unwrap_or_else(|| Box::new(13)), Box::new(12));
assert_eq!(OptionBox::none().unwrap_or_else(|| Box::new(13)), Box::new(13));

pub fn map<U, F>(self, f: F) -> Option<U> where
    F: FnOnce(Box<T>) -> U, 
[src]

Maps an Option<Box> to Option by applying a function to a contained value.

use galbi::*;

assert_eq!(OptionBox::some(20).map(|e| (*e)*2), Some(40));

pub fn map_or<U, F>(self, default: U, f: F) -> U where
    F: FnOnce(Box<T>) -> U, 
[src]

Applies a function to the contained value (if any), or returns the provided default (if not). 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.

use galbi::*;

let x = OptionBox::some(10);
assert_eq!(x.map_or(111, |e| *e), 10);

let x: OptionBox<i32> = OptionBox::none();
assert_eq!(x.map_or(10, |e| *e), 10);

pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U where
    D: FnOnce() -> U,
    F: FnOnce(Box<T>) -> U, 
[src]

Applies a function to the contained value (if any), or computes a default (if not).

use galbi::*;

let x = OptionBox::some(10);
assert_eq!(x.map_or_else(||111, |e| *e), 10);

let x: OptionBox<i32> = OptionBox::none();
assert_eq!(x.map_or_else(||10, |e| *e), 10);

pub fn ok_or<E>(self, err: E) -> Result<Box<T>, E>[src]

Transforms the Option<Box<T>> into a Result<Box<T>, E>, mapping Some(v) to Ok(v) and None to Err(err). Arguments passed to ok_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use ok_or_else, which is lazily evaluated.

use galbi::*;

let x = OptionBox::some(10);
assert_eq!(x.ok_or(Box::new(0)), Ok(Box::new(10)));

let x: OptionBox<i32> = OptionBox::none();
assert_eq!(x.ok_or(Box::new(0)), Err(Box::new(0)));

pub fn ok_or_else<E, F>(self, err: F) -> Result<Box<T>, E> where
    F: FnOnce() -> E, 
[src]

Transforms the Option<Box> into a Result<Box, E>, mapping Some(v) to Ok(v) and None to Err(err()).

use galbi::*;

let x = OptionBox::some(10);
assert_eq!(x.ok_or_else(||Box::new(0)), Ok(Box::new(10)));

let x: OptionBox<i32> = OptionBox::none();
assert_eq!(x.ok_or_else(||Box::new(0)), Err(Box::new(0)));

pub fn iter(&self) -> Iter<'_, Box<T>>[src]

Returns an iterator over the possibly contained value.

use galbi::*;

let x = OptionBox::some(10);
assert_eq!(x.iter().next(), Some(&Box::new(10)));

let x: OptionBox<i32> = OptionBox::none();
assert_eq!(x.iter().next(), None);

pub fn iter_mut(&mut self) -> IterMut<'_, Box<T>>[src]

Returns a mutable iterator over the possibly contained value.

use galbi::*;

let mut x = OptionBox::some(4);
match x.iter_mut().next() {
   Some(v) => *v = Box::new(42),
   None => {},
}
assert_eq!(x, OptionBox::some(42));

let mut x: OptionBox<u32> = OptionBox::none();
assert_eq!(x.iter_mut().next(), None);

pub fn and<U>(self, optb: Option<U>) -> Option<U>[src]

Returns None if the option is None, otherwise returns optb.

use galbi::*;

let x = OptionBox::some(2);
let y: Option<String> = None;
assert_eq!(x.and(y), None);

let x: OptionBox<i32> = OptionBox::none();
let y = Some("foo".to_string());
assert_eq!(x.and(y), None);

let x = OptionBox::some(2);
let y = Some("foo".to_string());
assert_eq!(x.and(y), Some("foo".to_string()));

let x: OptionBox<i32> = OptionBox::none();
let y: Option<String> = None;
assert_eq!(x.and(y), None);

pub fn and_then<U, F>(self, f: F) -> Option<U> where
    F: FnOnce(Box<T>) -> Option<U>, 
[src]

Returns None if the option is None, otherwise calls f with the wrapped value and returns the result. Some languages call this operation flatmap.

use galbi::*;

fn sq(x: Box<u32>) -> Option<u32> {
    Some((*x)*(*x))
}
fn nope(x: Box<u32>) -> Option<u32> {
    None
}

assert_eq!(OptionBox::some(2).and_then(sq), Some(4));
assert_eq!(OptionBox::some(2).and_then(nope), None);
assert_eq!(OptionBox::<u32>::none().and_then(sq), None);

pub fn filter<P>(self, predicate: P) -> Option<Box<T>> where
    P: FnOnce(&Box<T>) -> bool
[src]

Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:

  • [Some(t)] if predicate returns true (where t is the wrapped value), and
  • None if predicate returns false. This function works similar to Iterator::filter(). You can imagine the Option<Box> being an iterator over one or zero elements. filter() lets you decide which elements to keep.
use galbi::*;

fn is_even(ptr: &Box<i32>) -> bool {
    let num: i32 = **ptr;
    num % 2 == 0
}

assert_eq!(OptionBox::none().filter(is_even), None);
assert_eq!(OptionBox::some(3).filter(is_even), None);
assert_eq!(OptionBox::some(4).filter(is_even), Some(Box::new(4)));

pub fn or(self, optb: Option<Box<T>>) -> Option<Box<T>>[src]

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.

use galbi::*;

let x = OptionBox::some(2);
let y = None;
assert_eq!(x.or(y), Some(Box::new(2)))

pub fn or_else<F>(self, f: F) -> Option<Box<T>> where
    F: FnOnce() -> Option<Box<T>>, 
[src]

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

use galbi::*;

let x: OptionBox<i32> = OptionBox::none();
assert_eq!(x.or_else(|| Some(Box::new(1234))), Some(Box::new(1234)))

pub fn xor(self, optb: Option<Box<T>>) -> Option<Box<T>>[src]

Returns Some if exactly one of self, optb is Some, otherwise returns None.

use galbi::*;

let x = OptionBox::some(2);
let y = None;
assert_eq!(x.xor(y), Some(Box::new(2)))

pub fn get_or_insert(&mut self, v: Box<T>) -> &mut Box<T>[src]

Inserts v into the option if it is None, then returns a mutable reference to the contained value.

use galbi::*;

let mut x:OptionBox<i32> = OptionBox::none();
{
    let y: &mut Box<i32> = x.get_or_insert(Box::new(5));
    assert_eq!(y, &Box::new(5));

    *y = Box::new(10);
}

assert_eq!(x, OptionBox::some(10))

pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut Box<T> where
    F: FnOnce() -> Box<T>, 
[src]

Inserts a value computed from f into the option if it is None, then returns a mutable reference to the contained value.

use galbi::*;

let mut x:OptionBox<i32> = OptionBox::none();
{
    let y: &mut Box<i32> = x.get_or_insert_with(|| Box::new(5));
    assert_eq!(y, &Box::new(5));

    *y = Box::new(10);
}

assert_eq!(x, OptionBox::some(10))

pub fn take(&mut self) -> Option<Box<T>>[src]

Takes the value out of the option, leaving a None in its place.

use galbi::*;

let mut x = OptionBox::some(1234);
let y = x.take();

assert_eq!(x, OptionBox::none());
assert_eq!(y, Some(Box::new(1234)));

pub fn replace(&mut self, value: Box<T>) -> Option<Box<T>>[src]

Replaces the actual value in the option by the value given in parameter, returning the old value if present, leaving a Some in its place without deinitializing either one.

use galbi::*;

let mut x = OptionBox::some(2);
let old = x.replace(Box::new(5));
assert_eq!(x, OptionBox::some(5));
assert_eq!(old, Some(Box::new(2)));

pub fn zip<U>(self, other: Option<U>) -> Option<(Box<T>, U)>[src]

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.

use galbi::*;

let x = OptionBox::some(1);
let y = Some("foo");
let z = None::<i32>;

assert_eq!(x.zip(y), Some((Box::new(1), "foo")));

let x = OptionBox::some(1);
assert_eq!(x.zip(z), None);

Trait Implementations

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

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

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

impl<T: Hash> Hash for OptionBox<T>[src]

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

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

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

impl<T> StructuralEq for OptionBox<T>[src]

impl<T> StructuralPartialEq for OptionBox<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for OptionBox<T> where
    T: RefUnwindSafe
[src]

impl<T> Send for OptionBox<T> where
    T: Send
[src]

impl<T> Sync for OptionBox<T> where
    T: Sync
[src]

impl<T> Unpin for OptionBox<T>[src]

impl<T> UnwindSafe for OptionBox<T> where
    T: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.