[][src]Enum these::These

pub enum These<T, U> {
    This(T),
    That(U),
    Both(T, U),
}

Variants

This(T)
That(U)
Both(T, U)

Implementations

impl<T, U> These<T, U>[src]

pub fn as_ref(&self) -> These<&T, &U>[src]

Convert from &These<T, U> to These<&T, &U>. Produce a new These, containing references to the original values.

Examples

use these::These;

let this: These<_, u32> = These::This(String::from("Hello"));
assert_eq!(this.as_ref().this(), Some(&String::from("Hello")));
assert_eq!(this.as_ref().that(), None);

let that: These<&str, _> = These::That(42);
let forty_two = that.as_ref().that();
assert_eq!(forty_two, Some(&42));
assert_eq!(that.this(), None);

let these = These::Both("Hello", 42);
assert_eq!(these.as_ref().here(), Some(&"Hello"));
assert_eq!(these.as_ref().there(), Some(&42));

pub fn as_mut(&mut self) -> These<&mut T, &mut U>[src]

Convert from &mut These<T, U> to These<&mut T, &mut U>. Produce a new These, containing mutable references to the original values.

Examples

use these::These;

let mut this: These<_, u32> = These::This(String::from("Hello"));
let mut world = this.as_mut().this().unwrap();
world.push_str(" World!");
assert_eq!(this.as_mut().this(), Some(&mut String::from("Hello World!")));
assert_eq!(this.as_mut().that(), None);

let mut that: These<&str, _> = These::That(42);
let forty_two = that.as_mut().that().unwrap();
*forty_two += 1;
assert_eq!(that.as_ref().that(), Some(&43));
assert_eq!(that.this(), None);

let mut these = These::Both("Hello", 42);
assert_eq!(these.as_mut().here(), Some(&mut "Hello"));
assert_eq!(these.as_mut().there(), Some(&mut 42));

pub fn collapse_these<V, F, G, H>(self, f: F, g: G, h: H) -> V where
    F: FnOnce(T) -> V,
    G: FnOnce(U) -> V,
    H: FnOnce(T, U) -> V, 
[src]

Collapse a These value given a set of three functions to some target type.

The first function will apply to This, the second to That, and the third to These.

This can be thought of as matching on the value and applying the functions, but removes the need to match.

Examples

use these::These;

// Functions to use for collapsing
let f = |s: &str| s.len();
let g = |i| i * 42;
let h = |s: &str, i| s.len() + i;

let this: These<&str, usize> = These::This("Hello");
assert_eq!(this.collapse_these(f, g, h), 5);

let that: These<&str, usize> = These::That(42);
assert_eq!(that.collapse_these(f, g, h), 1764);

let these: These<&str, usize> = These::Both("Hello", 42);
assert_eq!(these.collapse_these(f, g, h), 47);

pub fn map<V, F>(self, op: F) -> These<T, V> where
    F: FnOnce(U) -> V, 
[src]

Apply the function to the U value if the data is That or These.

Examples

use these::These;

let this: These<i8, i8> = These::This(1);
assert_eq!(this.map(|x| x + 1), These::This(1));

let that: These<i8, i8> = These::That(1);
assert_eq!(that.map(|x| x + 1), These::That(2));

let these: These<&str, i8> = These::Both("Hello", 1);
assert_eq!(these.map(|x| x + 1), These::Both("Hello", 2));

pub fn map_first<V, F>(self, op: F) -> These<V, U> where
    F: FnOnce(T) -> V, 
[src]

Apply the function to the T value if the data is This or These.

Examples

use these::These;

let this: These<i8, i8> = These::This(1);
assert_eq!(this.map_first(|x| x + 1), These::This(2));

let that: These<i8, i8> = These::That(1);
assert_eq!(that.map_first(|x| x + 1), These::That(1));

let these: These<&str, i8> = These::Both("Hello", 1);
assert_eq!(these.map_first(|s| s.len()), These::Both(5, 1));

pub fn map_second<V, F>(self, op: F) -> These<T, V> where
    F: FnOnce(U) -> V, 
[src]

Apply the function to the U value if the data is That or These. This is the same as map.

Examples

use these::These;

let this: These<i8, i8> = These::This(1);
assert_eq!(this.map_second(|x| x + 1), These::This(1));

let that: These<i8, i8> = These::That(1);
assert_eq!(that.map_second(|x| x + 1), These::That(2));

let these: These<&str, i8> = These::Both("Hello", 1);
assert_eq!(these.map_second(|i| i + 1), These::Both("Hello", 2));

pub fn map_both<V, W, F, G>(self, f: F, g: G) -> These<V, W> where
    F: FnOnce(T) -> V,
    G: FnOnce(U) -> W, 
[src]

Apply both functions to the T and U values respectively.

Examples

use these::These;

let f = |s: &str| s.len();
let g = |i| i * i;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.map_both(f, g), These::This(5));

let that: These<&str, i8> = These::That(8);
assert_eq!(that.map_both(f, g), These::That(64));

let these: These<&str, i8> = These::Both("Hello", 9);
assert_eq!(these.map_both(f, g), These::Both(5, 81));

pub fn fold_these<V, F>(self, default: V, op: F) -> V where
    F: FnOnce(U, V) -> V, 
[src]

Collapse the These value but using a default value in place of U (ignoring any U values).

If This is found it will return the default value. If That is found it will use the function with the value contained in That along with the default. If These is found it will use the function with the second element along with the default, ignoring the first element.

Examples

use these::These;

