mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
io/storage: add read() function
This commit is contained in:
parent
dda81546de
commit
657d8af8a7
1 changed files with 12 additions and 2 deletions
|
@ -1,11 +1,11 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{BufWriter, Write},
|
||||
io::{BufReader, BufWriter, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Serialize;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
pub fn write<T: Serialize>(path: &Path, value: &T) -> Result<()> {
|
||||
(|| -> Result<()> {
|
||||
|
@ -20,3 +20,13 @@ pub fn write<T: Serialize>(path: &Path, value: &T) -> Result<()> {
|
|||
})()
|
||||
.with_context(|| format!("Failed to write file {}", path.display()))
|
||||
}
|
||||
|
||||
pub fn read<T: DeserializeOwned>(path: &Path) -> Result<T> {
|
||||
(|| -> Result<T> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let decompressor = zstd::Decoder::new(reader)?;
|
||||
Ok(bincode::deserialize_from(decompressor)?)
|
||||
})()
|
||||
.with_context(|| format!("Failed to read file {}", path.display()))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue