#[cfg(not(feature = "send"))]
pub type TArc<T> = std::rc::Rc<T>;
#[cfg(feature = "send")]
pub type TArc<T> = std::sync::Arc<T>;
#[repr(transparent)]
#[derive(Debug)]
pub struct TCellU32 {
#[cfg(not(feature = "send"))]
inner: std::cell::Cell<u32>,
#[cfg(feature = "send")]
inner: std::sync::atomic::AtomicU32,
}
impl TCellU32 {
#[inline]
pub const fn new(v: u32) -> Self {
#[cfg(not(feature = "send"))]
{
Self {
inner: std::cell::Cell::new(v),
}
}
#[cfg(feature = "send")]
{
Self {
inner: std::sync::atomic::AtomicU32::new(v),
}
}
}
#[inline]
pub fn get(&self) -> u32 {
#[cfg(not(feature = "send"))]
{
self.inner.get()
}
#[cfg(feature = "send")]
{
self.inner.load(std::sync::atomic::Ordering::Relaxed)
}
}
#[inline]
pub fn set(&self, v: u32) {
#[cfg(not(feature = "send"))]
{
self.inner.set(v);
}
#[cfg(feature = "send")]
{
self.inner.store(v, std::sync::atomic::Ordering::Relaxed);
}
}
}
impl Clone for TCellU32 {
fn clone(&self) -> Self {
Self::new(self.get())
}
}
impl Default for TCellU32 {
fn default() -> Self {
Self::new(0)
}
}
#[repr(transparent)]
#[derive(Debug)]
pub struct TCellBool {
#[cfg(not(feature = "send"))]
inner: std::cell::Cell<bool>,
#[cfg(feature = "send")]
inner: std::sync::atomic::AtomicBool,
}
impl TCellBool {
#[inline]
pub const fn new(v: bool) -> Self {
#[cfg(not(feature = "send"))]
{
Self {
inner: std::cell::Cell::new(v),
}
}
#[cfg(feature = "send")]
{
Self {
inner: std::sync::atomic::AtomicBool::new(v),
}
}
}
#[inline]
pub fn get(&self) -> bool {
#[cfg(not(feature = "send"))]
{
self.inner.get()
}
#[cfg(feature = "send")]
{
self.inner.load(std::sync::atomic::Ordering::Relaxed)
}
}
#[inline]
pub fn set(&self, v: bool) {
#[cfg(not(feature = "send"))]
{
self.inner.set(v);
}
#[cfg(feature = "send")]
{
self.inner.store(v, std::sync::atomic::Ordering::Relaxed);
}
}
}
#[repr(transparent)]
pub struct TCellPtr {
#[cfg(not(feature = "send"))]
inner: std::cell::Cell<*const u8>,
#[cfg(feature = "send")]
inner: std::sync::atomic::AtomicPtr<u8>,
}
impl std::fmt::Debug for TCellPtr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TCellPtr")
.field("ptr", &self.get())
.finish()
}
}
impl TCellPtr {
#[inline]
pub const fn null() -> Self {
#[cfg(not(feature = "send"))]
{
Self {
inner: std::cell::Cell::new(std::ptr::null()),
}
}
#[cfg(feature = "send")]
{
Self {
inner: std::sync::atomic::AtomicPtr::new(std::ptr::null_mut()),
}
}
}
#[inline]
pub fn new(p: *const u8) -> Self {
#[cfg(not(feature = "send"))]
{
Self {
inner: std::cell::Cell::new(p),
}
}
#[cfg(feature = "send")]
{
Self {
inner: std::sync::atomic::AtomicPtr::new(p as *mut u8),
}
}
}
#[inline]
pub fn get(&self) -> *const u8 {
#[cfg(not(feature = "send"))]
{
self.inner.get()
}
#[cfg(feature = "send")]
{
self.inner.load(std::sync::atomic::Ordering::Relaxed)
}
}
#[inline]
pub fn set(&self, p: *const u8) {
#[cfg(not(feature = "send"))]
{
self.inner.set(p);
}
#[cfg(feature = "send")]
{
self.inner
.store(p as *mut u8, std::sync::atomic::Ordering::Relaxed);
}
}
#[inline]
pub fn cell_addr(&self) -> *const () {
self as *const _ as *const ()
}
}
impl Clone for TCellPtr {
fn clone(&self) -> Self {
Self::new(self.get())
}
}
#[repr(transparent)]
#[derive(Debug)]
pub struct TRefLock<T: ?Sized> {
#[cfg(not(feature = "send"))]
inner: std::cell::RefCell<T>,
#[cfg(feature = "send")]
inner: std::sync::RwLock<T>,
}
impl<T> TRefLock<T> {
#[inline]
pub const fn new(v: T) -> Self {
#[cfg(not(feature = "send"))]
{
Self {
inner: std::cell::RefCell::new(v),
}
}
#[cfg(feature = "send")]
{
Self {
inner: std::sync::RwLock::new(v),
}
}
}
#[cfg(not(feature = "send"))]
#[inline]
pub fn borrow(&self) -> std::cell::Ref<'_, T> {
self.inner.borrow()
}
#[cfg(feature = "send")]
#[inline]
pub fn borrow(&self) -> std::sync::RwLockReadGuard<'_, T> {
self.inner.read().unwrap()
}
#[cfg(not(feature = "send"))]
#[inline]
pub fn borrow_mut(&self) -> std::cell::RefMut<'_, T> {
self.inner.borrow_mut()
}
#[cfg(feature = "send")]
#[inline]
pub fn borrow_mut(&self) -> std::sync::RwLockWriteGuard<'_, T> {
self.inner.write().unwrap()
}
}