Skip to content

Commit 455d556

Browse files
committed
More consistent docs + hide internal fields
1 parent ce7669e commit 455d556

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

src/de.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'de> de::SeqAccess<'de> for SeqAccess {
2727
}
2828
}
2929

30-
/// Provides [`serde::de::MapAccess`] from any JS iterator that returns `[key, value]` pairs.
30+
/// Provides [`de::MapAccess`] from any JS iterator that returns `[key, value]` pairs.
3131
struct MapAccess {
3232
iter: js_sys::IntoIter,
3333
next_value: Option<Deserializer>,
@@ -143,7 +143,7 @@ impl<'de> de::SeqAccess<'de> for PreservedValueAccess {
143143
}
144144
}
145145

146-
/// Provides [`serde::de::EnumAccess`] from given JS values for the `tag` and the `payload`.
146+
/// Provides [`de::EnumAccess`] from given JS values for the `tag` and the `payload`.
147147
struct EnumAccess {
148148
tag: Deserializer,
149149
payload: Deserializer,
@@ -161,7 +161,7 @@ impl<'de> de::EnumAccess<'de> for EnumAccess {
161161
}
162162
}
163163

164-
/// A newtype that allows using any [`JsValue`] as a [`serde::Deserializer`].
164+
/// A newtype that allows using any [`JsValue`] as a [`deserializer`].
165165
pub struct Deserializer {
166166
value: JsValue,
167167
}
@@ -189,8 +189,6 @@ fn convert_pair(pair: JsValue) -> (Deserializer, Deserializer) {
189189
}
190190

