use orx_concurrent_option::ConcurrentOption;
pub struct ConcurrentElement<T>(pub(crate) ConcurrentOption<T>);
impl<T> From<ConcurrentOption<T>> for ConcurrentElement<T> {
fn from(value: ConcurrentOption<T>) -> Self {
Self(value)
}
}
impl<T> ConcurrentElement<T> {
#[inline(always)]
#[allow(clippy::missing_panics_doc)]
pub fn cloned(&self) -> T
where
T: Clone,
{
self.0.clone_into_option().expect(HAS_VALUE)
}
#[inline(always)]
#[allow(clippy::missing_panics_doc)]
pub fn copied(&self) -> T
where
T: Copy,
{
self.0.clone_into_option().expect(HAS_VALUE)
}
#[inline(always)]
#[allow(clippy::missing_panics_doc)]
pub fn map<F, U>(&self, f: F) -> U
where
F: FnOnce(&T) -> U,
{
self.0.map(f).expect(HAS_VALUE)
}
#[inline(always)]
#[allow(clippy::missing_panics_doc)]
pub fn replace(&self, value: T) -> T {
self.0.replace(value).expect(HAS_VALUE)
}
#[inline(always)]
#[allow(clippy::missing_panics_doc)]
pub fn set(&self, value: T) {
assert!(self.0.set_some(value), "Failed to set the element");
}
#[inline(always)]
#[allow(clippy::missing_panics_doc)]
pub fn update<F>(&self, f: F)
where
F: FnMut(&mut T),
{
assert!(self.0.update_if_some(f), "Failed to update the element");
}
}
const HAS_VALUE: &str = "ConcurrentElement must always have a value";