rkyv/impls/alloc/collections/
btree_set.rs

1use core::ops::ControlFlow;
2
3use rancor::{Fallible, Source};
4
5use crate::{
6    alloc::collections::BTreeSet,
7    collections::btree_set::{ArchivedBTreeSet, BTreeSetResolver},
8    ser::{Allocator, Writer},
9    Archive, Deserialize, Place, Serialize,
10};
11
12impl<K: Archive + Ord> Archive for BTreeSet<K>
13where
14    K::Archived: Ord,
15{
16    type Archived = ArchivedBTreeSet<K::Archived>;
17    type Resolver = BTreeSetResolver;
18
19    fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>) {
20        ArchivedBTreeSet::<K::Archived>::resolve_from_len(
21            self.len(),
22            resolver,
23            out,
24        );
25    }
26}
27
28impl<K, S> Serialize<S> for BTreeSet<K>
29where
30    K: Serialize<S> + Ord,
31    K::Archived: Ord,
32    S: Fallible + Allocator + Writer + ?Sized,
33    S::Error: Source,
34{
35    fn serialize(
36        &self,
37        serializer: &mut S,
38    ) -> Result<Self::Resolver, S::Error> {
39        Self::Archived::serialize_from_ordered_iter::<_, K, _>(
40            self.iter(),
41            serializer,
42        )
43    }
44}
45
46impl<K, D> Deserialize<BTreeSet<K>, D> for ArchivedBTreeSet<K::Archived>
47where
48    K: Archive + Ord,
49    K::Archived: Deserialize<K, D> + Ord,
50    D: Fallible + ?Sized,
51{
52    fn deserialize(
53        &self,
54        deserializer: &mut D,
55    ) -> Result<BTreeSet<K>, D::Error> {
56        let mut result = BTreeSet::new();
57        let r = self.visit(|ak| {
58            let k = match ak.deserialize(deserializer) {
59                Ok(k) => k,
60                Err(e) => return ControlFlow::Break(e),
61            };
62            result.insert(k);
63            ControlFlow::Continue(())
64        });
65        match r {
66            Some(e) => Err(e),
67            None => Ok(result),
68        }
69    }
70}
71
72impl<K, AK: PartialEq<K>> PartialEq<BTreeSet<K>> for ArchivedBTreeSet<AK> {
73    fn eq(&self, other: &BTreeSet<K>) -> bool {
74        if self.len() != other.len() {
75            false
76        } else {
77            let mut iter = other.iter();
78            self.visit(|ak| {
79                if let Some(k) = iter.next() {
80                    if ak.eq(k) {
81                        return ControlFlow::Continue(());
82                    }
83                }
84                ControlFlow::Break(())
85            })
86            .is_none()
87        }
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use crate::{
94        alloc::{collections::BTreeSet, string::ToString},
95        api::test::roundtrip,
96    };
97
98    #[test]
99    fn roundtrip_btree_set() {
100        let mut value = BTreeSet::new();
101        value.insert("foo".to_string());
102        value.insert("bar".to_string());
103        value.insert("baz".to_string());
104        value.insert("bat".to_string());
105
106        roundtrip(&value);
107    }
108
109    #[test]
110    fn roundtrip_btree_set_zst() {
111        let mut value = BTreeSet::new();
112        value.insert(());
113
114        roundtrip(&value);
115    }
116}