pub enum These<T, U> {
This(T),
That(U),
Both(T, U),
}
Variants§
Implementations§
Source§impl<T, U> These<T, U>
impl<T, U> These<T, U>
Sourcepub fn as_ref(&self) -> These<&T, &U>
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));
Sourcepub fn as_mut(&mut self) -> These<&mut T, &mut U>
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));
Sourcepub fn collapse_these<V, F, G, H>(self, f: F, g: G, h: H) -> V
pub fn collapse_these<V, F, G, H>(self, f: F, g: G, h: H) -> 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);
Sourcepub fn map<V, F>(self, op: F) -> These<T, V>where
F: FnOnce(U) -> V,
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));
Sourcepub fn map_first<V, F>(self, op: F) -> These<V, U>where
F: FnOnce(T) -> V,
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));
Sourcepub fn map_second<V, F>(self, op: F) -> These<T, V>where
F: FnOnce(U) -> V,
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));
Sourcepub fn map_both<V, W, F, G>(self, f: F, g: G) -> These<V, W>
pub fn map_both<V, W, F, G>(self, f: F, g: G) -> These<V, 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));
Sourcepub fn fold_these<V, F>(self, default: V, op: F) -> Vwhere
F: FnOnce(U, V) -> V,
pub fn fold_these<V, F>(self, default: V, op: F) -> Vwhere
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);
Sourcepub fn from_these(self, t_default: T, u_default: U) -> (T, U)
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));
Sourcepub fn swap_these(self) -> These<U, T>
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"));
Sourcepub fn this(self) -> Option<T>
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);
Sourcepub fn that(self) -> Option<U>
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);
Sourcepub fn these(self) -> Option<(T, U)>
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)));
Sourcepub fn here(self) -> Option<T>
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"));
Sourcepub fn there(self) -> Option<U>
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));
Sourcepub fn is_here(&self) -> bool
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);
Sourcepub fn is_there(&self) -> bool
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);
Sourcepub fn partition_these(xs: Vec<These<T, U>>) -> (Vec<T>, Vec<U>, Vec<(T, U)>)
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 Vec
s, 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 from_option(opt: Option<T>, default: U) -> Self
Sourcepub fn combine<F, G>(self, other: Self, f: F, g: G) -> Self
pub fn combine<F, G>(self, other: Self, f: F, g: G) -> Self
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.