pub mod refimpl;
pub mod roller;
use crate::{
value::{Addr, Key, Value},
Error,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub(crate) const ONE_LEN_BLOCK_WARNING: &str =
"writing key & value that exceeds block size, this is highly inefficient";
pub type NodeOwned = Node<Key, Value, Addr>;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Node<Key, Value, Addr> {
Branch(Vec<(Key, Addr)>),
Leaf(Vec<(Key, Value)>),
}
impl<K, V, A> Node<K, V, A> {
pub fn key(&self) -> Option<&K> {
match self {
Self::Branch(v) => v.get(0).map(|(k, _)| k),
Self::Leaf(v) => v.get(0).map(|(k, _)| k),
}
}
pub fn into_key(self) -> Option<K> {
match self {
Self::Branch(mut v) => {
if v.is_empty() {
None
} else {
Some(v.swap_remove(0).0)
}
},
Self::Leaf(mut v) => {
if v.is_empty() {
None
} else {
Some(v.swap_remove(0).0)
}
},
}
}
pub fn into_key_unchecked(self) -> K {
match self {
Self::Branch(mut v) => v.swap_remove(0).0,
Self::Leaf(mut v) => v.swap_remove(0).0,
}
}
pub fn len(&self) -> usize {
match self {
Self::Branch(v) => v.len(),
Self::Leaf(v) => v.len(),
}
}
pub fn is_empty(&self) -> bool {
match self {
Self::Branch(v) => v.is_empty(),
Self::Leaf(v) => v.is_empty(),
}
}
}
#[cfg(feature = "borsh")]
impl<K, V, A> Node<K, V, A>
where
K: borsh::BorshSerialize,
V: borsh::BorshSerialize,
A: borsh::BorshSerialize,
{
pub fn as_bytes(&self) -> Result<(Addr, Vec<u8>), Error> {
let bytes = crate::value::serialize(self)?;
let addr = Addr::hash(&bytes);
Ok((addr, bytes))
}
}