orx_concurrent_option/exclusive.rs
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
use crate::{states::*, ConcurrentOption};
use core::{
mem::MaybeUninit,
ops::{Deref, DerefMut},
sync::atomic::Ordering,
};
impl<T> ConcurrentOption<T> {
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
///
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
/// the inner type's [`Deref::Target`] type.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x: ConcurrentOption<String> = ConcurrentOption::some("hey".to_owned());
/// assert_eq!(x.exclusive_as_deref_mut().map(|x| {
/// x.make_ascii_uppercase();
/// x
/// }), Some("HEY".to_owned().as_mut_str()));
/// ```
pub fn exclusive_as_deref_mut(&mut self) -> Option<&mut <T as Deref>::Target>
where
T: DerefMut,
{
self.exclusive_as_mut().map(|x| x.deref_mut())
}
/// Converts from `&mut Option<T>` to `Option<&mut T>`.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::some(2);
/// match x.exclusive_as_mut() {
/// Some(v) => *v = 42,
/// None => {},
/// }
/// assert_eq!(unsafe { x.as_ref() }, Some(&42));
/// ```
pub fn exclusive_as_mut(&mut self) -> Option<&mut T> {
match self.state.load(Ordering::Relaxed) {
SOME => Some(unsafe { (*self.value.get()).assume_init_mut() }),
_ => None,
}
}
/// Takes the value out of the option, leaving a None in its place.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::some(42);
/// let y = x.exclusive_take();
/// assert_eq!(x, ConcurrentOption::none());
/// assert_eq!(y, Some(42));
///
/// let mut x: ConcurrentOption<u32> = ConcurrentOption::none();
/// let y = x.exclusive_take();
/// assert_eq!(x, ConcurrentOption::none());
/// assert_eq!(y, None);
/// ```
pub fn exclusive_take(&mut self) -> Option<T> {
match self.state.load(Ordering::Relaxed) {
SOME => {
self.state.store(NONE, Ordering::Relaxed);
let x = unsafe { &mut *self.value.get() };
Some(unsafe { x.assume_init_read() })
}
_ => None,
}
}
/// Takes the value out of the option, but only if the predicate evaluates to
/// `true` on a mutable reference to the value.
///
/// In other words, replaces `self` with None if the predicate returns `true`.
/// This method operates similar to [`ConcurrentOption::exclusive_take`] but conditional.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::some(42);
///
/// let prev = x.exclusive_take_if(|v| if *v == 42 {
/// *v += 1;
/// false
/// } else {
/// false
/// });
/// assert_eq!(x, ConcurrentOption::some(43));
/// assert_eq!(prev, None);
///
/// let prev = x.exclusive_take_if(|v| *v == 43);
/// assert_eq!(x, ConcurrentOption::none());
/// assert_eq!(prev, Some(43));
/// ```
pub fn exclusive_take_if<P>(&mut self, predicate: P) -> Option<T>
where
P: FnOnce(&mut T) -> bool,
{
match self.exclusive_as_mut().map_or(false, predicate) {
true => self.exclusive_take(),
false => None,
}
}
/// Returns a mutable iterator over the possibly contained value; yields
/// * the single element if the option is of Some variant;
/// * no elements otherwise.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::some(4);
/// match x.exclusive_iter_mut().next() {
/// Some(v) => *v = 42,
/// None => {},
/// }
/// assert_eq!(x, ConcurrentOption::some(42));
///
/// let mut x: ConcurrentOption<u32> = ConcurrentOption::none();
/// assert_eq!(x.exclusive_iter_mut().next(), None);
/// ```
pub fn exclusive_iter_mut(&mut self) -> crate::iter::IterMut<'_, T> {
let maybe = self.exclusive_as_mut();
crate::iter::IterMut { maybe }
}
/// Replaces the actual value in the option by the value given in parameter,
/// returning the old value if present,
/// leaving a Some in its place without de-initializing either one.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::some(2);
/// let old = x.exclusive_replace(5);
/// assert_eq!(x, ConcurrentOption::some(5));
/// assert_eq!(old, Some(2));
///
/// let mut x: ConcurrentOption<u32> = ConcurrentOption::none();
/// let old = x.exclusive_replace(3);
/// assert_eq!(x, ConcurrentOption::some(3));
/// assert_eq!(old, None);
/// ```
#[allow(clippy::panic, clippy::missing_panics_doc)]
pub fn exclusive_replace(&mut self, value: T) -> Option<T> {
match self.state.load(Ordering::Relaxed) {
SOME => {
self.state.store(RESERVED, Ordering::Relaxed);
let x = unsafe { (*self.value.get()).assume_init_mut() };
let old = core::mem::replace(x, value);
self.state.store(SOME, Ordering::Relaxed);
Some(old)
}
NONE => {
self.state.store(RESERVED, Ordering::Relaxed);
self.value = MaybeUninit::new(value).into();
self.state.store(SOME, Ordering::Relaxed);
None
}
_ => panic!("ConcurrentOption value is `replace`d while its value is being written."),
}
}
/// Inserts `value` into the option, then returns a mutable reference to it.
///
/// If the option already contains a value, the old value is dropped.
///
/// See also [`Option::get_or_insert`], which doesn't update the value if
/// the option already contains Some.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut opt: ConcurrentOption<_> = ConcurrentOption::none();
///
/// let val = opt.exclusive_insert(1);
/// assert_eq!(*val, 1);
/// assert_eq!(unsafe { opt.as_ref() }, Some(&1));
///
/// let val = opt.exclusive_insert(2);
/// assert_eq!(*val, 2);
/// *val = 3;
/// assert_eq!(opt.unwrap(), 3);
/// ```
#[allow(clippy::panic, clippy::missing_panics_doc)]
pub fn exclusive_insert(&mut self, value: T) -> &mut T {
match self.state.load(Ordering::Relaxed) {
SOME => {
self.state.store(RESERVED, Ordering::Relaxed);
let x = unsafe { (*self.value.get()).assume_init_mut() };
let _ = core::mem::replace(x, value);
self.state.store(SOME, Ordering::Relaxed);
}
NONE => {
self.state.store(RESERVED, Ordering::Relaxed);
self.value = MaybeUninit::new(value).into();
self.state.store(SOME, Ordering::Relaxed);
}
_ => panic!("ConcurrentOption value is `insert`ed while its value is being written."),
}
self.exclusive_as_mut().expect("should be some")
}
/// Inserts `value` into the option if it is None, then
/// returns a mutable reference to the contained value.
///
/// See also [`ConcurrentOption::insert`], which updates the value even if
/// the option already contains Some.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::none();
///
/// {
/// let y: &mut u32 = x.exclusive_get_or_insert(5);
/// assert_eq!(y, &5);
///
/// *y = 7;
/// }
///
/// assert_eq!(x, ConcurrentOption::some(7));
/// ```
pub fn exclusive_get_or_insert(&mut self, value: T) -> &mut T {
self.exclusive_get_or_insert_with(|| value)
}
/// Inserts a value computed from `f` into the option if it is None,
/// then returns a mutable reference to the contained value.
///
/// # Examples
///
/// ```rust
/// use orx_concurrent_option::*;
///
/// let mut x = ConcurrentOption::none();
///
/// {
/// let y: &mut u32 = x.exclusive_get_or_insert_with(|| 5);
/// assert_eq!(y, &5);
///
/// *y = 7;
/// }
///
/// assert_eq!(x, ConcurrentOption::some(7));
/// ```
#[allow(clippy::panic, clippy::missing_panics_doc)]
pub fn exclusive_get_or_insert_with<F>(&mut self, f: F) -> &mut T
where
F: FnOnce() -> T,
{
match self.state.load(Ordering::Relaxed) {
SOME => self.exclusive_as_mut().expect("is guaranteed to be some"),
NONE => {
self.state.store(RESERVED, Ordering::Relaxed);
self.value = MaybeUninit::new(f()).into();
self.state.store(SOME, Ordering::Relaxed);
self.exclusive_as_mut().expect("is guaranteed to be some")
}
_ => panic!(
"ConcurrentOption `get_or_insert_with` is called while its value is being written."
),
}
}
}