Skip to content

Commit

Permalink
Clarify
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 25, 2024
1 parent a68717d commit d78708c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/storage/tool_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,22 @@ impl ToolCache {
}
}

// NOTE: Using std::fs here and passing a reader to serde_json lets us
// deserialize the cache faster and without storing the file in memory.
async fn load_impl(path: PathBuf) -> AftmanResult<ToolCache> {
Ok(spawn_blocking(move || {
// NOTE: Using std::fs here and passing a reader to serde_json lets us
// deserialize the cache faster and without storing the file in memory.
let result = spawn_blocking(move || {
use std::{
fs::File,
io::{BufReader, Error},
};
let reader = BufReader::new(File::open(path)?);
let this: ToolCache = serde_json::from_reader(reader)?;
Ok::<_, Error>(this)
})
.await?
.unwrap_or_default())
});

Ok(result.await?.unwrap_or_default())
}

// Same as in our load implementation, see notes above.
async fn save_impl(path: PathBuf, cache: &ToolCache) -> AftmanResult<()> {
// NOTE: We save using sorted json arrays here, which is
// compatible with the deserialize implementation for DashSet,
Expand All @@ -192,7 +191,8 @@ async fn save_impl(path: PathBuf, cache: &ToolCache) -> AftmanResult<()> {
"installed": cache.all_installed(),
});

spawn_blocking(move || {
// Same as in our load implementation, see notes there.
let result = spawn_blocking(move || {
use std::{
fs::{create_dir_all, File},
io::{BufWriter, Error},
Expand All @@ -201,8 +201,8 @@ async fn save_impl(path: PathBuf, cache: &ToolCache) -> AftmanResult<()> {
let writer = BufWriter::new(File::create(path)?);
serde_json::to_writer(writer, &json)?;
Ok::<_, Error>(())
})
.await??;
});

result.await??;
Ok(())
}

0 comments on commit d78708c

Please sign in to comment.