1
1
use indexmap:: { IndexMap , IndexSet } ;
2
+ use thin_vec:: ThinVec ;
2
3
3
4
use crate :: { Context , JsBigInt , JsObject , JsString , JsSymbol } ;
4
5
5
- use super :: { Header , Snapshot , SnapshotError } ;
6
+ use super :: { Header , Snapshot , SnapshotError , SnapshotResult } ;
6
7
7
8
/// TODO: doc
8
9
pub trait Serialize {
9
10
/// Serialize type
10
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > ;
11
- }
12
-
13
- impl Serialize for Header {
14
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > {
15
- s. write_bytes ( & self . signature ) ?;
16
- s. write_u32 ( self . version ) ?;
17
- Ok ( ( ) )
18
- }
11
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > ;
19
12
}
20
13
21
14
/// TODO: doc
@@ -106,85 +99,201 @@ impl SnapshotSerializer {
106
99
}
107
100
108
101
/// TODO: doc
109
- pub fn write_bool ( & mut self , v : bool ) -> Result < ( ) , SnapshotError > {
102
+ pub fn write_bool ( & mut self , v : bool ) -> SnapshotResult < ( ) > {
110
103
Ok ( self . write_u8 ( if v { 1 } else { 0 } ) ?)
111
104
}
112
105
/// TODO: doc
113
- pub fn write_u8 ( & mut self , v : u8 ) -> Result < ( ) , SnapshotError > {
106
+ pub fn write_u8 ( & mut self , v : u8 ) -> SnapshotResult < ( ) > {
114
107
Ok ( self . write_bytes ( & [ v] ) ?)
115
108
}
116
109
/// TODO: doc
117
- pub fn write_i8 ( & mut self , v : i8 ) -> Result < ( ) , SnapshotError > {
110
+ pub fn write_i8 ( & mut self , v : i8 ) -> SnapshotResult < ( ) > {
118
111
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
119
112
}
120
113
121
114
/// TODO: doc
122
- pub fn write_u16 ( & mut self , v : u16 ) -> Result < ( ) , SnapshotError > {
115
+ pub fn write_u16 ( & mut self , v : u16 ) -> SnapshotResult < ( ) > {
123
116
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
124
117
}
125
118
/// TODO: doc
126
- pub fn write_i16 ( & mut self , v : i16 ) -> Result < ( ) , SnapshotError > {
119
+ pub fn write_i16 ( & mut self , v : i16 ) -> SnapshotResult < ( ) > {
127
120
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
128
121
}
129
122
130
123
/// TODO: doc
131
- pub fn write_u32 ( & mut self , v : u32 ) -> Result < ( ) , SnapshotError > {
124
+ pub fn write_u32 ( & mut self , v : u32 ) -> SnapshotResult < ( ) > {
132
125
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
133
126
}
134
127
/// TODO: doc
135
- pub fn write_i32 ( & mut self , v : i32 ) -> Result < ( ) , SnapshotError > {
128
+ pub fn write_i32 ( & mut self , v : i32 ) -> SnapshotResult < ( ) > {
136
129
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
137
130
}
138
131
139
132
/// TODO: doc
140
- pub fn write_f32 ( & mut self , v : f32 ) -> Result < ( ) , SnapshotError > {
133
+ pub fn write_f32 ( & mut self , v : f32 ) -> SnapshotResult < ( ) > {
141
134
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
142
135
}
143
136
/// TODO: doc
144
- pub fn write_f64 ( & mut self , v : f64 ) -> Result < ( ) , SnapshotError > {
137
+ pub fn write_f64 ( & mut self , v : f64 ) -> SnapshotResult < ( ) > {
145
138
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
146
139
}
147
140
148
141
/// TODO: doc
149
- pub fn write_u64 ( & mut self , v : u64 ) -> Result < ( ) , SnapshotError > {
142
+ pub fn write_u64 ( & mut self , v : u64 ) -> SnapshotResult < ( ) > {
150
143
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
151
144
}
152
145
/// TODO: doc
153
- pub fn write_i64 ( & mut self , v : i64 ) -> Result < ( ) , SnapshotError > {
146
+ pub fn write_i64 ( & mut self , v : i64 ) -> SnapshotResult < ( ) > {
154
147
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
155
148
}
156
149
/// TODO: doc
157
- pub fn write_u128 ( & mut self , v : u128 ) -> Result < ( ) , SnapshotError > {
150
+ pub fn write_u128 ( & mut self , v : u128 ) -> SnapshotResult < ( ) > {
158
151
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
159
152
}
160
153
/// TODO: doc
161
- pub fn write_i128 ( & mut self , v : i128 ) -> Result < ( ) , SnapshotError > {
154
+ pub fn write_i128 ( & mut self , v : i128 ) -> SnapshotResult < ( ) > {
162
155
Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
163
156
}
164
157
/// TODO: doc
165
- pub fn write_usize ( & mut self , v : usize ) -> Result < ( ) , SnapshotError > {
158
+ pub fn write_usize ( & mut self , v : usize ) -> SnapshotResult < ( ) > {
166
159
Ok ( self . write_bytes ( & ( v as u64 ) . to_le_bytes ( ) ) ?)
167
160
}
168
161
/// TODO: doc
169
- pub fn write_isize ( & mut self , v : isize ) -> Result < ( ) , SnapshotError > {
162
+ pub fn write_isize ( & mut self , v : isize ) -> SnapshotResult < ( ) > {
170
163
Ok ( self . write_bytes ( & ( v as i64 ) . to_le_bytes ( ) ) ?)
171
164
}
172
165
/// TODO: doc
173
- pub fn write_string ( & mut self , v : & str ) -> Result < ( ) , SnapshotError > {
166
+ pub fn write_string ( & mut self , v : & str ) -> SnapshotResult < ( ) > {
174
167
let asb = v. as_bytes ( ) ;
175
168
self . write_usize ( asb. len ( ) ) ?;
176
169
self . bytes . extend_from_slice ( asb) ;
177
170
Ok ( ( ) )
178
171
}
179
172
/// TODO: doc
180
- pub fn write_bytes ( & mut self , v : & [ u8 ] ) -> Result < ( ) , SnapshotError > {
173
+ pub fn write_bytes ( & mut self , v : & [ u8 ] ) -> SnapshotResult < ( ) > {
181
174
self . bytes . extend_from_slice ( v) ;
182
175
Ok ( ( ) )
183
176
}
184
177
}
185
178
179
+ impl Serialize for bool {
180
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
181
+ s. write_bool ( * self )
182
+ }
183
+ }
184
+
185
+ impl Serialize for u8 {
186
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
187
+ s. write_u8 ( * self )
188
+ }
189
+ }
190
+
191
+ impl Serialize for i8 {
192
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
193
+ s. write_i8 ( * self )
194
+ }
195
+ }
196
+
197
+ impl Serialize for u16 {
198
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
199
+ s. write_u16 ( * self )
200
+ }
201
+ }
202
+
203
+ impl Serialize for i16 {
204
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
205
+ s. write_i16 ( * self )
206
+ }
207
+ }
208
+
209
+ impl Serialize for u32 {
210
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
211
+ s. write_u32 ( * self )
212
+ }
213
+ }
214
+
215
+ impl Serialize for i32 {
216
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
217
+ s. write_i32 ( * self )
218
+ }
219
+ }
220
+
221
+ impl Serialize for u64 {
222
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
223
+ s. write_u64 ( * self )
224
+ }
225
+ }
226
+
227
+ impl Serialize for i64 {
228
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
229
+ s. write_i64 ( * self )
230
+ }
231
+ }
232
+
233
+ impl Serialize for usize {
234
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
235
+ s. write_usize ( * self )
236
+ }
237
+ }
238
+
239
+ impl Serialize for isize {
240
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
241
+ s. write_isize ( * self )
242
+ }
243
+ }
244
+
245
+ impl Serialize for f32 {
246
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
247
+ s. write_f32 ( * self )
248
+ }
249
+ }
250
+
251
+ impl Serialize for f64 {
252
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
253
+ s. write_f64 ( * self )
254
+ }
255
+ }
256
+
257
+ impl < T : Serialize > Serialize for Option < T > {
258
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
259
+ if let Some ( value) = self {
260
+ s. write_bool ( true ) ?;
261
+ value. serialize ( s) ?
262
+ } else {
263
+ s. write_bool ( false ) ?;
264
+ }
265
+ Ok ( ( ) )
266
+ }
267
+ }
268
+
269
+ impl < T : Serialize , E : Serialize > Serialize for Result < T , E > {
270
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
271
+ match self {
272
+ Ok ( value) => {
273
+ s. write_bool ( true ) ?;
274
+ value. serialize ( s) ?;
275
+ }
276
+ Err ( err) => {
277
+ s. write_bool ( false ) ?;
278
+ err. serialize ( s) ?;
279
+ }
280
+ }
281
+ Ok ( ( ) )
282
+ }
283
+ }
284
+
186
285
impl < T : Serialize > Serialize for Vec < T > {
187
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > {
286
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
287
+ s. write_usize ( self . len ( ) ) ?;
288
+ for element in self {
289
+ element. serialize ( s) ?;
290
+ }
291
+ Ok ( ( ) )
292
+ }
293
+ }
294
+
295
+ impl < T : Serialize > Serialize for ThinVec < T > {
296
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
188
297
s. write_usize ( self . len ( ) ) ?;
189
298
for element in self {
190
299
element. serialize ( s) ?;
@@ -194,7 +303,7 @@ impl<T: Serialize> Serialize for Vec<T> {
194
303
}
195
304
196
305
impl Serialize for JsString {
197
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > {
306
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
198
307
let index = s. strings . insert_full ( self . ptr . addr ( ) , self . clone ( ) ) . 0 ;
199
308
200
309
s. write_u32 ( index as u32 ) ?;
@@ -203,26 +312,24 @@ impl Serialize for JsString {
203
312
}
204
313
205
314
impl Serialize for JsSymbol {
206
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > {
315
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
207
316
let index = s. symbols . insert_full ( self . hash ( ) , self . clone ( ) ) . 0 ;
208
-
209
317
s. write_u32 ( index as u32 ) ?;
210
318
Ok ( ( ) )
211
319
}
212
320
}
213
321
214
322
impl Serialize for JsBigInt {
215
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > {
323
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
216
324
let index = s. bigints . insert_full ( self . clone ( ) ) . 0 ;
217
325
s. write_u32 ( index as u32 ) ?;
218
326
Ok ( ( ) )
219
327
}
220
328
}
221
329
222
330
impl Serialize for JsObject {
223
- fn serialize ( & self , s : & mut SnapshotSerializer ) -> Result < ( ) , SnapshotError > {
331
+ fn serialize ( & self , s : & mut SnapshotSerializer ) -> SnapshotResult < ( ) > {
224
332
let value = s. objects . insert_full ( self . clone ( ) ) . 0 ;
225
-
226
333
s. write_u32 ( value as u32 ) ?;
227
334
Ok ( ( ) )
228
335
}
0 commit comments