use std::error::Error;
use std::fmt;
use std::ops::Deref;
use std::ops::DerefMut;
mod futures_stream;
mod std_future;
mod std_io;
mod std_iter;
mod std_pin;
mod tokio_io;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Either<L, R> {
Left(L),
Right(R),
}
impl<L, R> Either<L, R> {
pub const fn as_ref(&self) -> Either<&L, &R> {
match self {
Either::Left(x) => Either::Left(x),
Either::Right(x) => Either::Right(x),
}
}
pub const fn as_mut(&mut self) -> Either<&mut L, &mut R> {
match self {
Either::Left(x) => Either::Left(x),
Either::Right(x) => Either::Right(x),
}
}
pub fn flip(self) -> Either<R, L> {
match self {
Either::Left(x) => Either::Right(x),
Either::Right(x) => Either::Left(x),
}
}
pub const fn is_left(&self) -> bool {
matches!(self, Self::Left(_))
}
pub fn is_left_and(&self, check: impl FnOnce(&L) -> bool) -> bool {
matches!(self, Self::Left(x) if check(x))
}
pub fn is_left_or(&self, check: impl FnOnce(&R) -> bool) -> bool {
matches!(self, Self::Left(_)) || self.is_right_and(check)
}
pub const fn as_left(&self) -> Option<&L> {
match self {
Either::Left(x) => Some(x),
Either::Right(_) => None,
}
}
pub const fn as_left_mut(&mut self) -> Option<&mut L> {
match self {
Either::Left(x) => Some(x),
Either::Right(_) => None,
}
}
pub fn left(self) -> Option<L> {
match self {
Either::Left(x) => Some(x),
Either::Right(_) => None,
}
}
pub fn left_ok(self) -> Result<L, R> {
match self {
Either::Left(x) => Ok(x),
Either::Right(x) => Err(x),
}
}
pub fn unwrap_left(self) -> L
where
R: fmt::Debug,
{
match self {
Either::Left(x) => x,
Either::Right(x) => panic!("Called `Either::unwrap_left` on a `Right` value: {x:?}"),
}
}
pub const fn is_right(&self) -> bool {
matches!(self, Self::Right(_))
}
pub fn is_right_and(&self, check: impl FnOnce(&R) -> bool) -> bool {
matches!(self, Self::Right(x) if check(x))
}
pub fn is_right_or(&self, check: impl FnOnce(&L) -> bool) -> bool {
matches!(self, Self::Right(_)) || self.is_left_and(check)
}
pub const fn as_right(&self) -> Option<&R> {
match self {
Either::Left(_) => None,
Either::Right(x) => Some(x),
}
}
pub const fn as_right_mut(&mut self) -> Option<&mut R> {
match self {
Either::Left(_) => None,
Either::Right(x) => Some(x),
}
}
pub fn right(self) -> Option<R> {
match self {
Either::Left(_) => None,
Either::Right(x) => Some(x),
}
}
pub fn right_ok(self) -> Result<R, L> {
match self {
Either::Left(x) => Err(x),
Either::Right(x) => Ok(x),
}
}
pub fn unwrap_right(self) -> R
where
L: fmt::Debug,
{
match self {
Either::Left(x) => panic!("Called `Either::unwrap_right` on a `Left` value: {x:?}"),
Either::Right(x) => x,
}
}
}
impl<T> Either<T, T> {
pub fn into_inner(self) -> T {
match self {
Either::Left(x) => x,
Either::Right(x) => x,
}
}
}
impl<L, R> Either<&L, &R> {
pub fn cloned(self) -> Either<L, R>
where
L: Clone,
R: Clone,
{
match self {
Either::Left(x) => Either::Left(x.clone()),
Either::Right(x) => Either::Right(x.clone()),
}
}
pub fn copied(self) -> Either<L, R>
where
L: Copy,
R: Copy,
{
match self {
Either::Left(x) => Either::Left(*x),
Either::Right(x) => Either::Right(*x),
}
}
}
impl<L, R> Either<&mut L, &mut R> {
pub fn cloned(self) -> Either<L, R>
where
L: Clone,
R: Clone,
{
match self {
Either::Left(x) => Either::Left(x.clone()),
Either::Right(x) => Either::Right(x.clone()),
}
}
pub fn copied(self) -> Either<L, R>
where
L: Copy,
R: Copy,
{
match self {
Either::Left(x) => Either::Left(*x),
Either::Right(x) => Either::Right(*x),
}
}
}
impl<L, R> fmt::Display for Either<L, R>
where
L: fmt::Display,
R: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Either::Left(x) => x.fmt(f),
Either::Right(x) => x.fmt(f),
}
}
}
impl<L, R> Error for Either<L, R>
where
L: Error + 'static,
R: Error + 'static,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Either::Left(x) => Some(x),
Either::Right(x) => Some(x),
}
}
}
impl<L, R, T> AsRef<T> for Either<L, R>
where
L: AsRef<T>,
R: AsRef<T>,
{
fn as_ref(&self) -> &T {
match self {
Either::Left(x) => x.as_ref(),
Either::Right(x) => x.as_ref(),
}
}
}
impl<L, R, T> AsMut<T> for Either<L, R>
where
L: AsMut<T>,
R: AsMut<T>,
{
fn as_mut(&mut self) -> &mut T {
match self {
Either::Left(x) => x.as_mut(),
Either::Right(x) => x.as_mut(),
}
}
}
impl<L, R, T> Deref for Either<L, R>
where
L: Deref<Target = T>,
R: Deref<Target = T>,
{
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
Either::Left(x) => x.deref(),
Either::Right(x) => x.deref(),
}
}
}
impl<L, R, T> DerefMut for Either<L, R>
where
L: DerefMut<Target = T>,
R: DerefMut<Target = T>,
{
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Either::Left(x) => x.deref_mut(),
Either::Right(x) => x.deref_mut(),
}
}
}