Debian Codegraph @

src/threading.rs

raw symbols SCIP
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")]
6 mod _implrust-analyzer cargo gix-features 0.48.0 threading/_impl/ {
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#;
8
9 /// A thread-safe cell which can be written to only once.
10 ///
11 /// Note: We use `once_cell` here because `std::sync::OnceLock::get_or_try_init()` is not yet stable.
12 /// Once it's stabilized, we can switch to `std::sync::OnceLock`.
13 #[cfg(feature = "once_cell")]
14 pub type OnceCellrust-analyzer cargo gix-features 0.48.0 threading/_impl/OnceCell#<T> = once_cellrust-analyzer cargo once_cell 1.21.4 crate/::syncrust-analyzer cargo once_cell 1.21.4 sync/::OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#<T>;
15 /// A reference counted pointer type for shared ownership.
16 pub type OwnSharedrust-analyzer cargo gix-features 0.48.0 threading/_impl/OwnShared#<T> = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<T>;
17 /// A synchronization primitive which can start read-only and transition to support mutation.
18 pub type MutableOnDemandrust-analyzer cargo gix-features 0.48.0 threading/_impl/MutableOnDemand#<T> = parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::RwLockrust-analyzer cargo parking_lot 0.12.5 rwlock/RwLock#<T>;
19 /// A synchronization primitive which provides read-write access right away.
20 pub type Mutablerust-analyzer cargo gix-features 0.48.0 threading/_impl/Mutable#<T> = parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::Mutexrust-analyzer cargo parking_lot 0.12.5 mutex/Mutex#<T>;
21 /// A guarded reference suitable for safekeeping in a struct.
22 pub type RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'a, T> = parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::RwLockReadGuardrust-analyzer cargo parking_lot 0.12.5 rwlock/RwLockReadGuard#<'a, T>;
23 /// A mapped reference created from a `RefGuard`
24 pub type MappedRefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/MappedRefGuard#<'a, U> = parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::MappedRwLockReadGuardrust-analyzer cargo parking_lot 0.12.5 rwlock/MappedRwLockReadGuard#<'a, U>;
25
26 /// Get a shared reference through a [`MutableOnDemand`] for read-only access.
27 pub fn get_refrust-analyzer cargo gix-features 0.48.0 threading/_impl/get_ref().<T>(v: &MutableOnDemandrust-analyzer cargo gix-features 0.48.0 threading/_impl/MutableOnDemand#<T>) -> RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'_, T> {
28 v.readrust-analyzer cargo lock_api 0.4.14 rwlock/impl#[`RwLock<R, T>`]read().()
29 }
30
31 /// Get a mutable reference through a [`MutableOnDemand`] for read-write access.
32 pub fn get_mutrust-analyzer cargo gix-features 0.48.0 threading/_impl/get_mut().<T>(v: &MutableOnDemandrust-analyzer cargo gix-features 0.48.0 threading/_impl/MutableOnDemand#<T>) -> parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::RwLockWriteGuardrust-analyzer cargo parking_lot 0.12.5 rwlock/RwLockWriteGuard#<'_, T> {
33 v.writerust-analyzer cargo lock_api 0.4.14 rwlock/impl#[`RwLock<R, T>`]write().()
34 }
35
36 /// Get a mutable reference to the underlying data, with semantics similar to [Arc::make_mut()].
37 pub fn make_mutrust-analyzer cargo gix-features 0.48.0 threading/_impl/make_mut().<T: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#>(this: &mut OwnSharedrust-analyzer cargo gix-features 0.48.0 threading/_impl/OwnShared#<T>) -> &mut T {
38 OwnSharedrust-analyzer cargo gix-features 0.48.0 threading/_impl/OwnShared#::make_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`]make_mut().(this)
39 }
40
41 /// Get a mutable reference through a [`Mutable`] for read-write access.
42 pub fn lockrust-analyzer cargo gix-features 0.48.0 threading/_impl/lock().<T>(v: &Mutablerust-analyzer cargo gix-features 0.48.0 threading/_impl/Mutable#<T>) -> parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::MutexGuardrust-analyzer cargo parking_lot 0.12.5 mutex/MutexGuard#<'_, T> {
43 v.lockrust-analyzer cargo lock_api 0.4.14 mutex/impl#[`Mutex<R, T>`]lock().()
44 }
45
46 /// Downgrade a handle previously obtained with [`get_mut()`] to drop mutation support.
47 pub fn downgrade_mut_to_refrust-analyzer cargo gix-features 0.48.0 threading/_impl/downgrade_mut_to_ref().<'a, T>(
48 v: parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::RwLockWriteGuardrust-analyzer cargo parking_lot 0.12.5 rwlock/RwLockWriteGuard#<'a, T>,
49 _orig: &'a MutableOnDemandrust-analyzer cargo gix-features 0.48.0 threading/_impl/MutableOnDemand#<T>,
50 ) -> RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'a, T> {
51 parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::RwLockWriteGuardrust-analyzer cargo parking_lot 0.12.5 rwlock/RwLockWriteGuard#::downgraderust-analyzer cargo lock_api 0.4.14 rwlock/impl#[`RwLockWriteGuard<'a, R, T>`]downgrade().(v)
52 }
53
54 /// Map a read guard into a sub-type it contains.
55 pub fn map_refrust-analyzer cargo gix-features 0.48.0 threading/_impl/map_ref().<T, U: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(v: RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'_, T>, f: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&T) -> &U) -> MappedRefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/MappedRefGuard#<'_, U> {
56 parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::RwLockReadGuardrust-analyzer cargo parking_lot 0.12.5 rwlock/RwLockReadGuard#::maprust-analyzer cargo lock_api 0.4.14 rwlock/impl#[`RwLockReadGuard<'a, R, T>`]map().(v, f)
57 }
58 }
59
60 #[cfg(not(feature = "parallel"))]
61 mod _implrust-analyzer cargo gix-features 0.48.0 threading/_impl/ {
62 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
63 cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::{Refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Ref#, RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#, RefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefMut#},
64 rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#,
65 };
66
67 /// A thread-safe cell which can be written to only once.
68 ///
69 /// Note: We use `once_cell` here because `std::cell::OnceCell::get_or_try_init()` is not yet stable.
70 /// Once it's stabilized, we can switch to `std::cell::OnceCell`.
71 #[cfg(feature = "once_cell")]
72 pub type OnceCell<T> = once_cellrust-analyzer cargo once_cell 1.21.4 crate/::unsyncrust-analyzer cargo once_cell 1.21.4 unsync/::OnceCellrust-analyzer cargo once_cell 1.21.4 unsync/OnceCell#<T>;
73 /// A reference counted pointer type for shared ownership.
74 pub type OwnShared<T> = Rc<T>;
75 /// A synchronization primitive which can start read-only and transition to support mutation.
76 pub type MutableOnDemand<T> = RefCell<T>;
77 /// A synchronization primitive which provides read-write access right away.
78 pub type Mutable<T> = RefCell<T>;
79 /// A guarded reference suitable for safekeeping in a struct.
80 pub type RefGuard<'a, T> = Ref<'a, T>;
81 /// A mapped reference created from a RefGuard
82 pub type MappedRefGuard<'a, U> = Ref<'a, U>;
83
84 /// Get a shared reference through a [`MutableOnDemand`] for read-only access.
85 pub fn get_mut<T>(v: &RefCell<T>) -> RefMut<'_, T> {
86 v.borrow_mut()
87 }
88
89 /// Get a mutable reference to the underlying data, with semantics similar to [Rc::make_mut()].
90 pub fn make_mut<T: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#>(this: &mut OwnSharedrust-analyzer cargo gix-features 0.48.0 threading/_impl/OwnShared#<T>) -> &mut T {
91 OwnSharedrust-analyzer cargo gix-features 0.48.0 threading/_impl/OwnShared#::make_mut(this)
92 }
93
94 /// Get a mutable reference through a [`Mutable`] for read-write access.
95 pub fn lock<T>(v: &Mutablerust-analyzer cargo gix-features 0.48.0 threading/_impl/Mutable#<T>) -> RefMut<'_, T> {
96 v.borrow_mut()
97 }
98
99 /// Get a mutable reference through a [`MutableOnDemand`] for read-write access.
100 pub fn get_ref<T>(v: &RefCell<T>) -> RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'_, T> {
101 v.borrow()
102 }
103
104 /// Downgrade a handle previously obtained with [`upgrade_ref_to_mut()`] to drop mutation support.
105 pub fn downgrade_mut_to_ref<'a, T>(v: RefMut<'a, T>, orig: &'a RefCell<T>) -> RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'a, T> {
106 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(v);
107 orig.borrow()
108 }
109
110 /// Map a read guard into a sub-type it contains.
111 pub fn map_ref<T, U: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(v: RefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/RefGuard#<'_, T>, f: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&T) -> &U) -> MappedRefGuardrust-analyzer cargo gix-features 0.48.0 threading/_impl/MappedRefGuard#<'_, U> {
112 Ref::map(v, f)
113 }
114 }
115
116 pub use _implrust-analyzer cargo gix-features 0.48.0 threading/_impl/::*;