Skip to main content

maplike/alloc/
btreeset.rs

1// SPDX-FileCopyrightText: 2025 maplike contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use _alloc::collections::BTreeSet;
6
7use crate::{Get, Insert, IntoIter, KeyedCollection, Remove, Set, StableRemove};
8
9impl<K> KeyedCollection for BTreeSet<K> {
10    type Key = K;
11    type Value = ();
12}
13
14impl<K: Ord> Get<K> for BTreeSet<K> {
15    #[inline(always)]
16    fn get(&self, key: &K) -> Option<&()> {
17        BTreeSet::get(self, key).map(|_| &())
18    }
19}
20
21impl<K: Ord> Set<K> for BTreeSet<K> {
22    #[inline(always)]
23    fn set(&mut self, key: K, _value: ()) {
24        BTreeSet::insert(self, key);
25    }
26}
27
28impl<K: Ord> Insert<K> for BTreeSet<K> {
29    #[inline(always)]
30    fn insert(&mut self, key: K, _value: ()) {
31        BTreeSet::insert(self, key);
32    }
33}
34
35impl<K: Ord> Remove<K> for BTreeSet<K> {
36    #[inline(always)]
37    fn remove(&mut self, key: &K) -> Option<()> {
38        BTreeSet::remove(self, key).then_some(())
39    }
40}
41
42impl<K: Ord> StableRemove<K> for BTreeSet<K> {}
43
44pub struct MapIntoIter<K>(_alloc::collections::btree_set::IntoIter<K>);
45
46impl<K> Iterator for MapIntoIter<K> {
47    type Item = (K, ());
48
49    #[inline(always)]
50    fn next(&mut self) -> Option<Self::Item> {
51        self.0.next().map(|k| (k, ()))
52    }
53}
54
55impl<K> IntoIter<K> for BTreeSet<K> {
56    type IntoIter = MapIntoIter<K>;
57
58    #[inline(always)]
59    fn into_iter(self) -> MapIntoIter<K> {
60        MapIntoIter(IntoIterator::into_iter(self))
61    }
62}