quither/lib.rs
1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! "Quad-state Either": Quither. In other words, either-or-neither-or-both type.
16//!
17//! Quither is a type that represents four states:
18//! - `Left(L)`
19//! - `Right(R)`
20//! - `Both(L, R)`
21//! - `Neither`
22//!
23//! This crate also provides an arbitrary combination types of `Either`, `Neither`, and `Both`.
24//! For example, `EitherOrBoth<L, R>` is a type that represents either a left (`Left(L)`) or right (`Right(R)`) or both (`Both(L, R)`).
25//! These types have consistent APIs (as much as possible ☺) so that you can use them interchangeably.
26//!
27//! Each combination types implements the common methods greedily, even if it's not very useful for that type itself.
28//! For example, `EitherOrNeither` type implements `is_both()` method, even if it always returns `false`.
29//!
30
31#![cfg_attr(not(feature = "use_std"), no_std)]
32
33mod and_or_getters;
34mod as_ref;
35mod conv;
36mod factor;
37mod get_or_insert;
38mod getters;
39mod into;
40pub mod iter;
41mod map;
42pub mod result;
43mod std_impls;
44
45// Pair types, essentially comibinations of `Either`, `Neither`, and `Both`.
46
47/// An enum that represents either a left (`Left(L)`) or right (`Right(R)`) value.
48#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
49pub enum Either<L, R> {
50 Left(L),
51 Right(R),
52}
53
54/// An enum that represents a single `Neither` value.
55#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
56pub enum Neither {
57 Neither,
58}
59
60/// An enum that represents a pair of values (`Both(L, R)`).
61#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
62pub enum Both<L, R> {
63 Both(L, R),
64}
65
66/// An enum that represents either a left (`Left(L)`) or right (`Right(R)`) or neither (`Neither`).
67#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
68pub enum EitherOrNeither<L, R> {
69 Neither,
70 Left(L),
71 Right(R),
72}
73
74/// An enum that represents either a left (`Left(L)`) or right (`Right(R)`) or both (`Both(L, R)`).
75#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
76pub enum EitherOrBoth<L, R> {
77 Left(L),
78 Right(R),
79 Both(L, R),
80}
81
82/// An enum that represents a single `Neither` value or a pair of values (`Both(L, R)`).
83#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
84pub enum NeitherOrBoth<L, R> {
85 Neither,
86 Both(L, R),
87}
88
89/// An enum that represents either an empty value, left value, right value, or both values.
90#[derive(Debug, Clone, Copy, Hash, Eq, Ord)]
91pub enum Quither<L, R> {
92 Neither,
93 Left(L),
94 Right(R),
95 Both(L, R),
96}
97
98impl<T> Either<T, T> {
99 pub fn into_inner(self) -> T {
100 match self {
101 Either::Left(l) => l,
102 Either::Right(r) => r,
103 }
104 }
105}
106
107pub use result::ResultExt;