39 lines
858 B
Rust
39 lines
858 B
Rust
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||
|
#[serde(untagged)]
|
||
|
pub enum Either<L, R> {
|
||
|
Left(L),
|
||
|
Right(R),
|
||
|
}
|
||
|
|
||
|
impl<L, R> Either<L, R> {
|
||
|
pub fn left(self) -> Option<L> {
|
||
|
if let Either::Left(l) = self {
|
||
|
Some(l)
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn right(self) -> Option<R> {
|
||
|
if let Either::Right(r) = self {
|
||
|
Some(r)
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn as_ref(&self) -> Either<&L, &R> {
|
||
|
match self {
|
||
|
Either::Left(ref l) => Either::Left(l),
|
||
|
Either::Right(ref r) => Either::Right(r),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn as_mut(&mut self) -> Either<&mut L, &mut R> {
|
||
|
match self {
|
||
|
Either::Left(ref mut l) => Either::Left(l),
|
||
|
Either::Right(ref mut r) => Either::Right(r),
|
||
|
}
|
||
|
}
|
||
|
}
|