From 308460400bbab1383a61e1e61238a9c730d30ed9 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 19:39:57 -0500 Subject: [PATCH] libcollections: convert `TrieSet` binops to by value --- src/libcollections/trie/set.rs | 104 +++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/src/libcollections/trie/set.rs b/src/libcollections/trie/set.rs index 5621726dc56..7b7b4d8280b 100644 --- a/src/libcollections/trie/set.rs +++ b/src/libcollections/trie/set.rs @@ -462,6 +462,8 @@ impl Extend for TrieSet { } } +// NOTE(stage0): Remove impl after a snapshot +#[cfg(stage0)] #[unstable = "matches collection reform specification, waiting for dust to settle"] impl BitOr for TrieSet { /// Returns the union of `self` and `rhs` as a new `TrieSet`. @@ -483,6 +485,30 @@ impl BitOr for TrieSet { } } +#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b> BitOr<&'b TrieSet, TrieSet> for &'a TrieSet { + /// Returns the union of `self` and `rhs` as a new `TrieSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TrieSet; + /// + /// let a: TrieSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TrieSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: TrieSet = &a | &b; + /// let v: Vec = set.iter().collect(); + /// assert_eq!(v, vec![1u, 2, 3, 4, 5]); + /// ``` + fn bitor(self, rhs: &TrieSet) -> TrieSet { + self.union(rhs).collect() + } +} + +// NOTE(stage0): Remove impl after a snapshot +#[cfg(stage0)] #[unstable = "matches collection reform specification, waiting for dust to settle"] impl BitAnd for TrieSet { /// Returns the intersection of `self` and `rhs` as a new `TrieSet`. @@ -504,6 +530,30 @@ impl BitAnd for TrieSet { } } +#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b> BitAnd<&'b TrieSet, TrieSet> for &'a TrieSet { + /// Returns the intersection of `self` and `rhs` as a new `TrieSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TrieSet; + /// + /// let a: TrieSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TrieSet = vec![2, 3, 4].into_iter().collect(); + /// + /// let set: TrieSet = &a & &b; + /// let v: Vec = set.iter().collect(); + /// assert_eq!(v, vec![2u, 3]); + /// ``` + fn bitand(self, rhs: &TrieSet) -> TrieSet { + self.intersection(rhs).collect() + } +} + +// NOTE(stage0): Remove impl after a snapshot +#[cfg(stage0)] #[unstable = "matches collection reform specification, waiting for dust to settle"] impl BitXor for TrieSet { /// Returns the symmetric difference of `self` and `rhs` as a new `TrieSet`. @@ -525,6 +575,30 @@ impl BitXor for TrieSet { } } +#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b> BitXor<&'b TrieSet, TrieSet> for &'a TrieSet { + /// Returns the symmetric difference of `self` and `rhs` as a new `TrieSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TrieSet; + /// + /// let a: TrieSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TrieSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: TrieSet = &a ^ &b; + /// let v: Vec = set.iter().collect(); + /// assert_eq!(v, vec![1u, 2, 4, 5]); + /// ``` + fn bitxor(self, rhs: &TrieSet) -> TrieSet { + self.symmetric_difference(rhs).collect() + } +} + +// NOTE(stage0): Remove impl after a snapshot +#[cfg(stage0)] #[unstable = "matches collection reform specification, waiting for dust to settle"] impl Sub for TrieSet { /// Returns the difference of `self` and `rhs` as a new `TrieSet`. @@ -546,6 +620,28 @@ impl Sub for TrieSet { } } +#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b> Sub<&'b TrieSet, TrieSet> for &'a TrieSet { + /// Returns the difference of `self` and `rhs` as a new `TrieSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TrieSet; + /// + /// let a: TrieSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TrieSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: TrieSet = &a - &b; + /// let v: Vec = set.iter().collect(); + /// assert_eq!(v, vec![1u, 2]); + /// ``` + fn sub(self, rhs: &TrieSet) -> TrieSet { + self.difference(rhs).collect() + } +} + /// A forward iterator over a set. pub struct SetItems<'a> { iter: Entries<'a, ()> @@ -837,7 +933,7 @@ mod test { let a: TrieSet = vec![1, 2, 3].into_iter().collect(); let b: TrieSet = vec![3, 4, 5].into_iter().collect(); - let set: TrieSet = a | b; + let set: TrieSet = &a | &b; let v: Vec = set.iter().collect(); assert_eq!(v, vec![1u, 2, 3, 4, 5]); } @@ -847,7 +943,7 @@ mod test { let a: TrieSet = vec![1, 2, 3].into_iter().collect(); let b: TrieSet = vec![2, 3, 4].into_iter().collect(); - let set: TrieSet = a & b; + let set: TrieSet = &a & &b; let v: Vec = set.iter().collect(); assert_eq!(v, vec![2u, 3]); } @@ -857,7 +953,7 @@ mod test { let a: TrieSet = vec![1, 2, 3].into_iter().collect(); let b: TrieSet = vec![3, 4, 5].into_iter().collect(); - let set: TrieSet = a ^ b; + let set: TrieSet = &a ^ &b; let v: Vec = set.iter().collect(); assert_eq!(v, vec![1u, 2, 4, 5]); } @@ -867,7 +963,7 @@ mod test { let a: TrieSet = vec![1, 2, 3].into_iter().collect(); let b: TrieSet = vec![3, 4, 5].into_iter().collect(); - let set: TrieSet = a - b; + let set: TrieSet = &a - &b; let v: Vec = set.iter().collect(); assert_eq!(v, vec![1u, 2]); }