#![allow(clippy::missing_const_for_fn)]
use crate::Error;
#[must_use]
pub enum TransactionOutput<T> {
Committed(T),
Aborted(Error),
}
impl<T> TransactionOutput<T> {
#[inline]
pub const fn is_committed(&self) -> bool {
matches!(self, Self::Committed(_))
}
#[inline]
pub fn is_aborted(&self) -> bool {
!self.is_committed()
}
#[inline]
pub fn unwrap(self) -> T {
match self {
Self::Committed(v) => v,
Self::Aborted(err) => panic!(
"called `TransactionOuput::unwrap()` on an `Aborted` value {}",
err
),
}
}
#[inline]
pub fn ok(self) -> Option<T> {
match self {
Self::Committed(v) => Some(v),
Self::Aborted(_) => None,
}
}
#[inline]
pub fn err(self) -> Option<Error> {
match self {
Self::Committed(_) => None,
Self::Aborted(err) => Some(err),
}
}
#[inline]
pub fn expect(self, msg: &str) -> T {
match self {
Self::Committed(v) => v,
Self::Aborted(e) => panic!("{}: {:?}", msg, e),
}
}
}
impl<T> From<TransactionOutput<T>> for Result<T, Error> {
fn from(output: TransactionOutput<T>) -> Self {
match output {
TransactionOutput::Committed(v) => Ok(v),
TransactionOutput::Aborted(err) => Err(err),
}
}
}