1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{RootStatus, Construct, Backend, ReadBackend, WriteBackend, Sequence, Raw, Dangling, Error, Index, Leak, Tree, Owned};

const LEN_INDEX: Index = Index::root().right();
const ITEM_ROOT_INDEX: Index = Index::root().left();

/// A tree with length mixed in.
pub struct LengthMixed<R: RootStatus, C: Construct, S: Sequence<Construct=C, RootStatus=Dangling>> {
    raw: Raw<R, C>,
    inner: S,
}

impl<R: RootStatus, C: Construct, S> LengthMixed<R, C, S> where
    S: Sequence<Construct=C, RootStatus=Dangling>,
    C::Value: From<usize> + Into<usize>,
{
    /// Reconstruct the mixed-length tree.
    pub fn reconstruct<DB: WriteBackend<Construct=C> + ?Sized, F>(
        root: C::Value,
        db: &mut DB,
        f: F
    ) -> Result<Self, Error<DB::Error>> where
        F: FnOnce(Raw<Dangling, C>, &mut DB, usize) -> Result<S, Error<DB::Error>>,
    {
        let raw = Raw::<R, C>::from_leaked(root);
        let len: usize = raw.get(db, LEN_INDEX)?
            .ok_or(Error::CorruptedDatabase)?
            .into();
        let inner_raw = raw.subtree(db, ITEM_ROOT_INDEX)?;

        let inner = f(inner_raw, db, len)?;
        Ok(Self { inner, raw })
    }

    /// Deconstruct the mixed-length tree.
    pub fn deconstruct<DB: ReadBackend<Construct=C> + ?Sized>(
        self,
        db: &mut DB
    ) -> Result<C::Value, Error<DB::Error>> {
        self.raw.get(db, LEN_INDEX)?;
        self.raw.get(db, ITEM_ROOT_INDEX)?;
        Ok(self.raw.root())
    }

    /// Call with the inner sequence.
    pub fn with<DB: Backend<Construct=C> + ?Sized, RT, F>(
        &self,
        db: &mut DB,
        f: F
    ) -> Result<RT, Error<DB::Error>> where
        F: FnOnce(&S, &mut DB) -> Result<RT, Error<DB::Error>>
    {
        f(&self.inner, db)
    }

    /// Call with a mutable reference to the inner sequence.
    pub fn with_mut<DB: WriteBackend<Construct=C> + ?Sized, RT, F>(
        &mut self,
        db: &mut DB,
        f: F
    ) -> Result<RT, Error<DB::Error>> where
        F: FnOnce(&mut S, &mut DB) -> Result<RT, Error<DB::Error>>
    {
        let ret = f(&mut self.inner, db)?;
        let new_len = self.inner.len();
        let new_inner_root = self.inner.root();

        self.raw.set(db, ITEM_ROOT_INDEX, new_inner_root)?;
        self.raw.set(db, LEN_INDEX, new_len.into())?;

        Ok(ret)
    }
}

impl<C: Construct, S> LengthMixed<Owned, C, S> where
    S: Sequence<Construct=C, RootStatus=Dangling> + Leak,
    C::Value: From<usize> + Into<usize>,
{
    /// Create a new mixed-length tree.
    pub fn create<DB: WriteBackend<Construct=C> + ?Sized, OS, F>(
        db: &mut DB,
        f: F
    ) -> Result<Self, Error<DB::Error>> where
        F: FnOnce(&mut DB) -> Result<OS, Error<DB::Error>>,
        OS: Sequence<Construct=C> + Leak<Metadata=S::Metadata>,
    {
        let inner = f(db)?;
        let len = inner.len();
        let mut raw = Raw::default();

        raw.set(db, ITEM_ROOT_INDEX, inner.root())?;
        raw.set(db, LEN_INDEX, len.into())?;
        let metadata = inner.metadata();
        inner.drop(db)?;
        let dangling_inner = S::from_leaked(metadata);

        Ok(Self { raw, inner: dangling_inner })
    }
}

impl<R: RootStatus, C: Construct, S> Tree for LengthMixed<R, C, S> where
    S: Sequence<Construct=C, RootStatus=Dangling>,
{
    type RootStatus = R;
    type Construct = C;

    fn root(&self) -> C::Value {
        self.raw.root()
    }

    fn drop<DB: WriteBackend<Construct=C> + ?Sized>(
        self,
        db: &mut DB
    ) -> Result<(), Error<DB::Error>> {
        self.inner.drop(db)?;
        self.raw.drop(db)?;
        Ok(())
    }

    fn into_raw(self) -> Raw<R, C> {
        self.raw
    }
}

impl<R: RootStatus, C: Construct, S> Sequence for LengthMixed<R, C, S> where
    S: Sequence<Construct=C, RootStatus=Dangling>,
{
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<R: RootStatus, C: Construct, S> Leak for LengthMixed<R, C, S> where
    S: Sequence<Construct=C, RootStatus=Dangling> + Leak,
{
    type Metadata = (C::Value, S::Metadata);

    fn metadata(&self) -> Self::Metadata {
        let inner_metadata = self.inner.metadata();
        let raw_metadata = self.raw.metadata();

        (raw_metadata, inner_metadata)
    }

    fn from_leaked((raw_metadata, inner_metadata): Self::Metadata) -> Self {
        Self {
            raw: Raw::from_leaked(raw_metadata),
            inner: S::from_leaked(inner_metadata),
        }
    }
}