@@ -18,8 +18,7 @@ impl<'a: 'de, 'de> de::Deserializer<'de> for Deserializer<'a> {
18
18
19
19
forward_to_deserialize_any ! {
20
20
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 str
21
- string newtype_struct ignored_any
22
- map unit_struct struct enum identifier
21
+ string ignored_any map unit_struct struct enum identifier
23
22
}
24
23
25
24
fn deserialize_any < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
@@ -118,6 +117,21 @@ impl<'a: 'de, 'de> de::Deserializer<'de> for Deserializer<'a> {
118
117
self . deserialize_bytes ( visitor)
119
118
}
120
119
120
+ fn deserialize_newtype_struct < V > (
121
+ self ,
122
+ name : & ' static str ,
123
+ visitor : V ,
124
+ ) -> Result < V :: Value , Self :: Error >
125
+ where
126
+ V : Visitor < ' de > ,
127
+ {
128
+ if name == "__neo4rs::RawBytes" {
129
+ self . parse_next_item ( Visitation :: RawBytes , visitor)
130
+ } else {
131
+ self . parse_next_item ( Visitation :: default ( ) , visitor)
132
+ }
133
+ }
134
+
121
135
fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
122
136
where
123
137
V : Visitor < ' de > ,
@@ -158,14 +172,21 @@ impl<'de> Deserializer<'de> {
158
172
return Err ( Error :: Empty ) ;
159
173
}
160
174
161
- if let Visitation :: SeqAsTuple ( 2 ) = v {
162
- return if self . bytes [ 0 ] == 0x92 {
163
- self . bytes . advance ( 1 ) ;
164
- Self :: parse_list ( v, 2 , self . bytes , visitor)
165
- } else {
166
- visitor. visit_seq ( ItemsParser :: new ( 2 , self . bytes ) )
167
- } ;
168
- }
175
+ match v {
176
+ Visitation :: SeqAsTuple ( 2 ) => {
177
+ return if self . bytes [ 0 ] == 0x92 {
178
+ self . bytes . advance ( 1 ) ;
179
+ Self :: parse_list ( v, 2 , self . bytes , visitor)
180
+ } else {
181
+ visitor. visit_seq ( ItemsParser :: new ( 2 , self . bytes ) )
182
+ } ;
183
+ }
184
+ Visitation :: RawBytes => {
185
+ let bytes = self . next_item_as_bytes ( ) ?;
186
+ return visitor. visit_bytes ( & bytes) ;
187
+ }
188
+ _ => ( ) ,
189
+ } ;
169
190
170
191
Self :: parse ( v, self . bytes , visitor)
171
192
}
@@ -175,6 +196,22 @@ impl<'de> Deserializer<'de> {
175
196
. map ( |_| ( ) )
176
197
}
177
198
199
+ fn next_item_as_bytes ( self ) -> Result < Bytes , Error > {
200
+ let mut full_bytes = self . bytes . clone ( ) ;
201
+
202
+ {
203
+ let this = Deserializer { bytes : self . bytes } ;
204
+ this. skip_next_item ( ) ?;
205
+ }
206
+
207
+ let start = full_bytes. as_ptr ( ) ;
208
+ let end = self . bytes . as_ptr ( ) ;
209
+
210
+ let len = unsafe { end. offset_from ( start) } ;
211
+ full_bytes. truncate ( len. unsigned_abs ( ) ) ;
212
+ Ok ( full_bytes)
213
+ }
214
+
178
215
fn parse < V : Visitor < ' de > > (
179
216
v : Visitation ,
180
217
bytes : & ' de mut Bytes ,
@@ -227,12 +264,11 @@ impl<'de> Deserializer<'de> {
227
264
bytes : & ' de mut Bytes ,
228
265
visitor : V ,
229
266
) -> Result < V :: Value , Error > {
230
- debug_assert ! ( bytes. len( ) >= len) ;
231
-
232
- let bytes = bytes. split_to ( len) ;
267
+ let bytes = Self :: take_slice ( len, bytes) ;
233
268
if v. visit_bytes_as_bytes ( ) {
234
- let bytes: & ' de [ u8 ] = unsafe { std:: mem:: transmute ( bytes. as_ref ( ) ) } ;
235
- visitor. visit_borrowed_bytes ( bytes)
269
+ visitor. visit_borrowed_bytes ( unsafe {
270
+ std:: slice:: from_raw_parts ( bytes. as_ptr ( ) , bytes. len ( ) )
271
+ } )
236
272
} else {
237
273
visitor. visit_seq ( SeqDeserializer :: new ( bytes. into_iter ( ) ) )
238
274
}
@@ -243,17 +279,20 @@ impl<'de> Deserializer<'de> {
243
279
bytes : & ' de mut Bytes ,
244
280
visitor : V ,
245
281
) -> Result < V :: Value , Error > {
246
- debug_assert ! ( bytes. len( ) >= len) ;
247
-
248
- let bytes = bytes. split_to ( len) ;
249
- let bytes: & ' de [ u8 ] = unsafe { std:: mem:: transmute ( bytes. as_ref ( ) ) } ;
250
-
251
- match std:: str:: from_utf8 ( bytes) {
252
- Ok ( s) => visitor. visit_borrowed_str ( s) ,
282
+ let bytes = Self :: take_slice ( len, bytes) ;
283
+ match std:: str:: from_utf8 ( & bytes) {
284
+ Ok ( s) => visitor. visit_borrowed_str ( unsafe {
285
+ std:: str:: from_utf8_unchecked ( std:: slice:: from_raw_parts ( s. as_ptr ( ) , s. len ( ) ) )
286
+ } ) ,
253
287
Err ( e) => Err ( Error :: InvalidUtf8 ( e) ) ,
254
288
}
255
289
}
256
290
291
+ fn take_slice ( len : usize , bytes : & mut Bytes ) -> Bytes {
292
+ debug_assert ! ( bytes. len( ) >= len) ;
293
+ bytes. split_to ( len)
294
+ }
295
+
257
296
fn parse_list < V : Visitor < ' de > > (
258
297
v : Visitation ,
259
298
len : usize ,
@@ -299,6 +338,7 @@ impl<'de> Deserializer<'de> {
299
338
}
300
339
}
301
340
341
+ #[ derive( Debug ) ]
302
342
struct ItemsParser < ' a > {
303
343
len : usize ,
304
344
excess : usize ,
@@ -339,6 +379,10 @@ impl<'a, 'de> SeqAccess<'de> for ItemsParser<'a> {
339
379
let bytes = self . bytes . get ( ) ;
340
380
seed. deserialize ( Deserializer { bytes } ) . map ( Some )
341
381
}
382
+
383
+ fn size_hint ( & self ) -> Option < usize > {
384
+ Some ( self . len )
385
+ }
342
386
}
343
387
344
388
impl < ' a , ' de > MapAccess < ' de > for ItemsParser < ' a > {
@@ -364,94 +408,42 @@ impl<'a, 'de> MapAccess<'de> for ItemsParser<'a> {
364
408
let bytes = self . bytes . get ( ) ;
365
409
seed. deserialize ( Deserializer { bytes } )
366
410
}
411
+
412
+ fn size_hint ( & self ) -> Option < usize > {
413
+ Some ( self . len )
414
+ }
367
415
}
368
416
369
417
impl < ' a , ' de > VariantAccess < ' de > for ItemsParser < ' a > {
370
418
type Error = Error ;
371
419
372
420
fn unit_variant ( mut self ) -> Result < ( ) , Self :: Error > {
373
421
self . next_value ( )
374
- // if self.len != 0 {
375
- // return Err(Error::InvalidLength {
376
- // expected: 0,
377
- // actual: self.len,
378
- // });
379
- // }
380
- // Ok(())
381
422
}
382
423
383
424
fn newtype_variant_seed < T > ( mut self , seed : T ) -> Result < T :: Value , Self :: Error >
384
425
where
385
426
T : DeserializeSeed < ' de > ,
386
427
{
387
428
self . next_value_seed ( seed)
388
- // let bytes = self.bytes.get();
389
- // seed.deserialize(Deserializer { bytes })
390
429
}
391
430
392
- fn tuple_variant < V > ( mut self , len : usize , visitor : V ) -> Result < V :: Value , Self :: Error >
431
+ fn tuple_variant < V > ( self , _len : usize , visitor : V ) -> Result < V :: Value , Self :: Error >
393
432
where
394
433
V : Visitor < ' de > ,
395
434
{
396
- struct TupleVariant < V > {
397
- len : usize ,
398
- visitor : V ,
399
- }
400
-
401
- impl < ' de , V > DeserializeSeed < ' de > for TupleVariant < V >
402
- where
403
- V : Visitor < ' de > ,
404
- {
405
- type Value = V :: Value ;
406
-
407
- fn deserialize < D > ( self , deserializer : D ) -> Result < Self :: Value , D :: Error >
408
- where
409
- D : serde:: Deserializer < ' de > ,
410
- {
411
- deserializer. deserialize_tuple ( self . len , self . visitor )
412
- }
413
- }
414
-
415
- self . next_value_seed ( TupleVariant { len, visitor } )
416
-
417
- // if len != self.len {
418
- // return Err(Error::InvalidLength {
419
- // expected: len,
420
- // actual: self.len,
421
- // });
422
- // }
423
- // visitor.visit_seq(self)
435
+ visitor. visit_seq ( self )
424
436
}
425
437
426
438
fn struct_variant < V > (
427
- mut self ,
439
+ self ,
428
440
_fields : & ' static [ & ' static str ] ,
429
441
visitor : V ,
430
442
) -> Result < V :: Value , Self :: Error >
431
443
where
432
444
V : Visitor < ' de > ,
433
445
{
434
- struct StructVariant < V > {
435
- visitor : V ,
436
- }
437
-
438
- impl < ' de , V > DeserializeSeed < ' de > for StructVariant < V >
439
- where
440
- V : Visitor < ' de > ,
441
- {
442
- type Value = V :: Value ;
443
-
444
- fn deserialize < D > ( self , deserializer : D ) -> Result < Self :: Value , D :: Error >
445
- where
446
- D : serde:: Deserializer < ' de > ,
447
- {
448
- deserializer. deserialize_map ( self . visitor )
449
- }
450
- }
451
-
452
- self . next_value_seed ( StructVariant { visitor } )
453
-
454
- // visitor.visit_map(self)
446
+ visitor. visit_seq ( self )
455
447
}
456
448
}
457
449
@@ -479,6 +471,7 @@ enum Visitation {
479
471
#[ default]
480
472
Default ,
481
473
BytesAsBytes ,
474
+ RawBytes ,
482
475
MapAsSeq ,
483
476
SeqAsTuple ( usize ) ,
484
477
}
@@ -498,6 +491,13 @@ struct SharedBytes<'a> {
498
491
_lifetime : PhantomData < & ' a mut ( ) > ,
499
492
}
500
493
494
+ #[ cfg( debug_assertions) ]
495
+ impl < ' a > fmt:: Debug for SharedBytes < ' a > {
496
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
497
+ crate :: bolt:: Dbg ( unsafe { & * self . bytes } ) . fmt ( f)
498
+ }
499
+ }
500
+
501
501
impl < ' a > SharedBytes < ' a > {
502
502
fn new ( bytes : & ' a mut Bytes ) -> Self {
503
503
Self {
0 commit comments