|
| 1 | +use crate::rule::{NonEmpty, NonEmptyRule}; |
| 2 | +use crate::Refined; |
| 3 | +use std::borrow::Borrow; |
| 4 | +use std::collections::hash_set::Difference; |
| 5 | + |
| 6 | +use std::collections::HashSet; |
| 7 | +use std::hash::{BuildHasher, Hash, RandomState}; |
| 8 | + |
| 9 | +pub type NonEmptyHashSet<T, S = RandomState> = Refined<NonEmptyRule<HashSet<T, S>>>; |
| 10 | +pub type NonEmptyHashSetRule<T, S = RandomState> = NonEmptyRule<HashSet<T, S>>; |
| 11 | + |
| 12 | +impl<T, S> NonEmptyHashSet<T, S> { |
| 13 | + #[allow(clippy::should_implement_trait)] |
| 14 | + pub fn into_iter(self) -> NonEmpty<std::collections::hash_set::IntoIter<T>> { |
| 15 | + Refined::new(self.into_value().into_iter()) |
| 16 | + .ok() |
| 17 | + .expect("This error is always unreachable") |
| 18 | + } |
| 19 | + |
| 20 | + #[allow(clippy::should_implement_trait)] |
| 21 | + pub fn iter(&self) -> NonEmpty<std::collections::hash_set::Iter<T>> { |
| 22 | + Refined::new(self.value().iter()) |
| 23 | + .ok() |
| 24 | + .expect("This error is always unreachable") |
| 25 | + } |
| 26 | + |
| 27 | + pub fn len(&self) -> usize { |
| 28 | + self.value().len() |
| 29 | + } |
| 30 | + |
| 31 | + pub fn is_empty(&self) -> bool { |
| 32 | + false |
| 33 | + } |
| 34 | + |
| 35 | + pub fn capacity(&self) -> usize { |
| 36 | + self.value().capacity() |
| 37 | + } |
| 38 | + |
| 39 | + pub fn hasher(&self) -> &S { |
| 40 | + self.value().hasher() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<T, S> NonEmptyHashSet<T, S> |
| 45 | +where |
| 46 | + T: Eq + Hash, |
| 47 | + S: BuildHasher, |
| 48 | +{ |
| 49 | + pub fn insert(self, value: T) -> Self { |
| 50 | + let mut result = self.into_value(); |
| 51 | + result.insert(value); |
| 52 | + Refined::new(result) |
| 53 | + .ok() |
| 54 | + .expect("This error is always unreachable") |
| 55 | + } |
| 56 | + |
| 57 | + pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T> |
| 58 | + where |
| 59 | + T: Borrow<Q>, |
| 60 | + Q: Hash + Eq, |
| 61 | + { |
| 62 | + self.value().get(value) |
| 63 | + } |
| 64 | + |
| 65 | + pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool |
| 66 | + where |
| 67 | + T: Borrow<Q>, |
| 68 | + Q: Hash + Eq, |
| 69 | + { |
| 70 | + self.value().contains(value) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S> { |
| 74 | + self.value().difference(other) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod test { |
| 80 | + use crate::rule::NonEmptyHashSet; |
| 81 | + use std::collections::HashSet; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_len() -> anyhow::Result<()> { |
| 85 | + let mut set = HashSet::new(); |
| 86 | + set.insert(1); |
| 87 | + let set = NonEmptyHashSet::new(set)?; |
| 88 | + assert_eq!(set.len(), 1); |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | +} |
0 commit comments