@@ -60,44 +60,45 @@ These are the main limitations you should be aware of before choosing to use
60
60
tree.
61
61
62
62
- While we provide procedural macros that implement serialization and
63
- deserialization, they require that your type is written and used in a specific
64
- way; in particular, the fields you want to ε-copy must be type parameters
65
- implementing [ ` DeserializeInner ` ] , to which a [ deserialized type] is associated.
66
- For example, we provide implementations for ` Vec<T> ` /` Box<[T]> ` , where ` T ` is
67
- zero-copy, or ` String ` /` Box<str> ` , which have associated deserialized type
68
- ` &[T] ` or ` &str ` , respectively. Vectors and boxed slices of types that are not
69
- zero-copy will be deserialized recursively in memory instead.
63
+ deserialization, they require that your type is written and used in a specific
64
+ way; in particular, the fields you want to ε-copy must be type parameters
65
+ implementing [ ` DeserializeInner ` ] , to which a [ deserialized type] is
66
+ associated. For example, we provide implementations for ` Vec<T> ` /` Box<[T]> ` ,
67
+ where ` T ` is zero-copy, or ` String ` /` Box<str> ` , which have associated
68
+ deserialized type ` &[T] ` or ` &str ` , respectively. Vectors and boxed slices of
69
+ types that are not zero-copy will be deserialized recursively in memory
70
+ instead.
70
71
71
72
- After deserialization of a type ` T ` , you will obtain an associated
72
- deserialized type [ ` DeserType<'_,T> ` ] , which will usually reference the
73
- underlying serialized support (e.g., a memory-mapped region); hence the need for
74
- a lifetime. If you need to store the deserialized structure in a field of a new
75
- structure you will need to couple permanently the deserialized structure with
76
- its serialized support, which is obtained by putting it in a [ ` MemCase ` ] using
77
- the convenience methods [ ` Deserialize::load_mem ` ] , [ ` Deserialize::load_mmap ` ] ,
78
- and [ ` Deserialize::mmap ` ] . A [ ` MemCase ` ] will deref to its contained type, so it
79
- can be used transparently as long as fields and methods are concerned, but if
80
- your original type is ` T ` the field of the new structure will have to be of type
81
- ` MemCase<DeserType<'static, T>> ` , not ` T ` .
73
+ deserialized type [ ` DeserType<'_,T> ` ] , which will usually reference the
74
+ underlying serialized support (e.g., a memory-mapped region); hence the need for
75
+ a lifetime. If you need to store the deserialized structure in a field of a new
76
+ structure you will need to couple permanently the deserialized structure with
77
+ its serialized support, which is obtained by putting it in a [ ` MemCase ` ] using
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 ` .
82
83
83
84
## Pros
84
85
85
86
- Almost instant deserialization with minimal allocation provided that you
86
- designed your type following the ε-serde guidelines or that you use standard
87
- types.
87
+ designed your type following the ε-serde guidelines or that you use standard
88
+ types.
88
89
89
90
- The structure you get by deserialization is the same structure you serialized,
90
- except that type parameters will be replaced by their associated deserialization
91
- type (e.g., vectors will become references to slices). This is not the
92
- case with [ rkiv] , which requires you to reimplement all methods on the
93
- deserialized type.
91
+ except that type parameters will be replaced by their associated deserialization
92
+ type (e.g., vectors will become references to slices). This is not the
93
+ case with [ rkiv] , which requires you to reimplement all methods on the
94
+ deserialized type.
94
95
95
96
- The structure you get by deserialization has exactly the same performance as
96
- the structure you serialized. This is not the case with [ zerovec] or [ rkiv] .
97
+ the structure you serialized. This is not the case with [ zerovec] or [ rkiv] .
97
98
98
99
- You can deserialize from read-only supports, as all dynamic information
99
- generated at deserialization time is stored in newly allocated memory. This is
100
- not the case with [ Abomonation] .
100
+ generated at deserialization time is stored in newly allocated memory. This is
101
+ not the case with [ Abomonation] .
101
102
102
103
## Example: Zero copy of standard types
103
104
@@ -298,20 +299,20 @@ s.store(&file);
298
299
let b = std :: fs :: read (& file )? ;
299
300
300
301
// The type of t will be inferred--it is shown here only for clarity
301
- let t : DeserType <' _ , MyStruct <Vec < isize >> > =
302
+ let t : MyStruct <& [ isize ] > =
302
303
<MyStruct <Vec <isize >>>:: deserialize_eps (b . as_ref ())? ;
303
304
304
305
assert_eq! (s . id, t . id);
305
306
assert_eq! (s . data, Vec :: from (t . data));
306
307
307
308
// This is a traditional deserialization instead
308
309
let t : MyStruct <Vec <isize >> =
309
- <MyStruct :: <Vec <isize >>> :: load_full (& file )? ;
310
+ <MyStruct <Vec <isize >>>:: load_full (& file )? ;
310
311
assert_eq! (s , t );
311
312
312
313
// In this case we map the data structure into memory
313
- let u : MemCase <DeserType <' static , MyStruct <Vec < isize >> >> =
314
- <MyStruct :: <Vec <isize >>> :: mmap (& file , Flags :: empty ())? ;
314
+ let u : MemCase <MyStruct <& [ isize ] >> =
315
+ <MyStruct <Vec <isize >>>:: mmap (& file , Flags :: empty ())? ;
315
316
assert_eq! (s . id, u . id);
316
317
assert_eq! (s . data, u . data. as_ref ());
317
318
# Ok (())
@@ -486,7 +487,7 @@ Every type serializable with ε-serde has two features that are in principle
486
487
orthogonal, but that in practice often condition one another:
487
488
488
489
- the type has an * associated deserialization type* , which is the type you
489
- obtain upon deserialization;
490
+ obtain upon deserialization;
490
491
- the type can be either [ ` ZeroCopy ` ] or [ ` DeepCopy ` ] ; it can also be neither.
491
492
492
493
There is no constraint on the associated deserialization type: it can be
@@ -513,11 +514,11 @@ types are always fully deserialized*. There are two reasons behind this
513
514
non-orthogonal choice:
514
515
515
516
- primitive types occupy so little space that deserializing them as a reference
516
- is not efficient;
517
+ is not efficient;
517
518
- if a type parameter ` T ` is a primitive type, writing generic code for
518
- ` AsRef<T> ` is really not nice;
519
+ ` AsRef<T> ` is really not nice;
519
520
- deserializing primitive types to a reference would require further padding to
520
- align them.
521
+ align them.
521
522
522
523
Since this is true only of primitive types, when deserializing a 1-tuple
523
524
containing a primitive type one obtains a reference (and indeed this workaround
0 commit comments