use ic_storage::{
Memory, RuntimeMemory,
adapter::{Bound, MapEntry, StableBTreeMap, Storable},
};
use std::{
borrow::Cow,
ops::{Deref, RangeBounds},
};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct RawKey(pub Vec<u8>);
impl Deref for RawKey {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.0
}
}
impl Storable for RawKey {
const BOUND: Bound = Bound::Bounded {
max_size: 512,
is_fixed_size: false,
};
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(&self.0)
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
Self(bytes.into_owned())
}
}
pub struct RawMap<M: Memory>(StableBTreeMap<RawKey, Vec<u8>, M>);
impl<M: Memory> RawMap<M> {
#[must_use]
pub fn load(memory: RuntimeMemory<M>) -> Self {
Self(StableBTreeMap::load(memory))
}
pub fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.0.get(&RawKey(key.to_vec()))
}
pub fn insert(&mut self, key: Vec<u8>, value: Vec<u8>) -> Option<Vec<u8>> {
self.0.insert(RawKey(key), value)
}
pub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>> {
self.0.remove(&RawKey(key.to_vec()))
}
pub fn iter(&self) -> impl Iterator<Item = MapEntry<RawKey, Vec<u8>>> {
self.0.iter()
}
pub fn range(
&self,
bounds: impl RangeBounds<Vec<u8>>,
) -> impl Iterator<Item = MapEntry<RawKey, Vec<u8>>> {
self.0.range((
bounds.start_bound().cloned().map(RawKey),
bounds.end_bound().cloned().map(RawKey),
))
}
pub fn keys(&self) -> impl Iterator<Item = Vec<u8>> {
self.0.keys().map(|key| key.0)
}
}