mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 10:11:01 +00:00
Start documenting everything, starting with serde::ser.
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! Helper module to enable serializing bytes more efficiently
|
//! Helper module to enable serializing bytes more efficiently
|
||||||
|
|
||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use std::ops;
|
use std::ops;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ascii;
|
use std::ascii;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::{
|
use std::collections::{
|
||||||
BinaryHeap,
|
BinaryHeap,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! Generic deserialization framework.
|
//! Generic deserialization framework.
|
||||||
|
|
||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use std::collections::{
|
use std::collections::{
|
||||||
BTreeMap,
|
BTreeMap,
|
||||||
BTreeSet,
|
BTreeSet,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,12 @@
|
|||||||
//! handshake protocol between serializers and serializees can be completely optimized away,
|
//! handshake protocol between serializers and serializees can be completely optimized away,
|
||||||
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
||||||
//! type.
|
//! type.
|
||||||
|
|
||||||
#![doc(html_root_url="https://serde-rs.github.io/serde/serde")]
|
#![doc(html_root_url="https://serde-rs.github.io/serde/serde")]
|
||||||
#![cfg_attr(feature = "nightly", feature(collections, core, enumset, nonzero, step_trait, zero_one))]
|
#![cfg_attr(feature = "nightly", feature(collections, core, enumset, nonzero, step_trait, zero_one))]
|
||||||
|
|
||||||
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
extern crate num;
|
extern crate num;
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::{
|
use std::collections::{
|
||||||
BinaryHeap,
|
BinaryHeap,
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ pub mod impls;
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// A trait that describes a type that can be serialized by a `Serializer`.
|
||||||
pub trait Serialize {
|
pub trait Serialize {
|
||||||
|
/// Serializes this value into this serializer.
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer;
|
where S: Serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// A trait that describes a type that can serialize a stream of values into the underlying format.
|
||||||
pub trait Serializer {
|
pub trait Serializer {
|
||||||
|
/// The error type that can be returned if some error occurs during serialization.
|
||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
/// `visit_bool` serializes a `bool` value.
|
/// `visit_bool` serializes a `bool` value.
|
||||||
@@ -110,13 +114,20 @@ pub trait Serializer {
|
|||||||
self.visit_seq(impls::SeqIteratorVisitor::new(value.iter(), Some(value.len())))
|
self.visit_seq(impls::SeqIteratorVisitor::new(value.iter(), Some(value.len())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a `()` value.
|
||||||
fn visit_unit(&mut self) -> Result<(), Self::Error>;
|
fn visit_unit(&mut self) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Serializes a unit struct value.
|
||||||
|
///
|
||||||
|
/// By default, unit structs are serialized as a `()`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit_struct(&mut self, _name: &'static str) -> Result<(), Self::Error> {
|
fn visit_unit_struct(&mut self, _name: &'static str) -> Result<(), Self::Error> {
|
||||||
self.visit_unit()
|
self.visit_unit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a unit variant, otherwise known as a variant with no arguments.
|
||||||
|
///
|
||||||
|
/// By default, unit variants are serialized as a `()`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit_variant(&mut self,
|
fn visit_unit_variant(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
@@ -155,17 +166,27 @@ pub trait Serializer {
|
|||||||
Some(value))
|
Some(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a `None` value.
|
||||||
fn visit_none(&mut self) -> Result<(), Self::Error>;
|
fn visit_none(&mut self) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Serializes a `Some(...)` value.
|
||||||
fn visit_some<V>(&mut self, value: V) -> Result<(), Self::Error>
|
fn visit_some<V>(&mut self, value: V) -> Result<(), Self::Error>
|
||||||
where V: Serialize;
|
where V: Serialize;
|
||||||
|
|
||||||
|
/// Serializes a sequence.
|
||||||
|
///
|
||||||
|
/// Callees of this method need to construct a `SeqVisitor`, which iterates through each item
|
||||||
|
/// in the sequence.
|
||||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||||
where V: SeqVisitor;
|
where V: SeqVisitor;
|
||||||
|
|
||||||
|
/// Serializes a sequence element.
|
||||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||||
where T: Serialize;
|
where T: Serialize;
|
||||||
|
|
||||||
|
/// Serializes a tuple.
|
||||||
|
///
|
||||||
|
/// By default this serializes a tuple as a sequence.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_tuple<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
fn visit_tuple<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor,
|
||||||
@@ -173,6 +194,9 @@ pub trait Serializer {
|
|||||||
self.visit_seq(visitor)
|
self.visit_seq(visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a tuple element.
|
||||||
|
///
|
||||||
|
/// By default, tuples are serialized as a sequence.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_tuple_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
fn visit_tuple_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
@@ -180,6 +204,9 @@ pub trait Serializer {
|
|||||||
self.visit_seq_elt(value)
|
self.visit_seq_elt(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a tuple struct.
|
||||||
|
///
|
||||||
|
/// By default, tuple structs are serialized as a tuple.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_tuple_struct<V>(&mut self,
|
fn visit_tuple_struct<V>(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
@@ -189,6 +216,9 @@ pub trait Serializer {
|
|||||||
self.visit_tuple(visitor)
|
self.visit_tuple(visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a tuple struct element.
|
||||||
|
///
|
||||||
|
/// By default, tuple struct elements are serialized as a tuple element.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_tuple_struct_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
fn visit_tuple_struct_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
@@ -196,6 +226,9 @@ pub trait Serializer {
|
|||||||
self.visit_tuple_elt(value)
|
self.visit_tuple_elt(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a tuple variant.
|
||||||
|
///
|
||||||
|
/// By default, tuple variants are serialized as a tuple struct.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_tuple_variant<V>(&mut self,
|
fn visit_tuple_variant<V>(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
@@ -207,6 +240,9 @@ pub trait Serializer {
|
|||||||
self.visit_tuple_struct(variant, visitor)
|
self.visit_tuple_struct(variant, visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a tuple element.
|
||||||
|
///
|
||||||
|
/// By default, tuples are serialized as a sequence.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_tuple_variant_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
fn visit_tuple_variant_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
@@ -214,13 +250,21 @@ pub trait Serializer {
|
|||||||
self.visit_tuple_struct_elt(value)
|
self.visit_tuple_struct_elt(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a map.
|
||||||
|
///
|
||||||
|
/// Callees of this method need to construct a `MapVisitor`, which iterates through each item
|
||||||
|
/// in the map.
|
||||||
fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||||
where V: MapVisitor;
|
where V: MapVisitor;
|
||||||
|
|
||||||
|
/// Serializes a map element (key-value pair).
|
||||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
|
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
|
||||||
where K: Serialize,
|
where K: Serialize,
|
||||||
V: Serialize;
|
V: Serialize;
|
||||||
|
|
||||||
|
/// Serializes a struct.
|
||||||
|
///
|
||||||
|
/// By default, structs are serialized as a map with the field name as the key.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_struct<V>(&mut self,
|
fn visit_struct<V>(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
@@ -230,6 +274,9 @@ pub trait Serializer {
|
|||||||
self.visit_map(visitor)
|
self.visit_map(visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes an element of a struct.
|
||||||
|
///
|
||||||
|
/// By default, struct elements are serialized as a map element with the field name as the key.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_struct_elt<V>(&mut self,
|
fn visit_struct_elt<V>(&mut self,
|
||||||
key: &'static str,
|
key: &'static str,
|
||||||
@@ -239,6 +286,9 @@ pub trait Serializer {
|
|||||||
self.visit_map_elt(key, value)
|
self.visit_map_elt(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a struct variant.
|
||||||
|
///
|
||||||
|
/// By default, struct variants are serialized as a struct.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_struct_variant<V>(&mut self,
|
fn visit_struct_variant<V>(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
@@ -250,6 +300,9 @@ pub trait Serializer {
|
|||||||
self.visit_struct(variant, visitor)
|
self.visit_struct(variant, visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes an element of a struct variant.
|
||||||
|
///
|
||||||
|
/// By default, struct variant elements are serialized as a struct element.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_struct_variant_elt<V>(&mut self,
|
fn visit_struct_variant_elt<V>(&mut self,
|
||||||
key: &'static str,
|
key: &'static str,
|
||||||
@@ -268,7 +321,12 @@ pub trait Serializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A trait that is used by a `Serialize` to iterate through a sequence.
|
||||||
pub trait SeqVisitor {
|
pub trait SeqVisitor {
|
||||||
|
/// Serializes a sequence item in the serializer.
|
||||||
|
///
|
||||||
|
/// This returns `Ok(Some(()))` when there are more items to serialize, or `Ok(None)` when
|
||||||
|
/// complete.
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||||
where S: Serializer;
|
where S: Serializer;
|
||||||
|
|
||||||
@@ -279,7 +337,12 @@ pub trait SeqVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A trait that is used by a `Serializer` to iterate through a map.
|
||||||
pub trait MapVisitor {
|
pub trait MapVisitor {
|
||||||
|
/// Serializes a map item in the serializer.
|
||||||
|
///
|
||||||
|
/// This returns `Ok(Some(()))` when there are more items to serialize, or `Ok(None)` when
|
||||||
|
/// complete.
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||||
where S: Serializer;
|
where S: Serializer;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user