#[cfg(not(feature = "sync-mutex"))]
use core::cell::RefCell;
#[cfg(not(feature = "sync-mutex"))]
#[derive(Debug, Default)]
pub struct Shared<T>(RefCell<T>);
#[cfg(feature = "sync-mutex")]
#[derive(Debug, Default)]
pub struct Shared<T>(std::sync::Mutex<T>);
#[cfg(not(feature = "sync-mutex"))]
pub type Ref<'a, T> = core::cell::Ref<'a, T>;
#[cfg(feature = "sync-mutex")]
pub type Ref<'a, T> = std::sync::MutexGuard<'a, T>;
#[cfg(not(feature = "sync-mutex"))]
pub type RefMut<'a, T> = core::cell::RefMut<'a, T>;
#[cfg(feature = "sync-mutex")]
pub type RefMut<'a, T> = std::sync::MutexGuard<'a, T>;
impl<T> Shared<T> {
#[must_use]
pub const fn new(value: T) -> Self {
#[cfg(not(feature = "sync-mutex"))]
{
Self(RefCell::new(value))
}
#[cfg(feature = "sync-mutex")]
{
Self(std::sync::Mutex::new(value))
}
}
pub fn borrow(&self) -> Ref<'_, T> {
#[cfg(not(feature = "sync-mutex"))]
{
self.0.borrow()
}
#[cfg(feature = "sync-mutex")]
{
self.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
pub fn borrow_mut(&self) -> RefMut<'_, T> {
#[cfg(not(feature = "sync-mutex"))]
{
self.0.borrow_mut()
}
#[cfg(feature = "sync-mutex")]
{
self.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
pub fn into_inner(self) -> T {
#[cfg(not(feature = "sync-mutex"))]
{
self.0.into_inner()
}
#[cfg(feature = "sync-mutex")]
{
self.0
.into_inner()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
}
#[cfg(all(feature = "sync-mutex", feature = "rustcrypto"))]
const _: () = {
const fn assert_send<T: Send>() {}
const fn assert_sync<T: Sync>() {}
assert_send::<crate::commissioning::commissioner::Commissioner>();
assert_send::<crate::ca::CertAuthority>();
assert_sync::<crate::ca::CertAuthority>();
assert_send::<crate::im::client::Subscription>();
assert_sync::<crate::im::client::Subscription>();
assert_send::<crate::im::client::ReportAssembler<1024>>();
assert_send::<Shared<u32>>();
assert_sync::<Shared<u32>>();
};