Enum Zot

Source
pub enum Zot<T> {
    Zero,
    One(T),
    Two(T, T),
}
Expand description

A collection of exactly zero, one, or two T elements.

Variants§

§

Zero

No value

§

One(T)

One value

§

Two(T, T)

Two values

Implementations§

Source§

impl<T> Zot<T>

Source

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

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

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

pub fn contains<U>(&self, v: &U) -> bool
where 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);
Source

pub const fn as_ref(&self) -> Zot<&T>

Converts from &Zot<T> to Zot<&T>.

Source

pub fn as_mut(&mut self) -> Zot<&mut T>

Converts from &mut Zot<T> to Zot<&mut T>.

Source

pub fn as_pin_ref(self: Pin<&Self>) -> Zot<Pin<&T>>

Converts from [Pin]<&Zot<T>> to Zot<[Pin]<&T>>.

Source

pub fn as_pin_mut(self: Pin<&mut Self>) -> Zot<Pin<&mut T>>

Converts from [Pin]<&mut Zot<T>> to Zot<[Pin]<&mut T>>.

Source

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`
Source

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`
Source

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`
Source

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`
Source

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
Source

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
Source

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
Source

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
Source

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

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

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

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

pub fn first(&self) -> Option<&T>

Source

pub fn first_mut(&mut self) -> Option<&mut T>

Source

pub fn second(&self) -> Option<&T>

Source

pub fn second_mut(&mut self) -> Option<&mut T>

Source

pub fn last(&self) -> Option<&T>

Source

pub fn last_mut(&mut self) -> Option<&mut T>

Source

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.

Source

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

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

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

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

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

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

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

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

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

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

pub fn from_options(a: Option<T>, b: Option<T>) -> Self

Construct a Zot from two Options.

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

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

pub fn without_first(&self) -> Self
where T: Clone,

Source

pub fn without_second(&self) -> Self
where T: Clone,

Source

pub fn remove_second(self) -> Self
where T: Clone,

Trait Implementations§

Source§

impl<T: Clone> Clone for Zot<T>

Source§

fn clone(&self) -> Zot<T>

Returns a duplicate 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> Debug for Zot<T>

Source§

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

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

impl<T> Default for Zot<T>

Source§

fn default() -> Self

Returns [Zero][zot::Zot::Zero].

§Examples
let z: zot::Zot<u32> = zot::Zot::default();
assert!(z.is_zero());
Source§

impl<'a, T> From<&'a Zot<T>> for Zot<&'a T>

Source§

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>

Source§

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<(Option<T>, Option<T>)> for Zot<T>

Source§

fn from(ts: (Option<T>, Option<T>)) -> Self

Creates a Zot from two Options.

§Examples
let x = zot::Zot::from((None, Some(2)));
assert_eq!(x, zot::Zot::One(2));

let x = zot::Zot::from((Some(3), Some(4)));
assert_eq!(x, zot::Zot::Two(3, 4));
Source§

impl<T> From<(Option<T>, T)> for Zot<T>

Source§

fn from(ts: (Option<T>, T)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<(T, Option<T>)> for Zot<T>

Source§

fn from(ts: (T, Option<T>)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<(T, T)> for Zot<T>

Source§

fn from(ts: (T, T)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Option<(T, Option<T>)>> for Zot<T>

Source§

fn from(ts: Option<(T, Option<T>)>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Option<T>> for Zot<T>

Source§

fn from(t: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Ot<T>> for Zot<T>

Source§

fn from(ot: Ot<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Zot<T>

Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Zot<T>> for (Option<T>, Option<T>)

Source§

fn from(zot: Zot<T>) -> Self

Convert a Zot into a tuple of Options. 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<T> From<Zot<T>> for Option<(T, Option<T>)>

Source§

fn from(zot: Zot<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for Zot<T>

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<'a, T> IntoIterator for &'a Zot<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Zot<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for Zot<T>

Source§

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

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

impl<T: Ord> Ord for Zot<T>

Source§

fn cmp(&self, other: &Zot<T>) -> 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> PartialEq for Zot<T>

Source§

fn eq(&self, other: &Zot<T>) -> 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> PartialOrd for Zot<T>

Source§

fn partial_cmp(&self, other: &Zot<T>) -> 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> Copy for Zot<T>

Source§

impl<T: Eq> Eq for Zot<T>

Source§

impl<T> StructuralPartialEq for Zot<T>

Auto Trait Implementations§

§

impl<T> Freeze for Zot<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Zot<T>
where T: RefUnwindSafe,

§

impl<T> Send for Zot<T>
where T: Send,

§

impl<T> Sync for Zot<T>
where T: Sync,

§

impl<T> Unpin for Zot<T>
where T: Unpin,

§

impl<T> UnwindSafe for Zot<T>
where T: 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.