let f = |v, i| v * i;

let this: These<&str, i16> = These::This("Hello");
assert_eq!(this.fold_these(42, f), 42);

let that: These<&str, i16> = These::That(8);
assert_eq!(that.fold_these(42, f), 336);

let these: These<&str, i16> = These::Both("Hello", 9);
assert_eq!(these.fold_these(42, f), 378);

pub fn from_these(self, t_default: T, u_default: U) -> (T, U)[src]

Create a tuple given a These value. In the case of This or That it will use the default values provided.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.from_these("World", 42), ("Hello", 42));

let that: These<&str, i8> = These::That(42);
assert_eq!(that.from_these("Hello", 100), ("Hello", 42));

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.from_these("World", 42), ("Hello", 42));

pub fn swap_these(self) -> These<U, T>[src]

Swap the types of values of a These value.

This turns into That. That turns into This. These values swap their order.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.swap_these(), These::That("Hello"));

let that: These<&str, i8> = These::That(42);
assert_eq!(that.swap_these(), These::This(42));

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.swap_these(), These::Both(42, "Hello"));

pub fn this(self) -> Option<T>[src]

Produce a Some from This, otherwise return None.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.this(), Some("Hello"));

let that: These<&str, i8> = These::That(42);
assert_eq!(that.this(), None);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.this(), None);

pub fn that(self) -> Option<U>[src]

Produce a Some from That, otherwise return None.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.that(), None);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.that(), Some(42));

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.that(), None);

pub fn these(self) -> Option<(T, U)>[src]

Produce a Some from These, otherwise return None.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.these(), None);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.these(), None);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.these(), Some(("Hello", 42)));

pub fn here(self) -> Option<T>[src]

Produce a Some if a This or These is found, otherwise None.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.here(), Some("Hello"));

let that: These<&str, i8> = These::That(42);
assert_eq!(that.here(), None);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.here(), Some("Hello"));

pub fn there(self) -> Option<U>[src]

Produce a Some if a That or These is found, otherwise None.

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.there(), None);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.there(), Some(42));

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.there(), Some(42));

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

Is it This?

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.is_this(), true);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.is_this(), false);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.is_this(), false);

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

Is it That?

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.is_that(), false);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.is_that(), true);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.is_that(), false);

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

Is it These?

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.is_these(), false);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.is_these(), false);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.is_these(), true);

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

Is it Here, i.e. This or These?

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.is_here(), true);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.is_here(), false);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.is_here(), true);

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

Is it There, i.e. That or These?

Examples

use these::These;

let this: These<&str, i8> = These::This("Hello");
assert_eq!(this.is_there(), false);

let that: These<&str, i8> = These::That(42);
assert_eq!(that.is_there(), true);

let these: These<&str, i8> = These::Both("Hello", 42);
assert_eq!(these.is_there(), true);

pub fn partition_these(xs: Vec<These<T, U>>) -> (Vec<T>, Vec<U>, Vec<(T, U)>)[src]

When given a Vec of These it will split it into three separate Vecs, each containing the This, That, or These inner values.

Examples

use these::These;

let xs = vec![These::This(1), These::That("Hello"), These::Both(42, "World")];
assert_eq!(These::partition_these(xs), (vec![1], vec!["Hello"], vec![(42, "World")]));

pub fn partition_here_there(xs: Vec<These<T, U>>) -> (Vec<T>, Vec<U>)[src]

When given a Vec of These it will split it into two separate Vecs, each containing the T or U inner values.

Examples

use these::These;

let xs = vec![These::This(1), These::That("Hello"), These::Both(42, "World")];
assert_eq!(These::partition_here_there(xs), (vec![1, 42], vec!["Hello", "World"]));

pub fn from_option(opt: Option<T>, default: U) -> Self[src]

pub fn combine<F, G>(self, other: Self, f: F, g: G) -> Self where
    F: FnOnce(T, T) -> T,
    G: FnOnce(U, U) -> U, 
[src]

The semigroup combination of two These values.

It will combine two values of the same type, using f and g.

It will also "promote" the outcome to a These::Both if it encounters a T and U value.

Trait Implementations

impl<T: Clone, U: Clone> Clone for These<T, U>[src]

impl<T: Copy, U: Copy> Copy for These<T, U>[src]

impl<T: Debug, U: Debug> Debug for These<T, U>[src]

impl<T: Eq, U: Eq> Eq for These<T, U>[src]

impl<E, T> From<Result<T, E>> for These<T, E>[src]

impl<T: Hash, U: Hash> Hash for These<T, U>[src]

impl<T: Ord, U: Ord> Ord for These<T, U>[src]

impl<T: PartialEq, U: PartialEq> PartialEq<These<T, U>> for These<T, U>[src]

impl<T: PartialOrd, U: PartialOrd> PartialOrd<These<T, U>> for These<T, U>[src]

impl<T, U> StructuralEq for These<T, U>[src]

impl<T, U> StructuralPartialEq for These<T, U>[src]

Auto Trait Implementations

impl<T, U> RefUnwindSafe for These<T, U> where
    T: RefUnwindSafe,
    U: RefUnwindSafe

impl<T, U> Send for These<T, U> where
    T: Send,
    U: Send

impl<T, U> Sync for These<T, U> where
    T: Sync,
    U: Sync

impl<T, U> Unpin for These<T, U> where
    T: Unpin,
    U: Unpin

impl<T, U> UnwindSafe for These<T, U> where
    T: UnwindSafe,
    U: UnwindSafe

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.