Skip to content

Commit 7304afc

Browse files
committed
impl Serialize for {map,set}::Slice
1 parent e9024a3 commit 7304afc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/serde_seq.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,42 @@ use core::hash::{BuildHasher, Hash};
2828
use core::marker::PhantomData;
2929

3030
use crate::IndexMap;
31+
use crate::map::Slice as MapSlice;
32+
use crate::set::Slice as SetSlice;
33+
34+
/// Serializes a `map::Slice` as an ordered sequence.
35+
///
36+
/// This behaves like [`crate::serde_seq`] for `IndexMap`, serializing a sequence
37+
/// of `(key, value)` pairs, rather than as a map that might not preserver order.
38+
///
39+
/// Requires crate feature `"serde"` or `"serde-1"`
40+
impl<K, V> Serialize for MapSlice<K, V>
41+
where
42+
K: Serialize,
43+
V: Serialize,
44+
{
45+
fn serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error>
46+
where
47+
T: Serializer,
48+
{
49+
serializer.collect_seq(self)
50+
}
51+
}
52+
53+
/// Serializes a `set::Slice` as an ordered sequence.
54+
///
55+
/// Requires crate feature `"serde"` or `"serde-1"`
56+
impl<T> Serialize for SetSlice<T>
57+
where
58+
T: Serialize,
59+
{
60+
fn serialize<Se>(&self, serializer: Se) -> Result<Se::Ok, Se::Error>
61+
where
62+
Se: Serializer,
63+
{
64+
serializer.collect_seq(self)
65+
}
66+
}
3167

3268
/// Serializes an `IndexMap` as an ordered sequence.
3369
///

0 commit comments

Comments
 (0)