pi_share/
lib.rs

1#![feature(dispatch_from_dyn)]
2#![feature(coerce_unsized)]
3#![feature(unsize)]
4//! 默认 不带 任何 feature
5//!
6//! ## 1. 几个类型封装
7//!
8//! + `Share` = `Xrc` | `Arc`
9//! + `ShareWeak` = `xrc::Weak` | `sync::Weak`
10//! + `ShareMutex` = `LockCell(RefCell<T>)` | Mutex
11//! + `ShareRwLock` = `LockCell(RefCell<T>)` | `RwLock`
12//! + `ShareCell` = `cell::TrustCell`
13//! + `SharePtr` = `SyncUnsafeCell<T>` | `AtomicPtr<T>`
14//! + `ShareRefCell` = `XrcCell<T>` | `ArcCell<T>`
15//! + `ShareBool` = `SyncUnsafeCell<bool>` | `AtomicBool`
16//! + `ShareU8` = `SyncUnsafeCell<u8>` | `AtomicU8`
17//! + `ShareU32` = `SyncUnsafeCell<u32>` | `AtomicU32`
18//! + `ShareUsize` = `SyncUnsafeCell<usize>` | `AtomicUsize`
19//!
20//! ## 2. 提供 Send, Sync 的 封装
21//!
22//! 目的:wasm 不支持 Send + Sync
23//!
24//! + ThreadSend = Send
25//! + ThreadSync = Sync + Send
26//!
27#![feature(sync_unsafe_cell)]
28#![feature(const_trait_impl)]
29#![feature(allocator_api)]
30// #![feature(receiver_trait)]
31#![feature(arbitrary_self_types)]
32#![feature(strict_provenance)]
33#![feature(error_in_core)]
34#![feature(error_generic_member_access)]
35#![feature(layout_for_ptr)]
36#![feature(trusted_len)]
37#![feature(slice_ptr_get)]
38#![feature(set_ptr_value)]
39#![feature(alloc_layout_extra)]
40
41pub mod arc_cell;
42pub mod atomic;
43pub mod cell;
44pub mod lock;
45pub mod xrc;
46pub mod xrc_cell;
47
48pub type ShareCell<T> = cell::TrustCell<T>;
49pub type Cell<T> = cell::TrustCell<T>;
50
51#[cfg(feature = "serial")]
52pub trait ThreadSend {}
53#[cfg(feature = "serial")]
54impl<T> ThreadSend for T {}
55#[cfg(feature = "serial")]
56pub trait ThreadSync {}
57#[cfg(feature = "serial")]
58impl<T> ThreadSync for T {}
59
60#[cfg(not(feature = "serial"))]
61pub trait ThreadSend: Send {}
62#[cfg(not(feature = "serial"))]
63impl<T: Send> ThreadSend for T {}
64#[cfg(not(feature = "serial"))]
65pub trait ThreadSync: Sync + Send {}
66#[cfg(not(feature = "serial"))]
67impl<T: Sync + Send> ThreadSync for T {}
68
69#[cfg(feature = "rc")]
70pub type Share<T> = crate::xrc::Xrc<T>;
71#[cfg(feature = "rc")]
72pub type ShareWeak<T> = xrc::Weak<T>;
73#[cfg(feature = "rc")]
74pub type ShareMutex<T> = crate::lock::LockCell<T>;
75#[cfg(feature = "rc")]
76pub type ShareRwLock<T> = crate::lock::LockCell<T>;
77#[cfg(feature = "rc")]
78pub type SharePtr<T> = crate::atomic::AtomicCell<T>;
79#[cfg(feature = "rc")]
80pub type ShareBool = crate::atomic::AtomicCell<bool>;
81#[cfg(feature = "rc")]
82pub type ShareU8 = crate::atomic::AtomicCell<u8>;
83#[cfg(feature = "rc")]
84pub type ShareUsize = crate::atomic::AtomicCell<usize>;
85#[cfg(feature = "rc")]
86pub type ShareU32 = crate::atomic::AtomicCell<u32>;
87#[cfg(feature = "rc")]
88pub type ShareU64 = crate::atomic::AtomicCell<u64>;
89#[cfg(feature = "rc")]
90pub use xrc_cell::XrcCell as ShareRefCell;
91#[cfg(feature = "rc")]
92#[inline(always)]
93pub fn fence(or: std::sync::atomic::Ordering) {}
94
95#[cfg(not(feature = "rc"))]
96use std::sync::{
97    atomic::{AtomicBool, AtomicPtr, AtomicU32, AtomicU64, AtomicU8, AtomicUsize},
98    Arc, Mutex, RwLock, Weak,
99};
100
101#[cfg(not(feature = "rc"))]
102pub type Share<T> = Arc<T>;
103#[cfg(not(feature = "rc"))]
104pub type ShareWeak<T> = Weak<T>;
105#[cfg(not(feature = "rc"))]
106pub type ShareMutex<T> = Mutex<T>;
107#[cfg(not(feature = "rc"))]
108pub type ShareRwLock<T> = RwLock<T>;
109#[cfg(not(feature = "rc"))]
110pub type SharePtr<T> = AtomicPtr<T>;
111#[cfg(not(feature = "rc"))]
112pub type ShareBool = AtomicBool;
113#[cfg(not(feature = "rc"))]
114pub type ShareU8 = AtomicU8;
115#[cfg(not(feature = "rc"))]
116pub type ShareUsize = AtomicUsize;
117#[cfg(not(feature = "rc"))]
118pub type ShareU32 = AtomicU32;
119#[cfg(not(feature = "rc"))]
120pub type ShareU64 = AtomicU64;
121#[cfg(not(feature = "rc"))]
122pub use arc_cell::ArcCell as ShareRefCell;
123#[cfg(not(feature = "rc"))]
124#[inline(always)]
125pub fn fence(or: std::sync::atomic::Ordering) {
126    std::sync::atomic::fence(or)
127}