dpdk_serde/
OrderedSet.rs

1// This file is part of dpdk. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT. No part of dpdk, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of dpdk. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT.
3
4
5#[derive(Debug, Clone)]
6pub struct OrderedSet<V: Debug + Clone + Eq + Hash + Serialize + Deserialize>(OrderMap<V, ()>);
7
8impl<V: Debug + Clone + Eq + Hash + Serialize + Deserialize> Serialize for OrderedSet<V>
9{
10	fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
11	{
12		let ordered: Vec<V> = self.0.keys().map(|key| key.clone()).collect();
13		ordered.serialize(serializer)
14	}
15}
16
17impl<V: Debug + Clone + Eq + Hash + Serialize + Deserialize> Deserialize for OrderedSet<V>
18{
19	fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, D::Error>
20	{
21		let mut ordered = Vec::<V>::deserialize(deserializer)?;
22		let mut map = OrderMap::with_capacity(ordered.len());
23		for value in ordered.drain(..)
24		{
25			map.insert(value, ());
26		}
27		Ok(OrderedSet(map))
28	}
29}
30
31impl<V: Debug + Clone + Eq + Hash + Serialize + Deserialize> Default for OrderedSet<V>
32{
33	#[inline(always)]
34	fn default() -> Self
35	{
36		OrderedSet(Default::default())
37	}
38}