use std::sync::OnceLock;
use serde::de::DeserializeOwned;
pub fn decompress_deflate(compressed_data: &[u8]) -> Vec<u8> {
miniz_oxide::inflate::decompress_to_vec(compressed_data).expect("Failed to decompress data")
}
pub fn load<T: DeserializeOwned>(compressed: &[u8]) -> T {
postcard::from_bytes(&decompress_deflate(compressed)).unwrap()
}
pub struct LazyData<T> {
compressed: &'static [u8],
cell: OnceLock<T>,
}
impl<T: DeserializeOwned> LazyData<T> {
pub const fn new(compressed: &'static [u8]) -> Self {
Self { compressed, cell: OnceLock::new() }
}
pub fn get(&self) -> &T {
self.cell.get_or_init(|| load(self.compressed))
}
}
pub struct LazyBlob {
compressed: &'static [u8],
cell: OnceLock<Vec<u8>>,
}
impl LazyBlob {
pub const fn new(compressed: &'static [u8]) -> Self {
Self { compressed, cell: OnceLock::new() }
}
pub fn get(&self) -> &[u8] {
self.cell.get_or_init(|| decompress_deflate(self.compressed))
}
}