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
use std::marker::PhantomData;
use crate::cell::{
CellBuilder, CellContainer, CellFamily, CellSlice, DefaultFinalizer, Finalizer, Load, Store,
};
use crate::util::*;
pub use account::*;
pub use block::*;
pub use config::*;
pub use global_version::*;
pub use message::*;
pub use transaction::*;
pub mod account;
pub mod block;
pub mod config;
pub mod currency;
pub mod global_version;
pub mod message;
pub mod shard;
pub mod transaction;
pub struct Lazy<C: CellFamily, T> {
cell: CellContainer<C>,
_marker: PhantomData<T>,
}
impl<C: CellFamily, T> std::fmt::Debug for Lazy<C, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug_tuple_field1_finish(f, "Lazy", &self.cell)
}
}
impl<C: CellFamily, T> Eq for Lazy<C, T> {}
impl<C: CellFamily, T> PartialEq for Lazy<C, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.cell.as_ref().eq(other.cell.as_ref())
}
}
impl<C: CellFamily, T> Clone for Lazy<C, T> {
#[inline]
fn clone(&self) -> Self {
Self {
cell: self.cell.clone(),
_marker: PhantomData,
}
}
}
impl<C: CellFamily, T> Lazy<C, T> {
#[inline]
pub fn from_raw(cell: CellContainer<C>) -> Self {
Self {
cell,
_marker: PhantomData,
}
}
#[inline]
pub fn into_inner(self) -> CellContainer<C> {
self.cell
}
#[inline]
pub fn inner(&self) -> &CellContainer<C> {
&self.cell
}
}
impl<C: DefaultFinalizer, T: Store<C>> Lazy<C, T> {
pub fn new(data: &T) -> Option<Self> {
let mut builder = CellBuilder::<C>::new();
let finalizer = &mut C::default_finalizer();
if !data.store_into(&mut builder, finalizer) {
return None;
}
Some(Self::from_raw(builder.build_ext(finalizer)?))
}
}
impl<'a, C: CellFamily, T: Load<'a, C> + 'a> Lazy<C, T> {
pub fn load(&'a self) -> Option<T> {
T::load_from(&mut self.cell.as_ref().as_slice())
}
}
impl<C: CellFamily, T> Store<C> for Lazy<C, T> {
fn store_into(&self, builder: &mut CellBuilder<C>, _: &mut dyn Finalizer<C>) -> bool {
builder.store_reference(self.cell.clone())
}
}
impl<'a, C: CellFamily, T> Load<'a, C> for Lazy<C, T> {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(Self {
cell: slice.load_reference_cloned()?,
_marker: PhantomData,
})
}
}