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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::hint::unreachable_unchecked;
use core::marker::PhantomData;

use rkyv::{ser::Serializer, Archive, Fallible, Serialize};

#[cfg(feature = "host")]
mod host_store;
#[cfg(feature = "host")]
pub use host_store::HostStore;

use crate::{
    Annotation, ArchivedCompound, Branch, Compound, MaybeArchived, Walker,
};

/// Offset based identifier
#[derive(Debug, Clone, Copy)]
pub struct Offset(u64);

/// An identifier representing a value stored somewhere else
pub struct Ident<I, T> {
    id: I,
    _marker: PhantomData<T>,
}

impl<I, T> Clone for Ident<I, T>
where
    I: Copy,
{
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            _marker: PhantomData,
        }
    }
}

impl<I, T> Copy for Ident<I, T> where I: Copy {}

impl<I, T> Ident<I, T> {
    /// Creates a typed identifier
    pub(crate) fn new(id: I) -> Self {
        Ident {
            id,
            _marker: PhantomData,
        }
    }

    /// Returns an untyped identifier
    pub fn erase(self) -> I {
        self.id
    }
}

/// Stored is a reference to a value stored, along with the backing store
#[derive(Clone)]
pub struct Stored<T, S>
where
    S: Store,
{
    store: S,
    ident: Ident<S::Identifier, T>,
}

unsafe impl<T, S> Send for Stored<T, S> where S: Store + Send {}
unsafe impl<T, S> Sync for Stored<T, S> where S: Store + Sync {}

impl<T, S> Stored<T, S>
where
    S: Store,
{
    pub(crate) fn new(store: S, ident: Ident<S::Identifier, T>) -> Self {
        Stored { store, ident }
    }

    /// Get a reference to the backing Store
    pub fn store(&self) -> &S {
        &self.store
    }

    /// Get a reference to the Identifier of the stored value
    pub fn ident(&self) -> &Ident<S::Identifier, T> {
        &self.ident
    }

    /// Get a reference to the inner value being stored
    pub fn inner(&self) -> &T::Archived
    where
        T: Archive,
    {
        self.store.get_raw(&self.ident)
    }

    /// Start a branch walk using the stored `T` as the root.  
    pub fn walk<W, A>(&self, walker: W) -> Option<Branch<T, A, S>>
    where
        S: Store,
        T: Compound<A, S>,
        T::Archived: ArchivedCompound<T, A, S>,
        T::Leaf: Archive,
        A: Annotation<T::Leaf>,
        W: Walker<T, A, S>,
    {
        let inner = self.inner();
        Branch::walk_with_store(
            MaybeArchived::Archived(inner),
            walker,
            self.store().clone(),
        )
    }
}

/// A type that works as a handle to a `Storage` backend.
pub trait Store: Clone + Fallible<Error = core::convert::Infallible> {
    /// The identifier used for refering to stored values
    type Identifier: Copy;
    /// The underlying storage
    type Storage: Storage<Self::Identifier>;

    /// Put a value into storage, and get a representative token back
    fn put<T>(&self, t: &T) -> Stored<T, Self>
    where
        T: Serialize<Self::Storage>;

    /// Gets a reference to an archived value
    fn get_raw<'a, T>(
        &'a self,
        ident: &Ident<Self::Identifier, T>,
    ) -> &'a T::Archived
    where
        T: Archive;
}

/// The main trait for providing storage backends to use with `microkelvin`
pub trait Storage<I>:
    Serializer + Fallible<Error = core::convert::Infallible>
{
    /// Write a value into the storage, returns a representation
    fn put<T>(&mut self, t: &T) -> I
    where
        T: Serialize<Self>;

    /// Gets a value from the store
    fn get<T>(&self, id: &I) -> &T::Archived
    where
        T: Archive;
}

pub trait UnwrapInfallible<T> {
    fn unwrap_infallible(self) -> T;
}

impl<T> UnwrapInfallible<T> for Result<T, core::convert::Infallible> {
    fn unwrap_infallible(self) -> T {
        match self {
            Ok(t) => t,
            Err(_) => unsafe {
                // safe, since the error type cannot be instantiated
                unreachable_unchecked()
            },
        }
    }
}