Skip to content

Commit e66e8be

Browse files
committed
Format code
1 parent e7a3e71 commit e66e8be

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

src/locks/memory.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ struct LockedKey<'a> {
1919
}
2020

2121
impl<'a> SharedLockGuard<'a> for RwLockReadGuard<'a, ()> {
22-
fn release(self: Box<Self>) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + 'a>> {
22+
fn release(
23+
self: Box<Self>,
24+
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + 'a>> {
2325
Box::pin(async move {
24-
drop(self); // Explicitly drop the guard to release the read lock
26+
drop(self); // Explicitly drop the guard to release the read lock
2527
Ok(())
2628
})
2729
}
2830
}
2931

3032
impl<'a> ExclusiveLockGuard<'a> for RwLockWriteGuard<'a, ()> {
31-
fn release(self: Box<Self>) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + 'a>> {
33+
fn release(
34+
self: Box<Self>,
35+
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + 'a>> {
3236
Box::pin(async move {
33-
drop(self); // Explicitly drop the guard to release the write lock
37+
drop(self); // Explicitly drop the guard to release the write lock
3438
Ok(())
3539
})
3640
}

src/locks/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub enum LocksType {
2828
Postgres,
2929
}
3030

31-
use std::pin::Pin;
3231
use std::future::Future;
32+
use std::pin::Pin;
3333

3434
#[must_use = "droping temporary lock makes no sense"]
3535
pub trait SharedLockGuard<'a> {

src/locks/postgres.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ pub struct PostgresSharedLockGuard {
167167
}
168168

169169
impl<'a> SharedLockGuard<'a> for PostgresSharedLockGuard {
170-
fn release(mut self: Box<Self>) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
170+
fn release(
171+
mut self: Box<Self>,
172+
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
171173
Box::pin(async move {
172174
if let Some(mut conn) = self.conn.take() {
173175
sqlx::query("SELECT pg_advisory_unlock_shared($1)")
@@ -196,7 +198,9 @@ pub struct PostgresExclusiveLockGuard {
196198
}
197199

198200
impl<'a> ExclusiveLockGuard<'a> for PostgresExclusiveLockGuard {
199-
fn release(mut self: Box<Self>) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
201+
fn release(
202+
mut self: Box<Self>,
203+
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
200204
Box::pin(async move {
201205
if let Some(mut conn) = self.conn.take() {
202206
sqlx::query("SELECT pg_advisory_unlock($1)")

tests/postgres_locks_test.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,14 @@ mod postgres_locks_tests {
160160
let lock1 = locks.prepare_lock(lock_key.clone()).await;
161161
let lock2 = locks.prepare_lock(lock_key.clone()).await;
162162

163-
let guard1 = lock1.acquire_shared().await.expect("Should acquire shared lock");
164-
let guard2 = lock2.acquire_shared().await.expect("Should acquire shared lock");
163+
let guard1 = lock1
164+
.acquire_shared()
165+
.await
166+
.expect("Should acquire shared lock");
167+
let guard2 = lock2
168+
.acquire_shared()
169+
.await
170+
.expect("Should acquire shared lock");
165171

166172
// Both guards are held - this should not deadlock
167173
let _ = guard1.release().await;
@@ -190,15 +196,21 @@ mod postgres_locks_tests {
190196

191197
// Acquire an exclusive lock
192198
let lock1 = locks.prepare_lock(lock_key.clone()).await;
193-
let guard1 = lock1.acquire_exclusive().await.expect("Should acquire exclusive lock");
199+
let guard1 = lock1
200+
.acquire_exclusive()
201+
.await
202+
.expect("Should acquire exclusive lock");
194203

195204
// Try to acquire a shared lock in another task
196205
let locks_clone = locks.clone();
197206
let lock_key_clone = lock_key.clone();
198207

199208
let task = tokio::spawn(async move {
200209
let lock2 = locks_clone.prepare_lock(lock_key_clone).await;
201-
let guard2 = lock2.acquire_shared().await.expect("Should acquire shared lock");
210+
let guard2 = lock2
211+
.acquire_shared()
212+
.await
213+
.expect("Should acquire shared lock");
202214
let _ = guard2.release().await;
203215
true
204216
});
@@ -251,10 +263,16 @@ mod postgres_locks_tests {
251263
let lock1 = locks.prepare_lock(lock_key1).await;
252264
let lock2 = locks.prepare_lock(lock_key2).await;
253265

254-
let guard1 = lock1.acquire_exclusive().await.expect("Should acquire exclusive lock");
266+
let guard1 = lock1
267+
.acquire_exclusive()
268+
.await
269+
.expect("Should acquire exclusive lock");
255270

256271
// Should be able to acquire exclusive lock on different key immediately
257-
let guard2 = lock2.acquire_exclusive().await.expect("Should acquire exclusive lock");
272+
let guard2 = lock2
273+
.acquire_exclusive()
274+
.await
275+
.expect("Should acquire exclusive lock");
258276

259277
// Both locks should be held independently
260278
let _ = guard1.release().await;
@@ -284,7 +302,10 @@ mod postgres_locks_tests {
284302
// Acquire and release lock in a scope
285303
{
286304
let lock1 = locks.prepare_lock(lock_key.clone()).await;
287-
let guard1 = lock1.acquire_exclusive().await.expect("Should acquire lock");
305+
let guard1 = lock1
306+
.acquire_exclusive()
307+
.await
308+
.expect("Should acquire lock");
288309
// Explicitly release before scope ends
289310
let _ = guard1.release().await;
290311
}
@@ -294,7 +315,10 @@ mod postgres_locks_tests {
294315

295316
// Should be able to acquire the lock immediately
296317
let lock2 = locks.prepare_lock(lock_key.clone()).await;
297-
let guard2 = lock2.acquire_exclusive().await.expect("Should acquire lock after release");
318+
let guard2 = lock2
319+
.acquire_exclusive()
320+
.await
321+
.expect("Should acquire lock after release");
298322

299323
// If we get here, the lock was successfully released and reacquired
300324
let _ = guard2.release().await;

0 commit comments

Comments
 (0)