Add a collect_map example

This commit is contained in:
David Tolnay 2017-04-13 14:21:28 -07:00
parent 41488252ff
commit 07bce54ad3
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -917,6 +917,24 @@ pub trait Serializer: Sized {
/// using [`serialize_map`]. Implementors should not need to override this
/// method.
///
/// ```rust
/// use std::collections::BTreeSet;
/// use serde::{Serialize, Serializer};
///
/// struct MapToUnit {
/// keys: BTreeSet<i32>,
/// }
///
/// // Serializes as a map in which the values are all unit.
/// impl Serialize for MapToUnit {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// serializer.collect_map(self.keys.iter().map(|k| (k, ())))
/// }
/// }
/// ```
///
/// [`serialize_map`]: #tymethod.serialize_map
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where