Debian Codegraph @

src/sync.rs

raw symbols SCIP
1 // For types with identical signatures that don't require runtime support,
2 // we can just arbitrarily pick one to use based on what's enabled.
3 //
4 // We'll generally lean towards Tokio's types as those are more featureful
5 // (including `tokio-console` support) and more widely deployed.
6
7 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
8 pub use async_stdrust-analyzer cargo async-std 1.13.2 crate/::syncrust-analyzer cargo async-std 1.13.2 sync/::{Mutexrust-analyzer cargo async-lock 3.4.2 mutex/Mutex# as AsyncMutexrust-analyzer cargo async-lock 3.4.2 mutex/Mutex#, MutexGuardrust-analyzer cargo async-lock 3.4.2 mutex/MutexGuard# as AsyncMutexGuardrust-analyzer cargo async-lock 3.4.2 mutex/MutexGuard#};
9
10 #[cfg(feature = "_rt-tokio")]
11 pub use tokiorust-analyzer cargo tokio 1.53.1 crate/::syncrust-analyzer cargo tokio 1.53.1 sync/::{Mutexrust-analyzer cargo tokio 1.53.1 sync/mutex/Mutex# as AsyncMutexrust-analyzer cargo tokio 1.53.1 sync/mutex/Mutex#, MutexGuardrust-analyzer cargo tokio 1.53.1 sync/mutex/MutexGuard# as AsyncMutexGuardrust-analyzer cargo tokio 1.53.1 sync/mutex/MutexGuard#};
12
13 pub struct AsyncSemaphorerust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore# {
14 // We use the semaphore from futures-intrusive as the one from async-std
15 // is missing the ability to add arbitrary permits, and is not guaranteed to be fair:
16 // * https://github.com/smol-rs/async-lock/issues/22
17 // * https://github.com/smol-rs/async-lock/issues/23
18 //
19 // We're on the look-out for a replacement, however, as futures-intrusive is not maintained
20 // and there are some soundness concerns (although it turns out any intrusive future is unsound
21 // in MIRI due to the necessitated mutable aliasing):
22 // https://github.com/launchbadge/sqlx/issues/1668
23 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
24 inner: futures_intrusiverust-analyzer cargo futures-intrusive 0.5.0 crate/::syncrust-analyzer cargo futures-intrusive 0.5.0 sync/::Semaphorerust-analyzer cargo futures-intrusive 0.5.0 sync/semaphore/if_std/Semaphore#,
25
26 #[cfg(feature = "_rt-tokio")]
27 innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner.: tokiorust-analyzer cargo tokio 1.53.1 crate/::syncrust-analyzer cargo tokio 1.53.1 sync/::Semaphorerust-analyzer cargo tokio 1.53.1 sync/semaphore/Semaphore#,
28 }
29
30 impl AsyncSemaphorerust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore# {
31 #[track_caller]
32 pub fn newrust-analyzer cargo sqlx-core 0.8.3 sync/impl#[AsyncSemaphore]new().(fair: bool, permits: usize) -> Selfrust-analyzer cargo sqlx-core 0.8.3 sync/impl#[AsyncSemaphore] {
33 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(not(any(feature = "_rt-async-std", feature = "_rt-tokio"))) {
34 craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::missing_rtrust-analyzer cargo sqlx-core 0.8.3 rt/missing_rt().((fair, permits));
35 }
36
37 AsyncSemaphorerust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore# {
38 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
39 innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner.: futures_intrusiverust-analyzer cargo futures-intrusive 0.5.0 crate/::syncrust-analyzer cargo futures-intrusive 0.5.0 sync/::Semaphorerust-analyzer cargo futures-intrusive 0.5.0 sync/semaphore/if_std/Semaphore#::new(fair, permits),
40 #[cfg(feature = "_rt-tokio")]
41 innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner.: {
42 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(fair, "Tokio only has fair permits");
43 tokiorust-analyzer cargo tokio 1.53.1 crate/::syncrust-analyzer cargo tokio 1.53.1 sync/::Semaphorerust-analyzer cargo tokio 1.53.1 sync/semaphore/Semaphore#::newrust-analyzer cargo tokio 1.53.1 sync/semaphore/impl#[Semaphore]new().(permits)
44 },
45 }
46 }
47
48 pub fn permitsrust-analyzer cargo sqlx-core 0.8.3 sync/impl#[AsyncSemaphore]permits().(&self) -> usize {
49 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
50 return self.inner.permits();
51
52 #[cfg(feature = "_rt-tokio")]
53 return self.innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner..available_permitsrust-analyzer cargo tokio 1.53.1 sync/semaphore/impl#[Semaphore]available_permits().();
54
55 #[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
56 craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::missing_rtrust-analyzer cargo sqlx-core 0.8.3 rt/missing_rt().(())
57 }
58
59 pub async fn acquirerust-analyzer cargo sqlx-core 0.8.3 sync/impl#[AsyncSemaphore]acquire().(&self, permits: u32) -> AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#<'_> {
60 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
61 return AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser# {
62 inner: self.inner.acquire(permits as usize).await,
63 };
64
65 #[cfg(feature = "_rt-tokio")]
66 return AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser# {
67 innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#inner.: self
68 .innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner.
69 // Weird quirk: `tokio::sync::Semaphore` mostly uses `usize` for permit counts,
70 // but `u32` for this and `try_acquire_many()`.
71 .acquire_manyrust-analyzer cargo tokio 1.53.1 sync/semaphore/impl#[Semaphore]acquire_many().(permits)
72 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().
73 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("BUG: we do not expose the `.close()` method"),
74 };
75
76 #[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
77 craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::missing_rtrust-analyzer cargo sqlx-core 0.8.3 rt/missing_rt().(permits)
78 }
79
80 pub fn try_acquirerust-analyzer cargo sqlx-core 0.8.3 sync/impl#[AsyncSemaphore]try_acquire().(&self, permits: u32) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#<'_>> {
81 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
82 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser# {
83 inner: self.inner.try_acquire(permits as usize)?,
84 });
85
86 #[cfg(feature = "_rt-tokio")]
87 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser# {
88 innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#inner.: self.innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner..try_acquire_manyrust-analyzer cargo tokio 1.53.1 sync/semaphore/impl#[Semaphore]try_acquire_many().(permits).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().,
89 });
90
91 #[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
92 craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::missing_rtrust-analyzer cargo sqlx-core 0.8.3 rt/missing_rt().(permits)
93 }
94
95 pub fn releaserust-analyzer cargo sqlx-core 0.8.3 sync/impl#[AsyncSemaphore]release().(&self, permits: usize) {
96 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
97 return self.inner.release(permits);
98
99 #[cfg(feature = "_rt-tokio")]
100 return self.innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphore#inner..add_permitsrust-analyzer cargo tokio 1.53.1 sync/semaphore/impl#[Semaphore]add_permits().(permits);
101
102 #[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
103 craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::missing_rtrust-analyzer cargo sqlx-core 0.8.3 rt/missing_rt().(permits)
104 }
105 }
106
107 pub struct AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#<'a> {
108 // We use the semaphore from futures-intrusive as the one from async-std
109 // is missing the ability to add arbitrary permits, and is not guaranteed to be fair:
110 // * https://github.com/smol-rs/async-lock/issues/22
111 // * https://github.com/smol-rs/async-lock/issues/23
112 //
113 // We're on the look-out for a replacement, however, as futures-intrusive is not maintained
114 // and there are some soundness concerns (although it turns out any intrusive future is unsound
115 // in MIRI due to the necessitated mutable aliasing):
116 // https://github.com/launchbadge/sqlx/issues/1668
117 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
118 inner: futures_intrusiverust-analyzer cargo futures-intrusive 0.5.0 crate/::syncrust-analyzer cargo futures-intrusive 0.5.0 sync/::SemaphoreReleaserrust-analyzer cargo futures-intrusive 0.5.0 sync/semaphore/if_std/SemaphoreReleaser#<'a>,
119
120 #[cfg(feature = "_rt-tokio")]
121 innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#inner.: tokiorust-analyzer cargo tokio 1.53.1 crate/::syncrust-analyzer cargo tokio 1.53.1 sync/::SemaphorePermitrust-analyzer cargo tokio 1.53.1 sync/semaphore/SemaphorePermit#<'a>,
122
123 #[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
124 _phantom: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<&'a ()>,
125 }
126
127 impl AsyncSemaphoreReleaserrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#<'_> {
128 pub fn disarmrust-analyzer cargo sqlx-core 0.8.3 sync/impl#[`AsyncSemaphoreReleaser<'_>`]disarm().(self) {
129 #[cfg(feature = "_rt-tokio")]
130 {
131 self.innerrust-analyzer cargo sqlx-core 0.8.3 sync/AsyncSemaphoreReleaser#inner..forgetrust-analyzer cargo tokio 1.53.1 sync/semaphore/impl#[`SemaphorePermit<'a>`]forget().();
132 }
133
134 #[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
135 {
136 let mut this = self;
137 this.inner.disarm();
138 }
139
140 #[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
141 craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::missing_rtrust-analyzer cargo sqlx-core 0.8.3 rt/missing_rt().(())
142 }
143 }