Enum These

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

Variants§

§

This(T)

§

That(U)

§

Both(T, U)

Implementations§

Source§

impl<T, U> These<T, U>

Source

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

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

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

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

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,

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

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

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

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

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

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

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

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,

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

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

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

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

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

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

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

pub fn this(self) -> Option<T>

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

pub fn that(self) -> Option<U>

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

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

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

pub fn here(self) -> Option<T>

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

pub fn there(self) -> Option<U>

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

pub fn is_this(&self) -> bool

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

pub fn is_that(&self) -> bool

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

pub fn is_these(&self) -> bool

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

pub fn is_here(&self) -> bool

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

pub fn is_there(&self) -> bool

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

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

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")]));
Source

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

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"]));
Source

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

Source

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

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§

Source§

impl<T: Clone, U: Clone> Clone for These<T, U>

Source§

fn clone(&self) -> These<T, U>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, U: Debug> Debug for These<T, U>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<E, T> From<Result<T, E>> for These<T, E>

Source§

fn from(result: Result<T, E>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash, U: Hash> Hash for These<T, U>

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

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

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord, U: Ord> Ord for These<T, U>

Source§

fn cmp(&self, other: &These<T, U>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq, U: PartialEq> PartialEq for These<T, U>

Source§

fn eq(&self, other: &These<T, U>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, U: PartialOrd> PartialOrd for These<T, U>

Source§

fn partial_cmp(&self, other: &These<T, U>) -> Option<Ordering>

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

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy, U: Copy> Copy for These<T, U>

Source§

impl<T: Eq, U: Eq> Eq for These<T, U>

Source§

impl<T, U> StructuralPartialEq for These<T, U>

Auto Trait Implementations§

§

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

§

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

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.