@@ -76,14 +76,12 @@ These are the main limitations you should be aware of before choosing to use
76
76
structure you will need to couple permanently the deserialized structure with
77
77
its serialized support, which is obtained by putting it in a [ ` MemCase ` ] using
78
78
the convenience methods [ ` Deserialize::load_mem ` ] , [ ` Deserialize::load_mmap ` ] ,
79
- and [ ` Deserialize::mmap ` ] . A [ ` MemCase ` ] will deref to its contained type, so it
80
- can be used transparently as long as fields and methods are concerned, but if
81
- your original type is ` T ` the field of the new structure will have to be of type
82
- ` MemCase<DeserType<'static, T>> ` , not ` T ` .
79
+ and [ ` Deserialize::mmap ` ] . A [ ` MemCase ` ] provides a method that yields references
80
+ to the deserialized type associated to its contained type.
83
81
84
82
- No validation or padding cleaning is performed on zero-copy types. If you plan
85
83
to serialize data and distribute it, you must take care of these issues.
86
-
84
+
87
85
## Pros
88
86
89
87
- Almost instant deserialization with minimal allocation provided that you
@@ -140,7 +138,7 @@ let t: DeserType<'_, [usize; 1000]> =
140
138
assert_eq! (s , * t );
141
139
142
140
// This is a traditional deserialization instead
143
- let t : [usize ; 1000 ] =
141
+ let t : [usize ; 1000 ] =
144
142
unsafe { <[usize ; 1000]>:: deserialize_full (
145
143
& mut std :: fs :: File :: open (& file )?
146
144
)? };
@@ -149,25 +147,19 @@ assert_eq!(s, t);
149
147
// In this case we map the data structure into memory
150
148
//
151
149
// Note: requires the `mmap` feature.
152
- let u : MemCase <& [usize ; 1000 ]> =
153
- unsafe { <[usize ; 1000]>:: mmap (& file , Flags :: empty ())? };
154
-
155
- assert_eq! (s , * * u );
156
-
157
- // When using a MemCase, the lifetime of the derived deserialization type is 'static
158
- let u : MemCase <DeserType <'static , [usize ; 1000 ]>> =
150
+ let u : MemCase <[usize ; 1000 ]> =
159
151
unsafe { <[usize ; 1000]>:: mmap (& file , Flags :: empty ())? };
160
152
161
- assert_eq! (s , * * u );
153
+ assert_eq! (s , * * u . borrow () );
162
154
# Ok (())
163
155
# }
164
156
```
165
157
166
158
Note how we serialize an array, but we deserialize a reference. The reference
167
159
points inside ` b ` , so there is no copy performed. The call to
168
160
[ ` deserialize_full ` ] creates a new array instead. The third call maps the data
169
- structure into memory and returns a [ ` MemCase ` ] that can be used transparently
170
- as a reference to the array; moreover, the [ ` MemCase ` ] can be passed to other
161
+ structure into memory and returns a [ ` MemCase ` ] that can be used to get
162
+ a reference to the array; moreover, the [ ` MemCase ` ] can be passed to other
171
163
functions or stored in a structure field, as it contains both the structure and
172
164
the memory-mapped region that supports it.
173
165
@@ -205,14 +197,14 @@ let t: DeserType<'_, Vec<usize>> =
205
197
assert_eq! (s , * t );
206
198
207
199
// This is a traditional deserialization instead
208
- let t : Vec <usize > =
200
+ let t : Vec <usize > =
209
201
unsafe { <Vec <usize >>:: load_full (& file )? };
210
202
assert_eq! (s , t );
211
203
212
204
// In this case we map the data structure into memory
213
- let u : MemCase <DeserType <' static , Vec <usize >>> =
205
+ let u : MemCase <Vec <usize >> =
214
206
unsafe { <Vec <usize >>:: mmap (& file , Flags :: empty ())? };
215
- assert_eq! (s , * * u );
207
+ assert_eq! (s , * * u . borrow () );
216
208
# Ok (())
217
209
# }
218
210
```
@@ -264,14 +256,14 @@ let t: DeserType<'_, Vec<Data>> =
264
256
assert_eq! (s , * t );
265
257
266
258
// This is a traditional deserialization instead
267
- let t : Vec <Data > =
259
+ let t : Vec <Data > =
268
260
unsafe { <Vec <Data >>:: load_full (& file )? };
269
261
assert_eq! (s , t );
270
262
271
263
// In this case we map the data structure into memory
272
- let u : MemCase <DeserType <' static , Vec <Data >>> =
264
+ let u : MemCase <Vec <Data >> =
273
265
unsafe { <Vec <Data >>:: mmap (& file , Flags :: empty ())? };
274
- assert_eq! (s , * * u );
266
+ assert_eq! (s , * * u . borrow () );
275
267
# Ok (())
276
268
# }
277
269
```
@@ -309,20 +301,21 @@ unsafe { s.store(&file) };
309
301
let b = std :: fs :: read (& file )? ;
310
302
311
303
// The type of t will be inferred--it is shown here only for clarity
312
- let t : MyStruct <& [isize ]> =
304
+ let t : MyStruct <& [isize ]> =
313
305
unsafe { <MyStruct <Vec <isize >>>:: deserialize_eps (b . as_ref ())? };
314
306
315
307
assert_eq! (s . id, t . id);
316
308
assert_eq! (s . data, Vec :: from (t . data));
317
309
318
310
// This is a traditional deserialization instead
319
- let t : MyStruct <Vec <isize >> =
311
+ let t : MyStruct <Vec <isize >> =
320
312
unsafe { <MyStruct <Vec <isize >>>:: load_full (& file )? };
321
313
assert_eq! (s , t );
322
314
323
315
// In this case we map the data structure into memory
324
- let u : MemCase <MyStruct <& [ isize ]>> =
316
+ let u : MemCase <MyStruct <Vec < isize >>> =
325
317
unsafe { <MyStruct <Vec <isize >>>:: mmap (& file , Flags :: empty ())? };
318
+ let u : & MyStruct <& [isize ]> = u . borrow ();
326
319
assert_eq! (s . id, u . id);
327
320
assert_eq! (s . data, u . data. as_ref ());
328
321
# Ok (())
@@ -371,8 +364,9 @@ let t = unsafe { MyStruct::deserialize_eps(b.as_ref())? };
371
364
assert_eq! (s . sum (), t . sum ());
372
365
373
366
let t = unsafe { <MyStruct >:: mmap (& file , Flags :: empty ())? };
367
+ let t : & MyStructParam <& [isize ]> = t . borrow ();
374
368
375
- // t works transparently as a MyStructParam<&[isize]>
369
+ // t works transparently as a & MyStructParam<&[isize]>
376
370
assert_eq! (s . id, t . id);
377
371
assert_eq! (s . data, t . data. as_ref ());
378
372
assert_eq! (s . sum (), t . sum ());
@@ -407,7 +401,7 @@ unsafe { s.store(&file) };
407
401
let b = std :: fs :: read (& file )? ;
408
402
409
403
// The type of t is unchanged
410
- let t : MyStruct <Vec <isize >> =
404
+ let t : MyStruct <Vec <isize >> =
411
405
unsafe { <MyStruct <Vec <isize >>>:: deserialize_eps (b . as_ref ())? };
412
406
# Ok (())
413
407
# }
@@ -445,7 +439,7 @@ unsafe { s.store(&file) };
445
439
let b = std :: fs :: read (& file )? ;
446
440
447
441
// The type of t is unchanged
448
- let t : & MyStruct <i32 > =
442
+ let t : & MyStruct <i32 > =
449
443
unsafe { <MyStruct <i32 >>:: deserialize_eps (b . as_ref ())? };
450
444
# Ok (())
451
445
# }
@@ -480,7 +474,7 @@ let e = Enum::B(vec![0, 1, 2, 3]);
480
474
let mut file = std :: env :: temp_dir ();
481
475
file . push (" serialized7" );
482
476
unsafe { e . store (& file ) };
483
- // Deserializing using just Enum will fail, as the type parameter
477
+ // Deserializing using just Enum will fail, as the type parameter
484
478
// by default is Vec<usize>
485
479
assert! (unsafe { <Enum >:: load_full (& file ) }. is_err ());
486
480
# Ok (())
@@ -513,7 +507,7 @@ let t: &[i32] = unsafe { <Vec<i32>>::deserialize_eps(b.as_ref())? };
513
507
let t : Vec <i32 > = unsafe { <Vec <i32 >>:: deserialize_full (
514
508
& mut std :: fs :: File :: open (& file )?
515
509
)? };
516
- let t : MemCase <& [ i32 ] > = unsafe { <Vec <i32 >>:: mmap (& file , Flags :: empty ())? };
510
+ let t : MemCase <Vec < i32 > > = unsafe { <Vec <i32 >>:: mmap (& file , Flags :: empty ())? };
517
511
518
512
// Within a structure
519
513
#[derive(Epserde , Debug , PartialEq , Eq , Default , Clone )]
@@ -532,7 +526,7 @@ let t: Data<&[i32]> = unsafe { <Data<Vec<i32>>>::deserialize_eps(b.as_ref())? };
532
526
let t : Data <Vec <i32 >> = unsafe { <Data <Vec <i32 >>>:: deserialize_full (
533
527
& mut std :: fs :: File :: open (& file )?
534
528
)? };
535
- let t : MemCase <Data <& [ i32 ] >> = unsafe { <Data <Vec <i32 >>>:: mmap (& file , Flags :: empty ())? };
529
+ let t : MemCase <Data <Vec < i32 > >> = unsafe { <Data <Vec <i32 >>>:: mmap (& file , Flags :: empty ())? };
536
530
# Ok (())
537
531
# }
538
532
```
@@ -565,7 +559,7 @@ let t: &[i32] = unsafe { <Vec<i32>>::deserialize_eps(b.as_ref())? };
565
559
let t : Vec <i32 > = unsafe { <Vec <i32 >>:: deserialize_full (
566
560
& mut std :: fs :: File :: open (& file )?
567
561
)? };
568
- let t : MemCase <& [ i32 ] > = unsafe { <Vec <i32 >>:: mmap (& file , Flags :: empty ())? };
562
+ let t : MemCase <Vec < i32 > > = unsafe { <Vec <i32 >>:: mmap (& file , Flags :: empty ())? };
569
563
570
564
// Within a structure
571
565
#[derive(Epserde , Debug , PartialEq , Eq , Default , Clone )]
@@ -584,7 +578,7 @@ let t: Data<&[i32]> = unsafe { <Data<Vec<i32>>>::deserialize_eps(b.as_ref())? };
584
578
let t : Data <Vec <i32 >> = unsafe { <Data <Vec <i32 >>>:: deserialize_full (
585
579
& mut std :: fs :: File :: open (& file )?
586
580
)? };
587
- let t : MemCase <Data <& [ i32 ] >> = unsafe { <Data <Vec <i32 >>>:: mmap (& file , Flags :: empty ())? };
581
+ let t : MemCase <Data <Vec < i32 > >> = unsafe { <Data <Vec <i32 >>>:: mmap (& file , Flags :: empty ())? };
588
582
# Ok (())
589
583
# }
590
584
```
0 commit comments