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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use crate::cell::*;
use crate::dict::*;
use crate::num::*;
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub struct StorageUsedShort {
pub cells: VarUint56,
pub bits: VarUint56,
}
impl StorageUsedShort {
pub const ZERO: Self = Self {
cells: VarUint56::ZERO,
bits: VarUint56::ZERO,
};
}
impl<C: CellFamily> Store<C> for StorageUsedShort {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
self.cells.store_into(builder, finalizer) && self.bits.store_into(builder, finalizer)
}
}
impl<'a, C: CellFamily> Load<'a, C> for StorageUsedShort {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(Self {
cells: VarUint56::load_from(slice)?,
bits: VarUint56::load_from(slice)?,
})
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum AccountStatus {
Uninit = 0b00,
Frozen = 0b01,
Active = 0b10,
NotExists = 0b11,
}
impl<C: CellFamily> Store<C> for AccountStatus {
#[inline]
fn store_into(&self, builder: &mut CellBuilder<C>, _: &mut dyn Finalizer<C>) -> bool {
builder.store_small_uint(*self as u8, 2)
}
}
impl<'a, C: CellFamily> Load<'a, C> for AccountStatus {
#[inline]
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let ty = slice.load_small_uint(2)?;
Some(match ty {
0b00 => Self::Uninit,
0b01 => Self::Frozen,
0b10 => Self::Active,
0b11 => Self::NotExists,
_ => return None,
})
}
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct StateInit<C: CellFamily> {
pub split_depth: Option<SplitDepth>,
pub special: Option<SpecialFlags>,
pub code: Option<CellContainer<C>>,
pub data: Option<CellContainer<C>>,
pub libraries: Dict<C, 256>,
}
impl<C: CellFamily> StateInit<C> {
pub const fn bit_len(&self) -> u16 {
(1 + self.split_depth.is_some() as u16 * SplitDepth::BITS)
+ (1 + self.special.is_some() as u16 * SpecialFlags::BITS)
+ 3
}
pub const fn reference_count(&self) -> u8 {
self.code.is_some() as u8 + self.data.is_some() as u8 + !self.libraries.is_empty() as u8
}
}
impl<C: CellFamily> Store<C> for StateInit<C> {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
self.split_depth.store_into(builder, finalizer)
&& self.special.store_into(builder, finalizer)
&& self.code.store_into(builder, finalizer)
&& self.data.store_into(builder, finalizer)
&& self.libraries.store_into(builder, finalizer)
}
}
impl<'a, C: CellFamily> Load<'a, C> for StateInit<C> {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(Self {
split_depth: Option::<SplitDepth>::load_from(slice)?,
special: Option::<SpecialFlags>::load_from(slice)?,
code: Option::<CellContainer<C>>::load_from(slice)?,
data: Option::<CellContainer<C>>::load_from(slice)?,
libraries: Dict::<C, 256>::load_from(slice)?,
})
}
}
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub struct SpecialFlags {
pub tick: bool,
pub tock: bool,
}
impl SpecialFlags {
pub const BITS: u16 = 2;
}
impl<C: CellFamily> Store<C> for SpecialFlags {
fn store_into(&self, builder: &mut CellBuilder<C>, _: &mut dyn Finalizer<C>) -> bool {
builder.store_small_uint(((self.tick as u8) << 1) | self.tock as u8, 2)
}
}
impl<'a, C: CellFamily> Load<'a, C> for SpecialFlags {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let data = slice.load_small_uint(2)?;
Some(Self {
tick: data & 0b10 != 0,
tock: data & 0b01 != 0,
})
}
}
#[derive(Default, Clone, Eq, PartialEq)]
pub struct CurrencyCollection<C: CellFamily> {
pub tokens: Tokens,
pub other: ExtraCurrencyCollection<C>,
}
impl<C: CellFamily> std::fmt::Debug for CurrencyCollection<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CurrencyCollection")
.field("tokens", &self.tokens)
.field("other", &self.other)
.finish()
}
}
impl<C: CellFamily> CurrencyCollection<C> {
pub const ZERO: Self = Self {
tokens: Tokens::ZERO,
other: ExtraCurrencyCollection::new(),
};
pub const fn new(tokens: u128) -> Self {
Self {
tokens: Tokens::new(tokens),
other: ExtraCurrencyCollection::new(),
}
}
pub const fn bit_len(&self) -> u16 {
self.tokens.unwrap_bit_len() + 1
}
}
impl<C: CellFamily> Store<C> for CurrencyCollection<C> {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
self.tokens.store_into(builder, finalizer) && self.other.store_into(builder, finalizer)
}
}
impl<'a, C: CellFamily> Load<'a, C> for CurrencyCollection<C> {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(Self {
tokens: Tokens::load_from(slice)?,
other: ExtraCurrencyCollection::<C>::load_from(slice)?,
})
}
}
#[derive(Default, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct ExtraCurrencyCollection<C: CellFamily>(Dict<C, 32>);
impl<C: CellFamily> std::fmt::Debug for ExtraCurrencyCollection<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ExtraCurrencyCollection")
.field(&self.0)
.finish()
}
}
impl<C: CellFamily> ExtraCurrencyCollection<C> {
pub const fn new() -> Self {
Self(Dict::new())
}
pub const fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<C: CellFamily> Store<C> for ExtraCurrencyCollection<C> {
#[inline]
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
self.0.store_into(builder, finalizer)
}
}
impl<'a, C: CellFamily> Load<'a, C> for ExtraCurrencyCollection<C> {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(Self(Dict::load_from(slice)?))
}
}