pub enum Zot<T> {
Zero,
One(T),
Two(T, T),
}
Expand description
A collection of exactly zero, one, or two T
elements.
Variants§
Implementations§
Source§impl<T> Zot<T>
impl<T> Zot<T>
Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true
if the Zot
is a [Zero
] value.
§Examples
let x: zot::Zot<u32>= zot::Zot::Zero;
assert_eq!(x.is_zero(), true);
let x: zot::Zot<u32>= zot::Zot::Two(2, 3);
assert_eq!(x.is_zero(), false);
Sourcepub const fn is_one(&self) -> bool
pub const fn is_one(&self) -> bool
Returns true
if the Zot
is a [One
] value.
§Examples
let x: zot::Zot<u32>= zot::Zot::One(2);
assert_eq!(x.is_one(), true);
let x: zot::Zot<u32>= zot::Zot::Two(2, 3);
assert_eq!(x.is_one(), false);
Sourcepub const fn is_two(&self) -> bool
pub const fn is_two(&self) -> bool
Returns true
if the Zot
is a [Two
] value.
§Examples
let x: zot::Zot<u32>= zot::Zot::Two(2, 3);
assert_eq!(x.is_two(), true);
let x: zot::Zot<u32>= zot::Zot::One(2);
assert_eq!(x.is_two(), false);
Sourcepub fn contains<U>(&self, v: &U) -> boolwhere
U: PartialEq<T>,
pub fn contains<U>(&self, v: &U) -> boolwhere
U: PartialEq<T>,
Returns true
if at least one contained value equals the given value.
§Examples
let x: zot::Zot<u32>= zot::Zot::One(2);
assert_eq!(x.contains(&2), true);
let x: zot::Zot<u32>= zot::Zot::One(3);
assert_eq!(x.contains(&2), false);
let x: zot::Zot<u32>= zot::Zot::Two(3, 2);
assert_eq!(x.contains(&2), true);
let x: zot::Zot<u32>= zot::Zot::Zero;
assert_eq!(x.contains(&2), false);
Sourcepub fn as_pin_ref(self: Pin<&Self>) -> Zot<Pin<&T>>
pub fn as_pin_ref(self: Pin<&Self>) -> Zot<Pin<&T>>
Converts from [Pin
]<&Zot<T>>
to Zot<
[Pin
]<&T>>
.
Sourcepub fn as_pin_mut(self: Pin<&mut Self>) -> Zot<Pin<&mut T>>
pub fn as_pin_mut(self: Pin<&mut Self>) -> Zot<Pin<&mut T>>
Converts from [Pin
]<&mut Zot<T>>
to Zot<
[Pin
]<&mut T>>
.
Sourcepub fn expect_zero(self, msg: &str)
pub fn expect_zero(self, msg: &str)
Returns the contained [Zero
] value, consuming the self
value.
§Panics
Panics if the value is not [Zero
] with a custom panic message provided by
msg
.
§Examples
let x: zot::Zot<&str>= zot::Zot::Zero;
assert_eq!(x.expect_zero("fruits are healthy"), ());
let x= zot::Zot::One("value");
x.expect_zero("fruits are healthy"); // panics with `fruits are healthy`
Sourcepub fn expect_one(self, msg: &str) -> T
pub fn expect_one(self, msg: &str) -> T
Returns the contained [One
] value, consuming the self
value.
§Panics
Panics if the value is not [One
] with a custom panic message provided by
msg
.
§Examples
let x= zot::Zot::One("value");
assert_eq!(x.expect_one("fruits are healthy"), "value");
let x= zot::Zot::Two("value", "value");
x.expect_one("fruits are healthy"); // panics with `fruits are healthy`
Sourcepub fn expect_two(self, msg: &str) -> (T, T)
pub fn expect_two(self, msg: &str) -> (T, T)
Returns the contained [Two
] values as a (T, T)
, consuming the self
value.
§Panics
Panics if the value is not [Two
] with a custom panic message provided by
msg
.
§Examples
let x= zot::Zot::Two("value", "value");
assert_eq!(x.expect_two("fruits are healthy"), ("value", "value"));
let x= zot::Zot::One("value");
x.expect_two("fruits are healthy"); // panics with `fruits are healthy`
Sourcepub fn expect_option(self, msg: &str) -> Option<T>
pub fn expect_option(self, msg: &str) -> Option<T>
Returns the contained [Zero
] or [One
] value as an Option<T>
, consuming the self
value.
§Panics
Panics if the value is [Two
] with a custom panic message provided by
msg
.
§Examples
let x= zot::Zot::One("value");
assert_eq!(x.expect_option("fruits are healthy"), Some("value"));
let x= zot::Zot::Two("value", "value");
x.expect_option("fruits are healthy"); // panics with `fruits are healthy`
Sourcepub fn unwrap_zero(self)
pub fn unwrap_zero(self)
Returns the contained [Zero
] value, consuming the self
value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the [Zero
]
case explicitly, or call unwrap_zero_or
, unwrap_zero_or_else
.
§Panics
Panics if the self value is not [Zero
].
§Examples
let x: zot::Zot<&str>= zot::Zot::Zero;
assert_eq!(x.unwrap_zero(), ());
let x= zot::Zot::One("air");
assert_eq!(x.unwrap_zero(), ()); // fails
Sourcepub fn unwrap_one(self) -> T
pub fn unwrap_one(self) -> T
Returns the contained [One
] value, consuming the self
value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the [One
]
case explicitly, or call unwrap_one_or
, unwrap_one_or_else
.
§Panics
Panics if the self value is not [One
].
§Examples
let x= zot::Zot::One("air");
assert_eq!(x.unwrap_one(), "air");
let x= zot::Zot::Two("air", "wind");
assert_eq!(x.unwrap_one(), "air"); // fails
Sourcepub fn unwrap_two(self) -> (T, T)
pub fn unwrap_two(self) -> (T, T)
Returns the contained [Two
] value as a (T, T)
, consuming the self
value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the [Two
]
case explicitly, or call unwrap_two_or
, unwrap_two_or_else
.
§Panics
Panics if the self value is not [Two
].
§Examples
let x= zot::Zot::Two("air", "wind");
assert_eq!(x.unwrap_two(), ("air", "wind"));
let x= zot::Zot::One("air");
assert_eq!(x.unwrap_two(), ("air", "wind")); // fails
Sourcepub fn unwrap_option(self) -> Option<T>
pub fn unwrap_option(self) -> Option<T>
Returns the contained [Zero
] or [One
] value as an Option<T>
, consuming the self
value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the [Zero
]
and [One'] cases explicitly, or call [
unwrap_option_or], [
unwrap_option_or_else`].
§Panics
Panics if the self value is not [Zero
] or [One
].
§Examples
let x= zot::Zot::One("air");
assert_eq!(x.unwrap_option(), Some("air"));
let x= zot::Zot::Two("air", "wind");
assert_eq!(x.unwrap_option(), Some("air")); // fails
Sourcepub fn unwrap_one_or(self, default: T) -> T
pub fn unwrap_one_or(self, default: T) -> T
Returns the contained [One
] value or a provided default.
Arguments passed to unwrap_one_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use unwrap_one_or_else
,
which is lazily evaluated.
§Examples
assert_eq!(zot::Zot::One("car").unwrap_one_or("bike"), "car");
assert_eq!(zot::Zot::Zero.unwrap_one_or("bike"), "bike");
Sourcepub fn unwrap_two_or(self, default: (T, T)) -> (T, T)
pub fn unwrap_two_or(self, default: (T, T)) -> (T, T)
Returns the contained [Two
] value as a (T, T)
or a provided default.
Arguments passed to unwrap_two_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use unwrap_two_or_else
,
which is lazily evaluated.
§Examples
assert_eq!(zot::Zot::Two("car", "truck").unwrap_two_or(("bike", "pogo")), ("car", "truck"));
assert_eq!(zot::Zot::Zero.unwrap_two_or(("bike", "pogo")), ("bike", "pogo"));
Sourcepub fn unwrap_one_or_else<F: FnOnce() -> T>(self, f: F) -> T
pub fn unwrap_one_or_else<F: FnOnce() -> T>(self, f: F) -> T
Returns the contained [One
] value or computes it from a closure.
§Examples
let k = 10;
assert_eq!(zot::Zot::One(4).unwrap_one_or_else(|| 2 * k), 4);
assert_eq!(zot::Zot::Zero.unwrap_one_or_else(|| 2 * k), 20);
Sourcepub fn unwrap_two_or_else<F: FnOnce() -> (T, T)>(self, f: F) -> (T, T)
pub fn unwrap_two_or_else<F: FnOnce() -> (T, T)>(self, f: F) -> (T, T)
Returns the contained [Two
] value or computes it from a closure.
§Examples
let k = 10;
assert_eq!(zot::Zot::Two(4, 5).unwrap_two_or_else(|| (2 * k, 3 * k)), (4, 5));
assert_eq!(zot::Zot::Zero.unwrap_two_or_else(|| (2 * k, 3 * k)), (20, 30));
pub fn first(&self) -> Option<&T>
pub fn first_mut(&mut self) -> Option<&mut T>
pub fn second(&self) -> Option<&T>
pub fn second_mut(&mut self) -> Option<&mut T>
pub fn last(&self) -> Option<&T>
pub fn last_mut(&mut self) -> Option<&mut T>
Sourcepub fn map<U, F: FnMut(T) -> U>(self, f: F) -> Zot<U>
pub fn map<U, F: FnMut(T) -> U>(self, f: F) -> Zot<U>
Maps a Zot<T>
to Zot<U>
by applying a function to a contained values.
Sourcepub const fn iter(&self) -> Iter<'_, T>
pub const fn iter(&self) -> Iter<'_, T>
Returns an iterator over the contained values.
§Examples
let x= zot::Zot::Two(4, 5);
let mut iter = x.iter();
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&5));
let x= zot::Zot::One(4);
let mut iter = x.iter();
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), None);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns a mutable iterator over the contained values.
§Examples
let mut x= zot::Zot::One(4);
match x.iter_mut().next() {
Some(v) => *v = 42,
None => {},
}
assert_eq!(x, zot::Zot::One(42));
let mut x: zot::Zot<u32>= zot::Zot::Zero;
assert_eq!(x.iter_mut().next(), None);
Sourcepub fn take(&mut self) -> Zot<T>
pub fn take(&mut self) -> Zot<T>
Takes the value out of the Zot
, leaving a [Zero
] in its place.
§Examples
let mut x = zot::Zot::One(2);
let y = x.take();
assert_eq!(x, zot::Zot::Zero);
assert_eq!(y, zot::Zot::One(2));
let mut x: zot::Zot<u32> = zot::Zot::Zero;
let y = x.take();
assert_eq!(x, zot::Zot::Zero);
assert_eq!(y, zot::Zot::Zero);
Sourcepub fn take_first(&mut self) -> Option<T>
pub fn take_first(&mut self) -> Option<T>
Takes the first value out of the Zot
, leaving only [One
] if
there were initially two values, otherwise [Zero
].
§Examples
let mut x = zot::Zot::Two(2, 3);
let y = x.take_first();
assert_eq!(x, zot::Zot::One(3));
assert_eq!(y, Some(2));
let mut x: zot::Zot<u32> = zot::Zot::Zero;
let y = x.take_first();
assert_eq!(x, zot::Zot::Zero);
assert_eq!(y, None);
Sourcepub fn take_second(&mut self) -> Option<T>
pub fn take_second(&mut self) -> Option<T>
Takes the second value out of the Zot
if it is [Two
], leaving
only [One
], otherwise the Zot
is unchanged.
§Examples
let mut x = zot::Zot::Two(2, 3);
let y = x.take_second();
assert_eq!(x, zot::Zot::One(2));
assert_eq!(y, Some(3));
let mut x: zot::Zot<u32> = zot::Zot::One(2);
let y = x.take_second();
assert_eq!(x, zot::Zot::One(2));
assert_eq!(y, None);
Sourcepub fn take_last(&mut self) -> Option<T>
pub fn take_last(&mut self) -> Option<T>
Takes the last value out of the Zot
, leaving only [One
] if
there were initially two values, otherwise [Zero
].
§Examples
let mut x = zot::Zot::Two(2, 3);
let y = x.take_last();
assert_eq!(x, zot::Zot::One(2));
assert_eq!(y, Some(3));
let mut x: zot::Zot<u32> = zot::Zot::Zero;
let y = x.take_last();
assert_eq!(x, zot::Zot::Zero);
assert_eq!(y, None);
Sourcepub fn replace_one(&mut self, value: T) -> Zot<T>
pub fn replace_one(&mut self, value: T) -> Zot<T>
Replaces the actual value in the Zot
by the value given in parameter,
returning the old value,
leaving a [One
] in its place without deinitializing either one.
§Examples
let mut x = zot::Zot::One(2);
let old = x.replace_one(5);
assert_eq!(x, zot::Zot::One(5));
assert_eq!(old, zot::Zot::One(2));
let mut x = zot::Zot::Zero;
let old = x.replace_one(3);
assert_eq!(x, zot::Zot::One(3));
assert_eq!(old, zot::Zot::Zero);
Sourcepub fn replace_two(&mut self, first: T, second: T) -> Zot<T>
pub fn replace_two(&mut self, first: T, second: T) -> Zot<T>
Replaces the actual value in the Zot
by the values given in parameter,
returning the old value,
leaving a [Two
] in its place without deinitializing either one.
§Examples
let mut x = zot::Zot::One(2);
let old = x.replace_two(5, 6);
assert_eq!(x, zot::Zot::Two(5, 6));
assert_eq!(old, zot::Zot::One(2));
let mut x = zot::Zot::Zero;
let old = x.replace_two(3, 4);
assert_eq!(x, zot::Zot::Two(3, 4));
assert_eq!(old, zot::Zot::Zero);
Sourcepub fn replace_first(&mut self, value: T) -> Option<T>
pub fn replace_first(&mut self, value: T) -> Option<T>
Replaces the first value in the Zot
by the value given in parameter,
returning the old value if present,
leaving a [One
] or [Two
] in its place without deinitializing either one.
§Examples
let mut x = zot::Zot::Two(2, 3);
let old = x.replace_first(5);
assert_eq!(x, zot::Zot::Two(5, 3));
assert_eq!(old, Some(2));
let mut x = zot::Zot::Zero;
let old = x.replace_first(3);
assert_eq!(x, zot::Zot::One(3));
assert_eq!(old, None);
Sourcepub fn replace_last(&mut self, value: T) -> Option<T>
pub fn replace_last(&mut self, value: T) -> Option<T>
Replaces the last value in the Zot
by the value given in parameter,
returning the old value if present,
leaving a [One
] or [Two
] in its place without deinitializing either one.
§Examples
let mut x = zot::Zot::Two(2, 3);
let old = x.replace_last(5);
assert_eq!(x, zot::Zot::Two(2, 5));
assert_eq!(old, Some(3));
let mut x = zot::Zot::Zero;
let old = x.replace_last(3);
assert_eq!(x, zot::Zot::One(3));
assert_eq!(old, None);
Sourcepub fn from_options(a: Option<T>, b: Option<T>) -> Self
pub fn from_options(a: Option<T>, b: Option<T>) -> Self
Construct a Zot
from two Option
s.
§Examples
let x: zot::Zot<u32>= zot::Zot::from_options(None, Some(3));
assert_eq!(x, zot::Zot::One(3));
let x: zot::Zot<u32>= zot::Zot::from_options(Some(2), Some(3));
assert_eq!(x, zot::Zot::Two(2, 3));
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of contained elements.
§Examples
let x: zot::Zot<u32>= zot::Zot::Two(7, 20);
assert_eq!(x.len(), 2);
pub fn without_first(&self) -> Selfwhere
T: Clone,
pub fn without_second(&self) -> Selfwhere
T: Clone,
pub fn remove_second(self) -> Selfwhere
T: Clone,
Trait Implementations§
Source§impl<'a, T> From<&'a Zot<T>> for Zot<&'a T>
impl<'a, T> From<&'a Zot<T>> for Zot<&'a T>
Source§fn from(o: &'a Zot<T>) -> Zot<&'a T>
fn from(o: &'a Zot<T>) -> Zot<&'a T>
Converts from &Zot<T>
to Zot<&T>
.
§Examples
let s: zot::Zot<String> = zot::Zot::One(String::from("Hello, Rustaceans!"));
let o: zot::Zot<usize> = zot::Zot::from(&s).map(|ss: &String| ss.len());
println!("Can still print s: {:?}", s);
assert_eq!(o, zot::Zot::One(18));
Source§impl<'a, T> From<&'a mut Zot<T>> for Zot<&'a mut T>
impl<'a, T> From<&'a mut Zot<T>> for Zot<&'a mut T>
Source§fn from(o: &'a mut Zot<T>) -> Zot<&'a mut T>
fn from(o: &'a mut Zot<T>) -> Zot<&'a mut T>
Converts from &mut Zot<T>
to Zot<&mut T>
§Examples
let mut s = zot::Zot::One(String::from("Hello"));
let o: zot::Zot<&mut String> = zot::Zot::from(&mut s);
match o {
zot::Zot::One(t) => *t = String::from("Hello, Rustaceans!"),
_ => (),
}
assert_eq!(s, zot::Zot::One(String::from("Hello, Rustaceans!")));
Source§impl<T> From<Zot<T>> for (Option<T>, Option<T>)
impl<T> From<Zot<T>> for (Option<T>, Option<T>)
Source§fn from(zot: Zot<T>) -> Self
fn from(zot: Zot<T>) -> Self
Convert a Zot
into a tuple of Option
s.
If the second element of the tuple is Some
,
the first element will also be Some
.
§Examples
let x: (Option<_>, Option<_>) = zot::Zot::One(2).into();
assert_eq!(x, (Some(2), None));
let x: (Option<_>, Option<_>) = zot::Zot::Two(2, 3).into();
assert_eq!(x, (Some(2), Some(3)));
let x: (Option<i32>, Option<_>) = zot::Zot::Zero.into();
assert_eq!(x, (None, None));
Source§impl<'a, T> IntoIterator for &'a Zot<T>
impl<'a, T> IntoIterator for &'a Zot<T>
Source§impl<'a, T> IntoIterator for &'a mut Zot<T>
impl<'a, T> IntoIterator for &'a mut Zot<T>
Source§impl<T> IntoIterator for Zot<T>
impl<T> IntoIterator for Zot<T>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Returns a consuming iterator any contained values.
§Examples
let x = zot::Zot::Two("string", "text");
let v: Vec<&str> = x.into_iter().collect();
assert_eq!(v, ["string", "text"]);
let x = zot::Zot::Zero;
let v: Vec<&str> = x.into_iter().collect();
assert!(v.is_empty());