@@ -10,14 +10,15 @@ use crate::{static_str_to_js, Error, ObjectExt};
10
10
type Result < T = JsValue > = super :: Result < T > ;
11
11
12
12
/// Wraps other serializers into an enum tagged variant form.
13
- /// Uses {"Variant": ...payload...} for compatibility with serde-json.
13
+ ///
14
+ /// Results in `{"Variant": ...payload...}` for compatibility with serde-json.
14
15
pub struct VariantSerializer < S > {
15
16
variant : & ' static str ,
16
17
inner : S ,
17
18
}
18
19
19
20
impl < S > VariantSerializer < S > {
20
- pub const fn new ( variant : & ' static str , inner : S ) -> Self {
21
+ const fn new ( variant : & ' static str , inner : S ) -> Self {
21
22
Self { variant, inner }
22
23
}
23
24
@@ -64,14 +65,15 @@ impl<S: ser::SerializeStruct<Ok = JsValue, Error = Error>> ser::SerializeStructV
64
65
}
65
66
}
66
67
68
+ /// Serializes Rust iterables and tuples into JS arrays.
67
69
pub struct ArraySerializer < ' s > {
68
70
serializer : & ' s Serializer ,
69
71
target : Array ,
70
72
idx : u32 ,
71
73
}
72
74
73
75
impl < ' s > ArraySerializer < ' s > {
74
- pub fn new ( serializer : & ' s Serializer ) -> Self {
76
+ fn new ( serializer : & ' s Serializer ) -> Self {
75
77
Self {
76
78
serializer,
77
79
target : Array :: new ( ) ,
@@ -126,6 +128,10 @@ pub enum MapResult {
126
128
Object ( Object ) ,
127
129
}
128
130
131
+ /// Serializes Rust maps into JS `Map` or plain JS objects.
132
+ ///
133
+ /// Plain JS objects are used if `serialize_maps_as_objects` is set to `true`,
134
+ /// but then only string keys are supported.
129
135
pub struct MapSerializer < ' s > {
130
136
serializer : & ' s Serializer ,
131
137
target : MapResult ,
@@ -182,6 +188,7 @@ impl ser::SerializeMap for MapSerializer<'_> {
182
188
}
183
189
}
184
190
191
+ /// Serializes Rust structs into plain JS objects.
185
192
pub struct ObjectSerializer < ' s > {
186
193
serializer : & ' s Serializer ,
187
194
target : ObjectExt ,
@@ -314,6 +321,11 @@ impl<'s> ser::Serializer for &'s Serializer {
314
321
serialize_str( & str ) ;
315
322
}
316
323
324
+ /// Serializes `i64` into a `BigInt` or a JS number.
325
+ ///
326
+ /// If `serialize_large_number_types_as_bigints` is set to `false`,
327
+ /// `i64` is serialized as a JS number. But in this mode only numbers
328
+ /// within the safe integer range are supported.
317
329
fn serialize_i64 ( self , v : i64 ) -> Result {
318
330
if self . serialize_large_number_types_as_bigints {
319
331
return Ok ( v. into ( ) ) ;
@@ -334,6 +346,11 @@ impl<'s> ser::Serializer for &'s Serializer {
334
346
}
335
347
}
336
348
349
+ /// Serializes `u64` into a `BigInt` or a JS number.
350
+ ///
351
+ /// If `serialize_large_number_types_as_bigints` is set to `false`,
352
+ /// `u64` is serialized as a JS number. But in this mode only numbers
353
+ /// within the safe integer range are supported.
337
354
fn serialize_u64 ( self , v : u64 ) -> Result {
338
355
if self . serialize_large_number_types_as_bigints {
339
356
return Ok ( v. into ( ) ) ;
@@ -349,18 +366,24 @@ impl<'s> ser::Serializer for &'s Serializer {
349
366
}
350
367
}
351
368
369
+ /// Serializes `i128` into a `BigInt`.
352
370
fn serialize_i128 ( self , v : i128 ) -> Result {
353
371
Ok ( JsValue :: from ( v) )
354
372
}
355
373
374
+ /// Serializes `u128` into a `BigInt`.
356
375
fn serialize_u128 ( self , v : u128 ) -> Result {
357
376
Ok ( JsValue :: from ( v) )
358
377
}
359
378
379
+ /// Serializes `char` into a JS string.
360
380
fn serialize_char ( self , v : char ) -> Result {
361
381
Ok ( JsString :: from ( v) . into ( ) )
362
382
}
363
383
384
+ /// Serializes `bytes` into a JS `Uint8Array` or a plain JS array.
385
+ ///
386
+ /// If `serialize_bytes_as_arrays` is set to `true`, bytes are serialized as plain JS arrays.
364
387
fn serialize_bytes ( self , v : & [ u8 ] ) -> Result {
365
388
// Create a `Uint8Array` view into a Rust slice, and immediately copy it to the JS memory.
366
389
//
@@ -374,14 +397,21 @@ impl<'s> ser::Serializer for &'s Serializer {
374
397
}
375
398
}
376
399
400
+ /// Serializes `None` into `undefined` or `null`.
401
+ ///
402
+ /// If `serialize_missing_as_null` is set to `true`, `None` is serialized as `null`.
377
403
fn serialize_none ( self ) -> Result {
378
404
self . serialize_unit ( )
379
405
}
380
406
407
+ /// Serializes `Some(T)` as `T`.
381
408
fn serialize_some < T : ?Sized + Serialize > ( self , value : & T ) -> Result {
382
409
value. serialize ( self )
383
410
}
384
411
412
+ /// Serializes `()` into `undefined` or `null`.
413
+ ///
414
+ /// If `serialize_missing_as_null` is set to `true`, `()` is serialized as `null`.
385
415
fn serialize_unit ( self ) -> Result {
386
416
Ok ( if self . serialize_missing_as_null {
387
417
JsValue :: NULL
@@ -390,11 +420,12 @@ impl<'s> ser::Serializer for &'s Serializer {
390
420
} )
391
421
}
392
422
423
+ /// Serializes unit structs into `undefined` or `null`.
393
424
fn serialize_unit_struct ( self , _name : & ' static str ) -> Result {
394
425
self . serialize_unit ( )
395
426
}
396
427
397
- /// For compatibility with serde-json, serialises unit variants as "Variant" strings.
428
+ /// For compatibility with serde-json, sserializes unit variants as "Variant" strings.
398
429
fn serialize_unit_variant (
399
430
self ,
400
431
_name : & ' static str ,
@@ -404,6 +435,7 @@ impl<'s> ser::Serializer for &'s Serializer {
404
435
Ok ( static_str_to_js ( variant) . into ( ) )
405
436
}
406
437
438
+ /// Serializes newtype structs as their inner values.
407
439
fn serialize_newtype_struct < T : ?Sized + Serialize > (
408
440
self ,
409
441
name : & ' static str ,
@@ -419,6 +451,7 @@ impl<'s> ser::Serializer for &'s Serializer {
419
451
value. serialize ( self )
420
452
}
421
453
454
+ /// Serializes newtype variants as their inner values.
422
455
fn serialize_newtype_variant < T : ?Sized + Serialize > (
423
456
self ,
424
457
_name : & ' static str ,
@@ -429,16 +462,18 @@ impl<'s> ser::Serializer for &'s Serializer {
429
462
VariantSerializer :: new ( variant, self . serialize_newtype_struct ( variant, value) ?) . end ( Ok )
430
463
}
431
464
432
- /// Serialises any Rust iterable into a JS Array.
433
- // TODO: Figure out if there is a way to detect and serialise `Set` differently.
465
+ /// Sserializes any Rust iterable as a JS Array.
466
+ // TODO: Figure out if there is a way to detect and sserialize `Set` differently.
434
467
fn serialize_seq ( self , _len : Option < usize > ) -> Result < Self :: SerializeSeq > {
435
468
Ok ( ArraySerializer :: new ( self ) )
436
469
}
437
470
471
+ /// Sserializes Rust tuples as JS arrays.
438
472
fn serialize_tuple ( self , len : usize ) -> Result < Self :: SerializeTuple > {
439
473
self . serialize_seq ( Some ( len) )
440
474
}
441
475
476
+ /// Sserializes Rust tuple structs as JS arrays.
442
477
fn serialize_tuple_struct (
443
478
self ,
444
479
_name : & ' static str ,
@@ -447,6 +482,7 @@ impl<'s> ser::Serializer for &'s Serializer {
447
482
self . serialize_tuple ( len)
448
483
}
449
484
485
+ /// Sserializes Rust tuple variants as `{"Variant": [ ...tuple... ]}`.
450
486
fn serialize_tuple_variant (
451
487
self ,
452
488
_name : & ' static str ,
@@ -460,16 +496,19 @@ impl<'s> ser::Serializer for &'s Serializer {
460
496
) )
461
497
}
462
498
463
- /// Serialises Rust maps into JS `Map` or plain JS objects, depending on configuration of `serialize_maps_as_objects`.
499
+ /// Sserializes Rust maps into JS `Map` or plain JS objects.
500
+ ///
501
+ /// See [`MapSerializer`] for more details.
464
502
fn serialize_map ( self , _len : Option < usize > ) -> Result < Self :: SerializeMap > {
465
503
Ok ( MapSerializer :: new ( self , self . serialize_maps_as_objects ) )
466
504
}
467
505
468
- /// Serialises Rust typed structs into plain JS objects.
506
+ /// Sserializes Rust typed structs into plain JS objects.
469
507
fn serialize_struct ( self , _name : & ' static str , _len : usize ) -> Result < Self :: SerializeStruct > {
470
508
Ok ( ObjectSerializer :: new ( self ) )
471
509
}
472
510
511
+ /// Sserializes Rust struct-like variants into `{"Variant": { ...fields... }}`.
473
512
fn serialize_struct_variant (
474
513
self ,
475
514
_name : & ' static str ,
0 commit comments