-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathmutex_like.rs
More file actions
115 lines (103 loc) · 3.13 KB
/
Copy pathmutex_like.rs
File metadata and controls
115 lines (103 loc) · 3.13 KB
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
113
114
115
use std::cell::UnsafeCell;
use std::ops::{Deref, DerefMut};
#[cfg(feature = "bincode")]
use bincode::{
de::{BorrowDecoder, Decoder},
enc::Encoder,
error::{DecodeError, EncodeError},
BorrowDecode, Decode, Encode,
};
/// Mutex-like wrapper, but it actually does not perform any locking.
///
/// Use this wrapper when:
/// 1. [`Send`], [`Sync`] and the interior mutability is needed,
/// 2. it is (manually) guaranteed that data races will not occur, and
/// 3. the performance is critical.
///
/// **Note**: This wrapper completely bypasses the "shared XOR mutable" rule of Rust.
/// Therefore, using this wrapper is **extremely unsafe** and should be avoided whenever possible.
#[derive(Debug)]
#[repr(transparent)]
pub struct MutexLike<T: ?Sized> {
data: UnsafeCell<T>,
}
/// Smart pointer like wrapper that is returned when [`MutexLike`] is "locked".
#[derive(Debug)]
pub struct MutexGuardLike<'a, T: ?Sized + 'a> {
mutex: &'a MutexLike<T>,
}
unsafe impl<T: ?Sized + Send> Send for MutexLike<T> {}
unsafe impl<T: ?Sized + Send> Sync for MutexLike<T> {}
unsafe impl<'a, T: ?Sized + Sync + 'a> Sync for MutexGuardLike<'a, T> {}
impl<T> MutexLike<T> {
/// Creates a new [`MutexLike`] with the given value.
///
/// # Examples
/// ```
/// use postflop_solver::MutexLike;
///
/// let mutex_like = MutexLike::new(0);
/// ```
#[inline]
pub fn new(val: T) -> Self {
Self {
data: UnsafeCell::new(val),
}
}
}
impl<T: ?Sized> MutexLike<T> {
/// Acquires a mutex-like object **without** performing any locking.
///
/// # Examples
/// ```
/// use postflop_solver::MutexLike;
///
/// let mutex_like = MutexLike::new(0);
/// *mutex_like.lock() = 10;
/// assert_eq!(*mutex_like.lock(), 10);
/// ```
#[inline]
pub fn lock(&self) -> MutexGuardLike<T> {
MutexGuardLike { mutex: self }
}
}
impl<T: ?Sized + Default> Default for MutexLike<T> {
#[inline]
fn default() -> Self {
Self::new(Default::default())
}
}
impl<'a, T: ?Sized + 'a> Deref for MutexGuardLike<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.mutex.data.get() }
}
}
impl<'a, T: ?Sized + 'a> DerefMut for MutexGuardLike<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.mutex.data.get() }
}
}
#[cfg(feature = "bincode")]
impl<T: Encode> Encode for MutexLike<T> {
#[inline]
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.lock().encode(encoder)
}
}
#[cfg(feature = "bincode")]
impl<Context, T: Decode<Context>> Decode<Context> for MutexLike<T> where {
#[inline]
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Self::new(T::decode(decoder)?))
}
}
#[cfg(feature = "bincode")]
impl<'de, Context, T: BorrowDecode<'de, Context>> BorrowDecode<'de, Context> for MutexLike<T> {
#[inline]
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Self::new(T::borrow_decode(decoder)?))
}
}