mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-03 14:19:07 +02:00
io/storage: use fs::create_with_tmpfile() helper
This commit is contained in:
parent
1abb260997
commit
587db0464c
2 changed files with 5 additions and 17 deletions
|
@ -7,21 +7,20 @@ use std::{
|
|||
use anyhow::{Context, Result};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
use super::fs;
|
||||
|
||||
pub fn write<T: Serialize>(path: &Path, value: &T) -> Result<()> {
|
||||
(|| -> Result<()> {
|
||||
fs::create_with_tmpfile(path, |file| {
|
||||
let data = bincode::serialize(value)?;
|
||||
let len = u32::try_from(data.len())?;
|
||||
let compressed = zstd::bulk::compress(&data, 1)?;
|
||||
drop(data);
|
||||
|
||||
let mut file = File::create(path)?;
|
||||
file.write_all(&len.to_be_bytes())?;
|
||||
file.write_all(&compressed)?;
|
||||
file.flush()?;
|
||||
|
||||
Ok(())
|
||||
})()
|
||||
.with_context(|| format!("Failed to write file {}", path.display()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read<T: DeserializeOwned>(path: &Path) -> Result<T> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue