Skip to main content

clt_database/alloc/collections/
hash_set.rs

1use std::hash::{BuildHasher, Hash};
2
3use rustc_hash::FxBuildHasher;
4
5use super::{TryClone, TursoFromIterator, TursoHashSetExt, TursoTryWithCapacityExt};
6use crate::alloc::{HashSet, TryReserveError};
7
8impl<T, S> TursoHashSetExt<T> for HashSet<T, S>
9where
10    T: Eq + Hash,
11    S: BuildHasher,
12{
13    #[inline(always)]
14    fn try_insert(&mut self, value: T) -> Result<bool, TryReserveError> {
15        Ok(self.insert(value))
16    }
17}
18
19impl<T> TursoTryWithCapacityExt for HashSet<T>
20where
21    T: Eq + Hash,
22{
23    #[inline(always)]
24    fn try_with_capacity_ext(capacity: usize) -> Result<Self, TryReserveError> {
25        Ok(HashSet::with_capacity_and_hasher(capacity, FxBuildHasher))
26    }
27}
28
29impl<T> TursoFromIterator<T> for HashSet<T>
30where
31    T: Eq + Hash,
32{
33    #[inline(always)]
34    fn try_from_iter<I>(iter: I) -> Result<Self, TryReserveError>
35    where
36        I: IntoIterator<Item = T>,
37    {
38        Ok(iter.into_iter().collect())
39    }
40
41    #[inline(always)]
42    fn try_extend<I>(&mut self, iter: I) -> Result<(), TryReserveError>
43    where
44        I: IntoIterator<Item = T>,
45    {
46        self.extend(iter);
47        Ok(())
48    }
49}
50
51impl<T, S> TryClone for HashSet<T, S>
52where
53    T: Clone + Eq + Hash,
54    S: BuildHasher + Clone,
55{
56    type Error = TryReserveError;
57
58    #[inline(always)]
59    fn try_clone(&self) -> Result<Self, Self::Error> {
60        let mut cloned = Self::with_hasher(self.hasher().clone());
61        cloned.try_reserve(self.len())?;
62        cloned.extend(self.iter().cloned());
63        Ok(cloned)
64    }
65}