mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-01 05:29:05 +02:00
io: add storage helper
Use bincode+zstd to serialize data structures for temporary storage.
This commit is contained in:
parent
f47b38b2ca
commit
21fd3a42ca
4 changed files with 84 additions and 0 deletions
|
@ -1,2 +1,3 @@
|
|||
pub mod data;
|
||||
pub mod region;
|
||||
pub mod storage;
|
||||
|
|
22
src/io/storage.rs
Normal file
22
src/io/storage.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{BufWriter, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Serialize;
|
||||
|
||||
pub fn write<T: Serialize>(path: &Path, value: &T) -> Result<()> {
|
||||
(|| -> Result<()> {
|
||||
let file = File::create(path)?;
|
||||
let writer = BufWriter::new(file);
|
||||
let mut compressor = zstd::Encoder::new(writer, 1)?;
|
||||
bincode::serialize_into(&mut compressor, value)?;
|
||||
let writer = compressor.finish()?;
|
||||
let mut file = writer.into_inner()?;
|
||||
file.flush()?;
|
||||
Ok(())
|
||||
})()
|
||||
.with_context(|| format!("Failed to write file {}", path.display()))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue