1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicIsize, AtomicU64, AtomicUsize};
pub use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard, Mutex, MutexGuard};
pub mod async_std;
impl<T: ?Sized> ArcExt for T {}
pub trait ArcExt {
#[inline]
fn arc(self) -> Arc<Self>
where
Self: Sized,
{
Arc::new(self)
}
}
impl<T: ?Sized> RwLockExt for T {}
pub trait RwLockExt {
#[inline]
fn rwlock(self) -> RwLock<Self>
where
Self: Sized,
{
RwLock::new(self)
}
}
impl<T: ?Sized> MutexExt for T {}
pub trait MutexExt {
#[inline]
fn mutex(self) -> Mutex<Self>
where
Self: Sized,
{
Mutex::new(self)
}
}
pub trait AtomicExt<T> {
fn atomic(self) -> T;
}
impl AtomicExt<AtomicUsize> for usize {
#[inline]
fn atomic(self) -> AtomicUsize {
AtomicUsize::new(self)
}
}
impl AtomicExt<AtomicIsize> for isize {
#[inline]
fn atomic(self) -> AtomicIsize {
AtomicIsize::new(self)
}
}
impl AtomicExt<AtomicU64> for u64 {
#[inline]
fn atomic(self) -> AtomicU64 {
AtomicU64::new(self)
}
}
impl AtomicExt<AtomicI64> for i64 {
#[inline]
fn atomic(self) -> AtomicI64 {
AtomicI64::new(self)
}
}
impl AtomicExt<AtomicBool> for bool {
#[inline]
fn atomic(self) -> AtomicBool {
AtomicBool::new(self)
}
}
#[test]
fn test_rwlock() {
let a = 1.rwlock().arc();
assert_eq!(*a.read(), 1);
*a.write() = 2;
assert_eq!(*a.read(), 2);
}
#[test]
fn test_mutex() {
let m = 1.mutex().arc();
assert_eq!(*m.lock(), 1);
*m.lock() = 2;
assert_eq!(*m.lock(), 2);
}
#[test]
fn test_atomic() {
use std::sync::atomic::Ordering;
assert_eq!(100usize.atomic().load(Ordering::SeqCst), 100);
assert_eq!(100isize.atomic().load(Ordering::SeqCst), 100);
assert_eq!(100u64.atomic().load(Ordering::SeqCst), 100);
assert_eq!(100i64.atomic().load(Ordering::SeqCst), 100);
assert!(true.atomic().load(Ordering::SeqCst))
}