Skip to content

Commit b6cd08b

Browse files
Remove lock refs, use scope closures instead
1 parent 5c9ce8d commit b6cd08b

File tree

2 files changed

+36
-77
lines changed

2 files changed

+36
-77
lines changed

crates/icp-cli/src/store_artifact.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,31 @@ impl ArtifactStore {
6161

6262
impl ArtifactStore {
6363
pub(crate) async fn save(&self, name: &str, wasm: &[u8]) -> Result<(), SaveError> {
64-
// Lock Artifact Store
65-
let lock = self.lock.write_ref().await?;
66-
67-
// Store artifact
68-
write(&lock.artifact_by_name(name), wasm).context(SaveWriteFileSnafu)?;
69-
70-
Ok(())
64+
self.lock
65+
.with_write(async |store| {
66+
// Save artifact
67+
write(&store.artifact_by_name(name), wasm).context(SaveWriteFileSnafu)?;
68+
Ok(())
69+
})
70+
.await?
7171
}
7272

7373
pub(crate) async fn lookup(&self, name: &str) -> Result<Vec<u8>, LookupError> {
74-
// Lock Artifact Store
75-
let lock = self.lock.read_ref().await?;
76-
77-
let artifact = lock.artifact_by_name(name);
78-
// Not Found
79-
if !artifact.exists() {
80-
return Err(LookupError::LookupArtifactNotFound {
81-
name: name.to_owned(),
82-
});
83-
}
84-
85-
// Load artifact
86-
let wasm = read(&artifact).context(LookupReadFileSnafu)?;
87-
88-
Ok(wasm)
74+
self.lock
75+
.with_read(async |store| {
76+
let artifact = store.artifact_by_name(name);
77+
// Not Found
78+
if !artifact.exists() {
79+
return Err(LookupError::LookupArtifactNotFound {
80+
name: name.to_owned(),
81+
});
82+
}
83+
84+
// Load artifact
85+
let wasm = read(&artifact).context(LookupReadFileSnafu)?;
86+
87+
Ok(wasm)
88+
})
89+
.await?
8990
}
9091
}

crates/icp/src/fs/lock.rs

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use crate::prelude::*;
22
use snafu::{ResultExt, Snafu};
33
use std::{fs::File, io, ops::Deref};
4-
use tokio::{
5-
sync::{RwLock, RwLockReadGuard, RwLockWriteGuard},
6-
task::spawn_blocking,
7-
};
4+
use tokio::{sync::RwLock, task::spawn_blocking};
85

96
pub struct DirectoryStructureLock<T: PathsAccess> {
107
paths_access: T,
@@ -58,8 +55,7 @@ impl<T: PathsAccess> DirectoryStructureLock<T> {
5855
.await
5956
.unwrap()
6057
}
61-
62-
pub async fn read_ref(&self) -> Result<DirectoryStructureGuardReadRef<'_, T>, LockError> {
58+
pub async fn with_read<R>(&self, f: impl AsyncFnOnce(&T) -> R) -> Result<R, LockError> {
6359
let guard = self.lock_file.read().await;
6460
let lock_file = guard.try_clone().context(HandleCloneFailedSnafu {
6561
path: &self.lock_path,
@@ -70,14 +66,14 @@ impl<T: PathsAccess> DirectoryStructureLock<T> {
7066
.context(LockFailedSnafu {
7167
lock_path: &self.lock_path,
7268
})?;
73-
74-
Ok(DirectoryStructureGuardReadRef {
75-
paths_access: &self.paths_access,
76-
guard,
77-
})
69+
let ret = f(&self.paths_access).await;
70+
guard.unlock().context(LockFailedSnafu {
71+
lock_path: &self.lock_path,
72+
})?;
73+
Ok(ret)
7874
}
7975

80-
pub async fn write_ref(&self) -> Result<DirectoryStructureGuardWriteRef<'_, T>, LockError> {
76+
pub async fn with_write<R>(&self, f: impl AsyncFnOnce(&T) -> R) -> Result<R, LockError> {
8177
let guard = self.lock_file.write().await;
8278
let lock_file = guard.try_clone().context(HandleCloneFailedSnafu {
8379
path: &self.lock_path,
@@ -88,11 +84,11 @@ impl<T: PathsAccess> DirectoryStructureLock<T> {
8884
.context(LockFailedSnafu {
8985
lock_path: &self.lock_path,
9086
})?;
91-
92-
Ok(DirectoryStructureGuardWriteRef {
93-
paths_access: &self.paths_access,
94-
guard,
95-
})
87+
let ret = f(&self.paths_access).await;
88+
guard.unlock().context(LockFailedSnafu {
89+
lock_path: &self.lock_path,
90+
})?;
91+
Ok(ret)
9692
}
9793
}
9894

@@ -111,44 +107,6 @@ pub enum LockError {
111107
HandleCloneFailed { source: io::Error, path: PathBuf },
112108
}
113109

114-
pub struct DirectoryStructureGuardReadRef<'a, T> {
115-
paths_access: &'a T,
116-
guard: RwLockReadGuard<'a, File>,
117-
}
118-
119-
impl<'a, T> Deref for DirectoryStructureGuardReadRef<'a, T> {
120-
type Target = T;
121-
122-
fn deref(&self) -> &Self::Target {
123-
self.paths_access
124-
}
125-
}
126-
127-
impl<'a, T> Drop for DirectoryStructureGuardReadRef<'a, T> {
128-
fn drop(&mut self) {
129-
_ = self.guard.unlock();
130-
}
131-
}
132-
133-
pub struct DirectoryStructureGuardWriteRef<'a, T> {
134-
paths_access: &'a T,
135-
guard: RwLockWriteGuard<'a, File>,
136-
}
137-
138-
impl<'a, T> Deref for DirectoryStructureGuardWriteRef<'a, T> {
139-
type Target = T;
140-
141-
fn deref(&self) -> &Self::Target {
142-
self.paths_access
143-
}
144-
}
145-
146-
impl<'a, T> Drop for DirectoryStructureGuardWriteRef<'a, T> {
147-
fn drop(&mut self) {
148-
_ = self.guard.unlock();
149-
}
150-
}
151-
152110
pub struct DirectoryStructureGuardOwned<T> {
153111
paths_access: T,
154112
guard: File,

0 commit comments

Comments
 (0)