9 releases (breaking)
Uses new Rust 2024
| 0.7.0 | Jun 30, 2025 |
|---|---|
| 0.6.2 | Jun 28, 2025 |
| 0.5.0 | May 17, 2025 |
| 0.4.0 | May 12, 2025 |
| 0.1.0 | May 10, 2025 |
#476 in Rust patterns
626 downloads per month
135KB
2.5K
SLoC
quither
A flexible enum-based utility for representing values that may be on the left, right, neither, or both sides.
Highlights
- Provides a generic enum type supporting
Left,Right,Neither, andBothvariants- Supports arbitrary combinations of
Either,Both, andNeither.
- Supports arbitrary combinations of
- Iterator and standard trait support
- More and clearer iterator types than
itertools'sEitherandEitherOrBothtypes.
- More and clearer iterator types than
- (Supposed to) have compatible interfaces with
itertools'sEitherandEitherOrBothtypes. - Fallible
mapmethods, liketry_map()ortry_map_left(). - Convert between each variant types.
- Expanding conversion like
EithertoEitherOrBothis infallible. - Contracting conversion like
EitherOrBothtoEitheris fallible, where the error type returns the remaining variant type (Bothin this case).
- Expanding conversion like
- No-std compatible, can be build without
stdfeatures. - A bonus feature: Supports the transposition of
Result<impl IntoIterator, E>type intoimpl Iterator<Item = Result<T, E>>type.
Example
use quither::{Quither, NeitherOrBoth, EitherOrBoth};
// You can create values with any combination of variants:
let left: Quither<i32, i32> = Quither::Left(1);
let right: Quither<i32, i32> = Quither::Right(2);
let both: Quither<i32, i32> = Quither::Both(1, 2);
let neither = Neither::Neither;
let left2: EitherOrBoth<i32, i32> = EitherOrBoth::Left(1);
// Pattern matching on Quither
match both {
Quither::Left(l) => println!("Left: {}", l),
Quither::Right(r) => println!("Right: {}", r),
Quither::Both(l, r) => println!("Both: {}, {}", l, r),
Quither::Neither => println!("Neither"),
}
// You can convert the type to a "larger" type
let left2_in_quither: Quither<_, _> = left2.into();
// You can also convert into a "smaller" type with fallible conversion.
// For example, convert a Quither to a type with only Neither and Both variants
let neither_or_both: NeitherOrBoth<_, _> = both.try_into().unwrap();
// Pattern matching works as usual
match neither_or_both {
NeitherOrBoth::Both(l, r) => println!("Both: {}, {}", l, r),
NeitherOrBoth::Neither => println!("Neither"),
}
Crate Features
use_std(default: enabled): Enables implementations for std types (e.g., Read, BufRead)itertools(default: disabled): EnablesIntoimpls from and toitertools::Eitheranditertools::EitherOrBoth.
Dependencies
~180–700KB
~16K SLoC