git_features/threading.rs
1//! Type definitions for putting shared ownership and synchronized mutation behind the `threading` feature toggle.
2//!
3//! That way, single-threaded applications will not have to use thread-safe primitives, and simply do not specify the 'threading' feature.
4
5#[cfg(feature = "parallel")]
6mod _impl {
7 use std::sync::Arc;
8
9 /// A thread-safe cell which can be written to only once.
10 #[cfg(feature = "once_cell")]
11 pub type OnceCell<T> = once_cell::sync::OnceCell<T>;
12 /// A reference counted pointer type for shared ownership.
13 pub type OwnShared<T> = Arc<T>;
14 /// A synchronization primitive which can start read-only and transition to support mutation.
15 pub type MutableOnDemand<T> = parking_lot::RwLock<T>;
16 /// A synchronization primitive which provides read-write access right away.
17 pub type Mutable<T> = parking_lot::Mutex<T>;
18 /// A guarded reference suitable for safekeeping in a struct.
19 pub type RefGuard<'a, T> = parking_lot::RwLockReadGuard<'a, T>;
20 /// A mapped reference created from a RefGuard
21 pub type MappedRefGuard<'a, U> = parking_lot::MappedRwLockReadGuard<'a, U>;
22
23 /// Get a shared reference through a [`MutableOnDemand`] for read-only access.
24 pub fn get_ref<T>(v: &MutableOnDemand<T>) -> RefGuard<'_, T> {
25 v.read()
26 }
27
28 /// Get a mutable reference through a [`MutableOnDemand`] for read-write access.
29 pub fn get_mut<T>(v: &MutableOnDemand<T>) -> parking_lot::RwLockWriteGuard<'_, T> {
30 v.write()
31 }
32
33 /// Get a mutable reference through a [`Mutable`] for read-write access.
34 pub fn lock<T>(v: &Mutable<T>) -> parking_lot::MutexGuard<'_, T> {
35 v.lock()
36 }
37
38 /// Downgrade a handle previously obtained with [`get_mut()`] to drop mutation support.
39 pub fn downgrade_mut_to_ref<'a, T>(
40 v: parking_lot::RwLockWriteGuard<'a, T>,
41 _orig: &'a MutableOnDemand<T>,
42 ) -> RefGuard<'a, T> {
43 parking_lot::RwLockWriteGuard::downgrade(v)
44 }
45
46 /// Map a read guard into a sub-type it contains.
47 pub fn map_ref<T, U: ?Sized>(v: RefGuard<'_, T>, f: impl FnOnce(&T) -> &U) -> MappedRefGuard<'_, U> {
48 parking_lot::RwLockReadGuard::map(v, f)
49 }
50}
51
52#[cfg(not(feature = "parallel"))]
53mod _impl {
54 use std::{
55 cell::{Ref, RefCell, RefMut},
56 rc::Rc,
57 };
58
59 /// A thread-safe cell which can be written to only once.
60 #[cfg(feature = "once_cell")]
61 pub type OnceCell<T> = once_cell::unsync::OnceCell<T>;
62 /// A reference counted pointer type for shared ownership.
63 pub type OwnShared<T> = Rc<T>;
64 /// A synchronization primitive which can start read-only and transition to support mutation.
65 pub type MutableOnDemand<T> = RefCell<T>;
66 /// A synchronization primitive which provides read-write access right away.
67 pub type Mutable<T> = RefCell<T>;
68 /// A guarded reference suitable for safekeeping in a struct.
69 pub type RefGuard<'a, T> = Ref<'a, T>;
70 /// A mapped reference created from a RefGuard
71 pub type MappedRefGuard<'a, U> = Ref<'a, U>;
72
73 /// Get a shared reference through a [`MutableOnDemand`] for read-only access.
74 pub fn get_mut<T>(v: &RefCell<T>) -> RefMut<'_, T> {
75 v.borrow_mut()
76 }
77
78 /// Get a mutable reference through a [`Mutable`] for read-write access.
79 pub fn lock<T>(v: &Mutable<T>) -> RefMut<'_, T> {
80 v.borrow_mut()
81 }
82
83 /// Get a mutable reference through a [`MutableOnDemand`] for read-write access.
84 pub fn get_ref<T>(v: &RefCell<T>) -> RefGuard<'_, T> {
85 v.borrow()
86 }
87
88 /// Downgrade a handle previously obtained with [`upgrade_ref_to_mut()`] to drop mutation support.
89 pub fn downgrade_mut_to_ref<'a, T>(v: RefMut<'a, T>, orig: &'a RefCell<T>) -> RefGuard<'a, T> {
90 drop(v);
91 orig.borrow()
92 }
93
94 /// Map a read guard into a sub-type it contains.
95 pub fn map_ref<T, U: ?Sized>(v: RefGuard<'_, T>, f: impl FnOnce(&T) -> &U) -> MappedRefGuard<'_, U> {
96 Ref::map(v, f)
97 }
98}
99
100pub use _impl::*;