@@ -22,8 +22,11 @@ use crate::{
22
22
use document_serializer:: DocumentSerializer ;
23
23
24
24
/// Serializer used to convert a type `T` into raw BSON bytes.
25
- pub ( crate ) struct Serializer {
26
- bytes : Vec < u8 > ,
25
+ pub ( crate ) struct Serializer < ' a > {
26
+ bytes : & ' a mut Vec < u8 > ,
27
+
28
+ /// The index into `bytes` where the current serialization started.
29
+ start_index : usize ,
27
30
28
31
/// The index into `bytes` where the current element type will need to be stored.
29
32
/// This needs to be set retroactively because in BSON, the element type comes before the key,
@@ -58,21 +61,18 @@ impl SerializerHint {
58
61
}
59
62
}
60
63
61
- impl Serializer {
62
- pub ( crate ) fn new ( ) -> Self {
64
+ impl < ' a > Serializer < ' a > {
65
+ pub ( crate ) fn new ( bytes : & ' a mut Vec < u8 > ) -> Self {
66
+ let start_index = bytes. len ( ) ;
63
67
Self {
64
- bytes : Vec :: new ( ) ,
65
- type_index : 0 ,
68
+ bytes,
69
+ start_index,
70
+ type_index : start_index,
66
71
hint : SerializerHint :: None ,
67
72
human_readable : false ,
68
73
}
69
74
}
70
75
71
- /// Convert this serializer into the vec of the serialized bytes.
72
- pub ( crate ) fn into_vec ( self ) -> Vec < u8 > {
73
- self . bytes
74
- }
75
-
76
76
/// Reserve a spot for the element type to be set retroactively via `update_element_type`.
77
77
#[ inline]
78
78
fn reserve_element_type ( & mut self ) {
@@ -83,7 +83,7 @@ impl Serializer {
83
83
/// Retroactively set the element type of the most recently serialized element.
84
84
#[ inline]
85
85
fn update_element_type ( & mut self , t : ElementType ) -> Result < ( ) > {
86
- if self . type_index == 0 {
86
+ if self . type_index == self . start_index {
87
87
if matches ! ( t, ElementType :: EmbeddedDocument ) {
88
88
// don't need to set the element type for the top level document
89
89
return Ok ( ( ) ) ;
@@ -108,22 +108,22 @@ impl Serializer {
108
108
109
109
fn serialize_raw ( & mut self , v : RawBsonRef ) -> Result < ( ) > {
110
110
self . update_element_type ( v. element_type ( ) ) ?;
111
- v. append_to ( & mut self . bytes ) ;
111
+ v. append_to ( self . bytes ) ;
112
112
Ok ( ( ) )
113
113
}
114
114
}
115
115
116
- impl < ' a > serde:: Serializer for & ' a mut Serializer {
116
+ impl < ' a , ' b > serde:: Serializer for & ' a mut Serializer < ' b > {
117
117
type Ok = ( ) ;
118
118
type Error = Error ;
119
119
120
- type SerializeSeq = DocumentSerializer < ' a > ;
121
- type SerializeTuple = DocumentSerializer < ' a > ;
122
- type SerializeTupleStruct = DocumentSerializer < ' a > ;
123
- type SerializeTupleVariant = VariantSerializer < ' a > ;
124
- type SerializeMap = DocumentSerializer < ' a > ;
125
- type SerializeStruct = StructSerializer < ' a > ;
126
- type SerializeStructVariant = VariantSerializer < ' a > ;
120
+ type SerializeSeq = DocumentSerializer < ' a , ' b > ;
121
+ type SerializeTuple = DocumentSerializer < ' a , ' b > ;
122
+ type SerializeTupleStruct = DocumentSerializer < ' a , ' b > ;
123
+ type SerializeTupleVariant = VariantSerializer < ' a , ' b > ;
124
+ type SerializeMap = DocumentSerializer < ' a , ' b > ;
125
+ type SerializeStruct = StructSerializer < ' a , ' b > ;
126
+ type SerializeStructVariant = VariantSerializer < ' a , ' b > ;
127
127
128
128
fn is_human_readable ( & self ) -> bool {
129
129
self . human_readable
@@ -385,15 +385,15 @@ impl<'a> serde::Serializer for &'a mut Serializer {
385
385
}
386
386
}
387
387
388
- pub ( crate ) enum StructSerializer < ' a > {
388
+ pub ( crate ) enum StructSerializer < ' a , ' b > {
389
389
/// Serialize a BSON value currently represented in serde as a struct (e.g. ObjectId)
390
- Value ( ValueSerializer < ' a > ) ,
390
+ Value ( ValueSerializer < ' a , ' b > ) ,
391
391
392
392
/// Serialize the struct as a document.
393
- Document ( DocumentSerializer < ' a > ) ,
393
+ Document ( DocumentSerializer < ' a , ' b > ) ,
394
394
}
395
395
396
- impl SerializeStruct for StructSerializer < ' _ > {
396
+ impl SerializeStruct for StructSerializer < ' _ , ' _ > {
397
397
type Ok = ( ) ;
398
398
type Error = Error ;
399
399
@@ -424,8 +424,8 @@ enum VariantInnerType {
424
424
425
425
/// Serializer used for enum variants, including both tuple (e.g. Foo::Bar(1, 2, 3)) and
426
426
/// struct (e.g. Foo::Bar { a: 1 }).
427
- pub ( crate ) struct VariantSerializer < ' a > {
428
- root_serializer : & ' a mut Serializer ,
427
+ pub ( crate ) struct VariantSerializer < ' a , ' b > {
428
+ root_serializer : & ' a mut Serializer < ' b > ,
429
429
430
430
/// Variants are serialized as documents of the form `{ <variant name>: <document or array> }`,
431
431
/// and `doc_start` indicates the index at which the outer document begins.
@@ -438,22 +438,26 @@ pub(crate) struct VariantSerializer<'a> {
438
438
num_elements_serialized : usize ,
439
439
}
440
440
441
- impl < ' a > VariantSerializer < ' a > {
442
- fn start ( rs : & ' a mut Serializer , variant : & ' static CStr , inner_type : VariantInnerType ) -> Self {
441
+ impl < ' a , ' b > VariantSerializer < ' a , ' b > {
442
+ fn start (
443
+ rs : & ' a mut Serializer < ' b > ,
444
+ variant : & ' static CStr ,
445
+ inner_type : VariantInnerType ,
446
+ ) -> Self {
443
447
let doc_start = rs. bytes . len ( ) ;
444
448
// write placeholder length for document, will be updated at end
445
449
static ZERO : RawBsonRef = RawBsonRef :: Int32 ( 0 ) ;
446
- ZERO . append_to ( & mut rs. bytes ) ;
450
+ ZERO . append_to ( rs. bytes ) ;
447
451
448
452
let inner = match inner_type {
449
453
VariantInnerType :: Struct => ElementType :: EmbeddedDocument ,
450
454
VariantInnerType :: Tuple => ElementType :: Array ,
451
455
} ;
452
456
rs. bytes . push ( inner as u8 ) ;
453
- variant. append_to ( & mut rs. bytes ) ;
457
+ variant. append_to ( rs. bytes ) ;
454
458
let inner_start = rs. bytes . len ( ) ;
455
459
// write placeholder length for inner, will be updated at end
456
- ZERO . append_to ( & mut rs. bytes ) ;
460
+ ZERO . append_to ( rs. bytes ) ;
457
461
458
462
Self {
459
463
root_serializer : rs,
@@ -469,7 +473,7 @@ impl<'a> VariantSerializer<'a> {
469
473
T : Serialize + ?Sized ,
470
474
{
471
475
self . root_serializer . reserve_element_type ( ) ;
472
- CStr :: from_str ( k) ?. append_to ( & mut self . root_serializer . bytes ) ;
476
+ CStr :: from_str ( k) ?. append_to ( self . root_serializer . bytes ) ;
473
477
v. serialize ( & mut * self . root_serializer ) ?;
474
478
475
479
self . num_elements_serialized += 1 ;
@@ -492,7 +496,7 @@ impl<'a> VariantSerializer<'a> {
492
496
}
493
497
}
494
498
495
- impl serde:: ser:: SerializeTupleVariant for VariantSerializer < ' _ > {
499
+ impl serde:: ser:: SerializeTupleVariant for VariantSerializer < ' _ , ' _ > {
496
500
type Ok = ( ) ;
497
501
498
502
type Error = Error ;
@@ -511,7 +515,7 @@ impl serde::ser::SerializeTupleVariant for VariantSerializer<'_> {
511
515
}
512
516
}
513
517
514
- impl serde:: ser:: SerializeStructVariant for VariantSerializer < ' _ > {
518
+ impl serde:: ser:: SerializeStructVariant for VariantSerializer < ' _ , ' _ > {
515
519
type Ok = ( ) ;
516
520
517
521
type Error = Error ;
0 commit comments