191191
impl Deserializer {
192-
/// Casts the internal value into an object, including support for prototype-less objects.
193-
/// See https://github.com/rustwasm/wasm-bindgen/issues/1366 for why we don't use `dyn_ref`.
194192
fn as_object_entries(&self) -> Option<Array> {
195193
if self.value.is_object() {
196194
Some(Object::entries(self.value.unchecked_ref()))
@@ -415,6 +413,9 @@ impl<'de> de::Deserializer<'de> for Deserializer {
415413
self.deserialize_from_js_number_unsigned(visitor)
416414
}
417415

416+
/// Supported inputs:
417+
/// - `BigInt` within `i64` boundaries.
418+
/// - number within safe integer boundaries.
418419
fn deserialize_i64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
419420
if self.value.is_bigint() {
420421
match i64::try_from(self.value) {
@@ -428,6 +429,9 @@ impl<'de> de::Deserializer<'de> for Deserializer {
428429
}
429430
}
430431

432+
/// Supported inputs:
433+
/// - `BigInt` within `u64` boundaries.
434+
/// - number within safe integer boundaries.
431435
fn deserialize_u64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
432436
if self.value.is_bigint() {
433437
match u64::try_from(self.value) {
@@ -441,6 +445,8 @@ impl<'de> de::Deserializer<'de> for Deserializer {
441445
}
442446
}
443447

448+
/// Supported inputs:
449+
/// - `BigInt` within `i128` boundaries.
444450
fn deserialize_i128<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
445451
if self.value.is_bigint() {
446452
match i128::try_from(self.value) {
@@ -454,6 +460,8 @@ impl<'de> de::Deserializer<'de> for Deserializer {
454460
}
455461
}
456462

463+
/// Supported inputs:
464+
/// - `BigInt` within `u128` boundaries.
457465
fn deserialize_u128<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
458466
if self.value.is_bigint() {
459467
match u128::try_from(self.value) {
@@ -467,7 +475,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
467475
}
468476
}
469477

470-
/// Converts a JavaScript string to a Rust char.
478+
/// Converts a JavaScript string to a Rust `char`.
471479
///
472480
/// By default we don't perform detection of single chars because it's pretty complicated,
473481
/// but if we get a hint that they're expected, this methods allows to avoid heap allocations
@@ -481,6 +489,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
481489
self.invalid_type(visitor)
482490
}
483491

492+
/// Deserializes `undefined` or `null` into `None` and any other value into `Some(value)`.
484493
// Serde can deserialize `visit_unit` into `None`, but can't deserialize arbitrary value
485494
// as `Some`, so we need to provide own simple implementation.
486495
fn deserialize_option<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
@@ -491,7 +500,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
491500
}
492501
}
493502

494-
/// Simply calls `visit_newtype_struct`.
503+
/// Forwards to deserializing newtype contents.
495504
fn deserialize_newtype_struct<V: de::Visitor<'de>>(
496505
self,
497506
_name: &'static str,
@@ -502,6 +511,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
502511

503512
/// Supported inputs:
504513
/// - JS iterable (an object with [`[Symbol.iterator]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator)).
514+
///
505515
/// Supported outputs:
506516
/// - Any Rust sequence from Serde point of view ([`Vec`], [`HashSet`](std::collections::HashSet), etc.)
507517
fn deserialize_seq<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
@@ -535,6 +545,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
535545
/// Supported inputs:
536546
/// - A JS iterable that is expected to return `[key, value]` pairs.
537547
/// - A JS object, which will be iterated using [`Object.entries`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries).
548+
///
538549
/// Supported outputs:
539550
/// - A Rust key-value map ([`HashMap`](std::collections::HashMap), [`BTreeMap`](std::collections::BTreeMap), etc.).
540551
/// - A typed Rust structure with `#[derive(Deserialize)]`.
@@ -550,6 +561,7 @@ impl<'de> de::Deserializer<'de> for Deserializer {
550561

551562
/// Supported inputs:
552563
/// - A plain JS object.
564+
///
553565
/// Supported outputs:
554566
/// - A typed Rust structure with `#[derive(Deserialize)]`.
555567
fn deserialize_struct<V: de::Visitor<'de>>(

src/ser.rs

+47-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use crate::{static_str_to_js, Error, ObjectExt};
1010
type Result<T = JsValue> = super::Result<T>;
1111

1212
/// 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.
1415
pub struct VariantSerializer<S> {
1516
variant: &'static str,
1617
inner: S,
1718
}
1819

1920
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 {
2122
Self { variant, inner }
2223
}
2324

@@ -64,14 +65,15 @@ impl<S: ser::SerializeStruct<Ok = JsValue, Error = Error>> ser::SerializeStructV
6465
}
6566
}
6667

68+
/// Serializes Rust iterables and tuples into JS arrays.
6769
pub struct ArraySerializer<'s> {
6870
serializer: &'s Serializer,
6971
target: Array,
7072
idx: u32,
7173
}
7274

7375
impl<'s> ArraySerializer<'s> {
74-
pub fn new(serializer: &'s Serializer) -> Self {
76+
fn new(serializer: &'s Serializer) -> Self {
7577
Self {
7678
serializer,
7779
target: Array::new(),
@@ -126,6 +128,10 @@ pub enum MapResult {
126128
Object(Object),
127129
}
128130

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.
129135
pub struct MapSerializer<'s> {
130136
serializer: &'s Serializer,
131137
target: MapResult,
@@ -182,6 +188,7 @@ impl ser::SerializeMap for MapSerializer<'_> {
182188
}
183189
}
184190

191+
/// Serializes Rust structs into plain JS objects.
185192
pub struct ObjectSerializer<'s> {
186193
serializer: &'s Serializer,
187194
target: ObjectExt,
@@ -314,6 +321,11 @@ impl<'s> ser::Serializer for &'s Serializer {
314321
serialize_str(&str);
315322
}
316323

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.
317329
fn serialize_i64(self, v: i64) -> Result {
318330
if self.serialize_large_number_types_as_bigints {
319331
return Ok(v.into());
@@ -334,6 +346,11 @@ impl<'s> ser::Serializer for &'s Serializer {
334346
}
335347
}
336348

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.
337354
fn serialize_u64(self, v: u64) -> Result {
338355
if self.serialize_large_number_types_as_bigints {
339356
return Ok(v.into());
@@ -349,18 +366,24 @@ impl<'s> ser::Serializer for &'s Serializer {
349366
}
350367
}
351368

369+
/// Serializes `i128` into a `BigInt`.
352370
fn serialize_i128(self, v: i128) -> Result {
353371
Ok(JsValue::from(v))
354372
}
355373

374+
/// Serializes `u128` into a `BigInt`.
356375
fn serialize_u128(self, v: u128) -> Result {
357376
Ok(JsValue::from(v))
358377
}
359378

379+
/// Serializes `char` into a JS string.
360380
fn serialize_char(self, v: char) -> Result {
361381
Ok(JsString::from(v).into())
362382
}
363383

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.
364387
fn serialize_bytes(self, v: &[u8]) -> Result {
365388
// Create a `Uint8Array` view into a Rust slice, and immediately copy it to the JS memory.
366389
//
@@ -374,14 +397,21 @@ impl<'s> ser::Serializer for &'s Serializer {
374397
}
375398
}
376399

400+
/// Serializes `None` into `undefined` or `null`.
401+
///
402+
/// If `serialize_missing_as_null` is set to `true`, `None` is serialized as `null`.
377403
fn serialize_none(self) -> Result {
378404
self.serialize_unit()
379405
}
380406

407+
/// Serializes `Some(T)` as `T`.
381408
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result {
382409
value.serialize(self)
383410
}
384411

412+
/// Serializes `()` into `undefined` or `null`.
413+
///
414+
/// If `serialize_missing_as_null` is set to `true`, `()` is serialized as `null`.
385415
fn serialize_unit(self) -> Result {
386416
Ok(if self.serialize_missing_as_null {
387417
JsValue::NULL
@@ -390,11 +420,12 @@ impl<'s> ser::Serializer for &'s Serializer {
390420
})
391421
}
392422

423+
/// Serializes unit structs into `undefined` or `null`.
393424
fn serialize_unit_struct(self, _name: &'static str) -> Result {
394425
self.serialize_unit()
395426
}
396427

397-
/// For compatibility with serde-json, serialises unit variants as "Variant" strings.
428+
/// For compatibility with serde-json, sserializes unit variants as "Variant" strings.
398429
fn serialize_unit_variant(
399430
self,
400431
_name: &'static str,
@@ -404,6 +435,7 @@ impl<'s> ser::Serializer for &'s Serializer {
404435
Ok(static_str_to_js(variant).into())
405436
}
406437

438+
/// Serializes newtype structs as their inner values.
407439
fn serialize_newtype_struct<T: ?Sized + Serialize>(
408440
self,
409441
name: &'static str,
@@ -419,6 +451,7 @@ impl<'s> ser::Serializer for &'s Serializer {
419451
value.serialize(self)
420452
}
421453

454+
/// Serializes newtype variants as their inner values.
422455
fn serialize_newtype_variant<T: ?Sized + Serialize>(
423456
self,
424457
_name: &'static str,
@@ -429,16 +462,18 @@ impl<'s> ser::Serializer for &'s Serializer {
429462
VariantSerializer::new(variant, self.serialize_newtype_struct(variant, value)?).end(Ok)
430463
}
431464

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.
434467
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
435468
Ok(ArraySerializer::new(self))
436469
}
437470

471+
/// Sserializes Rust tuples as JS arrays.
438472
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
439473
self.serialize_seq(Some(len))
440474
}
441475

476+
/// Sserializes Rust tuple structs as JS arrays.
442477
fn serialize_tuple_struct(
443478
self,
444479
_name: &'static str,
@@ -447,6 +482,7 @@ impl<'s> ser::Serializer for &'s Serializer {
447482
self.serialize_tuple(len)
448483
}
449484

485+
/// Sserializes Rust tuple variants as `{"Variant": [ ...tuple... ]}`.
450486
fn serialize_tuple_variant(
451487
self,
452488
_name: &'static str,
@@ -460,16 +496,19 @@ impl<'s> ser::Serializer for &'s Serializer {
460496
))
461497
}
462498

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.
464502
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
465503
Ok(MapSerializer::new(self, self.serialize_maps_as_objects))
466504
}
467505

468-
/// Serialises Rust typed structs into plain JS objects.
506+
/// Sserializes Rust typed structs into plain JS objects.
469507
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
470508
Ok(ObjectSerializer::new(self))
471509
}
472510

511+
/// Sserializes Rust struct-like variants into `{"Variant": { ...fields... }}`.
473512
fn serialize_struct_variant(
474513
self,
475514
_name: &'static str,

tests/common/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ fn sequences() {
736736
test_via_json((100, "xyz".to_string(), true));
737737

738738
// Sets are currently indistinguishable from other sequences for
739-
// Serde serialisers, so this will become an array on the JS side.
739+
// Serde sserializers, so this will become an array on the JS side.
740740
test_via_json(hashset! {false, true});
741741
}
742742

0 commit comments

Comments
 (0)