use crate::error::{EncodingDecodingError, NetabaseError};
use bincode::{config::standard, decode_from_slice, encode_to_vec};
pub trait ToIVec: bincode::Encode + bincode::Decode<()> + Sized {
#[cfg(all(feature = "sled", not(feature = "wasm")))]
fn to_ivec(&self) -> Result<sled::IVec, NetabaseError> {
let bytes = encode_to_vec(self, standard()).map_err(EncodingDecodingError::from)?;
Ok(sled::IVec::from(bytes))
}
#[cfg(all(feature = "sled", not(feature = "wasm")))]
fn from_ivec(ivec: &sled::IVec) -> Result<Self, NetabaseError> {
let (decoded, _) =
decode_from_slice(&ivec[..], standard()).map_err(EncodingDecodingError::from)?;
Ok(decoded)
}
#[cfg(all(feature = "wasm", not(feature = "sled")))]
fn to_vec(&self) -> Result<Vec<u8>, NetabaseError> {
let bytes = encode_to_vec(self, standard()).map_err(EncodingDecodingError::from)?;
Ok(bytes)
}
#[cfg(all(feature = "wasm", not(feature = "sled")))]
fn from_vec(bytes: &[u8]) -> Result<Self, NetabaseError> {
let (decoded, _) =
decode_from_slice(bytes, standard()).map_err(EncodingDecodingError::from)?;
Ok(decoded)
}
#[cfg(all(feature = "sled", feature = "wasm"))]
fn to_ivec(&self) -> Result<sled::IVec, NetabaseError> {
let bytes = encode_to_vec(self, standard()).map_err(EncodingDecodingError::from)?;
Ok(sled::IVec::from(bytes))
}
#[cfg(all(feature = "sled", feature = "wasm"))]
fn from_ivec(ivec: &sled::IVec) -> Result<Self, NetabaseError> {
let (decoded, _) =
decode_from_slice(&ivec[..], standard()).map_err(EncodingDecodingError::from)?;
Ok(decoded)
}
}