Skip to content

Commit caea18d

Browse files
committed
Implement Add<&HashSet<T, S>> for &HashSet<T, S>
This is functionally the same as BitOr, but imo its more intuitive if it exists because of the Sub impl
1 parent f67749f commit caea18d

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/set.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloc::borrow::ToOwned;
55
use core::fmt;
66
use core::hash::{BuildHasher, Hash};
77
use core::iter::{Chain, FusedIterator};
8-
use core::ops::{BitAnd, BitOr, BitXor, Sub};
8+
use core::ops::{Add, BitAnd, BitOr, BitXor, Sub};
99

1010
use super::map::{self, DefaultHashBuilder, HashMap, Keys};
1111
use crate::raw::{Allocator, Global, RawExtractIf};
@@ -1502,6 +1502,38 @@ where
15021502
}
15031503
}
15041504

1505+
impl<T, S> Add<&HashSet<T, S>> for &HashSet<T, S>
1506+
where
1507+
T: Eq + Hash + Clone,
1508+
S: BuildHasher + Default,
1509+
{
1510+
type Output = HashSet<T, S>;
1511+
1512+
/// Returns the union of `self` and `rhs` as a new `HashSet<T, S>`.
1513+
///
1514+
/// # Examples
1515+
///
1516+
/// ```
1517+
/// use hashbrown::HashSet;
1518+
///
1519+
/// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1520+
/// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect();
1521+
///
1522+
/// let set = &a + &b;
1523+
///
1524+
/// let mut i = 0;
1525+
/// let expected = [1, 2, 3, 4];
1526+
/// for x in &set {
1527+
/// assert!(expected.contains(x));
1528+
/// i += 1;
1529+
/// }
1530+
/// assert_eq!(i, expected.len());
1531+
/// ```
1532+
fn add(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
1533+
self.union(rhs).cloned().collect()
1534+
}
1535+
}
1536+
15051537
impl<T, S> Sub<&HashSet<T, S>> for &HashSet<T, S>
15061538
where
15071539
T: Eq + Hash + Clone,

0 commit comments

Comments
 (0)