pub trait Construct: Sized {
type Value: Clone + Default;
fn intermediate_of(left: &Self::Value, right: &Self::Value) -> Self::Value;
fn empty_at<DB: WriteBackend<Construct=Self> + ?Sized>(
db: &mut DB,
depth_to_bottom: usize
) -> Result<Self::Value, DB::Error>;
}
pub trait Tree {
type RootStatus: RootStatus;
type Construct: Construct;
fn root(&self) -> <Self::Construct as Construct>::Value;
fn drop<DB: WriteBackend<Construct=Self::Construct> + ?Sized>(
self,
db: &mut DB
) -> Result<(), Error<DB::Error>>;
fn into_raw(self) -> crate::Raw<Self::RootStatus, Self::Construct>;
}
pub trait Sequence: Tree {
fn len(&self) -> usize;
}
pub trait RootStatus {
fn is_dangling() -> bool;
fn is_owned() -> bool { !Self::is_dangling() }
}
pub struct Dangling;
impl RootStatus for Dangling {
fn is_dangling() -> bool { true }
}
pub struct Owned;
impl RootStatus for Owned {
fn is_dangling() -> bool { false }
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Error<DBError> {
CorruptedDatabase,
AccessOverflowed,
InvalidParameter,
Backend(DBError),
}
impl<DBError> From<DBError> for Error<DBError> {
fn from(err: DBError) -> Self {
Error::Backend(err)
}
}
pub trait Backend {
type Construct: Construct;
type Error;
}
pub trait ReadBackend: Backend {
fn get(
&mut self,
key: &<Self::Construct as Construct>::Value,
) -> Result<Option<(<Self::Construct as Construct>::Value, <Self::Construct as Construct>::Value)>, Self::Error>;
}
pub trait WriteBackend: ReadBackend {
fn rootify(
&mut self,
key: &<Self::Construct as Construct>::Value,
) -> Result<(), Self::Error>;
fn unrootify(
&mut self,
key: &<Self::Construct as Construct>::Value,
) -> Result<(), Self::Error>;
fn insert(
&mut self,
key: <Self::Construct as Construct>::Value,
value: (<Self::Construct as Construct>::Value, <Self::Construct as Construct>::Value)
) -> Result<(), Self::Error>;
}
#[derive(Default, Clone, Debug)]
pub struct DynBackend<Ba: Backend>(pub Ba);
impl<Ba: Backend> core::ops::Deref for DynBackend<Ba> {
type Target = Ba;
fn deref(&self) -> &Ba { &self.0 }
}
impl<Ba: Backend> core::ops::DerefMut for DynBackend<Ba> {
fn deref_mut(&mut self) -> &mut Ba { &mut self.0 }
}
impl<Ba: Backend> Backend for DynBackend<Ba> {
type Construct = Ba::Construct;
type Error = ();
}
impl<Ba: ReadBackend> ReadBackend for DynBackend<Ba> {
fn get(
&mut self,
key: &<Self::Construct as Construct>::Value,
) -> Result<Option<(<Self::Construct as Construct>::Value, <Self::Construct as Construct>::Value)>, Self::Error> {
self.0.get(key).map_err(|_| ())
}
}
impl<Ba: WriteBackend> WriteBackend for DynBackend<Ba> {
fn rootify(
&mut self,
key: &<Self::Construct as Construct>::Value,
) -> Result<(), Self::Error> {
self.0.rootify(key).map_err(|_| ())
}
fn unrootify(
&mut self,
key: &<Self::Construct as Construct>::Value,
) -> Result<(), Self::Error> {
self.0.unrootify(key).map_err(|_| ())
}
fn insert(
&mut self,
key: <Self::Construct as Construct>::Value,
value: (<Self::Construct as Construct>::Value, <Self::Construct as Construct>::Value)
) -> Result<(), Self::Error> {
self.0.insert(key, value).map_err(|_| ())
}
}
pub trait Leak {
type Metadata;
fn from_leaked(metadata: Self::Metadata) -> Self;
fn metadata(&self) -> Self::Metadata;
}