use std::sync::Arc;
use crate::event::CasContext;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CasSuccess<T, R> {
Updated {
previous: Arc<T>,
current: Arc<T>,
output: R,
context: CasContext,
},
Finished {
current: Arc<T>,
output: R,
context: CasContext,
},
}
impl<T, R> CasSuccess<T, R> {
#[inline]
pub(crate) fn updated(
previous: Arc<T>,
current: Arc<T>,
output: R,
context: CasContext,
) -> Self {
Self::Updated {
previous,
current,
output,
context,
}
}
#[inline]
pub(crate) fn finished(current: Arc<T>, output: R, context: CasContext) -> Self {
Self::Finished {
current,
output,
context,
}
}
#[inline]
pub fn is_updated(&self) -> bool {
matches!(self, Self::Updated { .. })
}
#[inline]
pub fn previous(&self) -> Option<&Arc<T>> {
match self {
Self::Updated { previous, .. } => Some(previous),
Self::Finished { .. } => None,
}
}
#[inline]
pub fn current(&self) -> &Arc<T> {
match self {
Self::Updated { current, .. } | Self::Finished { current, .. } => current,
}
}
#[inline]
pub fn output(&self) -> &R {
match self {
Self::Updated { output, .. } | Self::Finished { output, .. } => output,
}
}
#[inline]
pub fn into_output(self) -> R {
match self {
Self::Updated { output, .. } | Self::Finished { output, .. } => output,
}
}
#[inline]
pub fn context(&self) -> CasContext {
match self {
Self::Updated { context, .. } | Self::Finished { context, .. } => *context,
}
}
#[inline]
pub fn attempts(&self) -> u32 {
self.context().attempt()
}
}