atomic_store/
load_store.rsuse crate::error::{BincodeDeSnafu, BincodeSerSnafu, PersistenceError};
use crate::storage_location::StorageLocation;
use crate::Result;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use serde::{de::DeserializeOwned, Serialize};
use snafu::ResultExt;
use std::marker::PhantomData;
pub trait LoadStore {
type ParamType;
fn load(&self, stream: &[u8]) -> Result<Self::ParamType>;
fn store(&mut self, param: &Self::ParamType) -> Result<Vec<u8>>;
}
#[derive(Debug)]
pub struct BincodeLoadStore<ParamType: Serialize + DeserializeOwned> {
phantom: PhantomData<ParamType>,
}
impl<ParamType: Serialize + DeserializeOwned> LoadStore for BincodeLoadStore<ParamType> {
type ParamType = ParamType;
fn load(&self, stream: &[u8]) -> Result<Self::ParamType> {
bincode::deserialize(stream).context(BincodeDeSnafu)
}
fn store(&mut self, param: &Self::ParamType) -> Result<Vec<u8>> {
bincode::serialize(param).context(BincodeSerSnafu)
}
}
impl<ParamType: Serialize + DeserializeOwned> Default for BincodeLoadStore<ParamType> {
fn default() -> Self {
BincodeLoadStore {
phantom: PhantomData,
}
}
}
#[derive(Debug)]
pub struct ArkLoadStore<ParamType: CanonicalSerialize + CanonicalDeserialize> {
phantom: PhantomData<ParamType>,
}
impl<ParamType: CanonicalSerialize + CanonicalDeserialize> LoadStore for ArkLoadStore<ParamType> {
type ParamType = ParamType;
fn load(&self, stream: &[u8]) -> Result<Self::ParamType> {
Self::ParamType::deserialize_compressed(stream)
.map_err(|err| PersistenceError::ArkDe { err })
}
fn store(&mut self, param: &Self::ParamType) -> Result<Vec<u8>> {
let mut ser_bytes: Vec<u8> = Vec::new();
param
.serialize_compressed(&mut ser_bytes)
.map_err(|err| PersistenceError::ArkSer { err })?;
Ok(ser_bytes)
}
}
impl<ParamType: CanonicalSerialize + CanonicalDeserialize> Default for ArkLoadStore<ParamType> {
fn default() -> Self {
ArkLoadStore {
phantom: PhantomData,
}
}
}
pub type StorageLocationLoadStore = BincodeLoadStore<StorageLocation>;