@@ -70,13 +70,37 @@ impl<T: Ord + Clone> VecExt<T> for Vec<T> {
7070}
7171
7272pub trait SetExt < T > : Sized {
73- fn split_first ( self ) -> Option < ( T , Self ) > ;
73+ fn split_first ( self ) -> Option < ( T , Set < T > ) > ;
7474
75- fn union_with ( self , other : Self ) -> Self ;
75+ fn union_with ( self , other : impl Upcast < Set < T > > ) -> Set < T > ;
7676
77- fn plus ( self , other : T ) -> Self ;
77+ fn with_element ( self , other : impl Upcast < T > ) -> Set < T > ;
7878
79- fn split_nth ( & self , i : usize ) -> Option < ( T , Self ) > ;
79+ fn without_element ( self , other : impl Upcast < T > ) -> Set < T > ;
80+
81+ fn split_nth ( & self , i : usize ) -> Option < ( T , Set < T > ) > ;
82+ }
83+
84+ impl < T : Ord + Clone > SetExt < T > for & Set < T > {
85+ fn split_first ( self ) -> Option < ( T , Set < T > ) > {
86+ self . clone ( ) . split_first ( )
87+ }
88+
89+ fn union_with ( self , other : impl Upcast < Set < T > > ) -> Set < T > {
90+ self . clone ( ) . union_with ( other)
91+ }
92+
93+ fn with_element ( self , other : impl Upcast < T > ) -> Set < T > {
94+ self . clone ( ) . with_element ( other)
95+ }
96+
97+ fn without_element ( self , other : impl Upcast < T > ) -> Set < T > {
98+ self . clone ( ) . without_element ( other)
99+ }
100+
101+ fn split_nth ( & self , i : usize ) -> Option < ( T , Set < T > ) > {
102+ <Set < T > >:: split_nth ( self , i)
103+ }
80104}
81105
82106impl < T : Ord + Clone > SetExt < T > for Set < T > {
@@ -87,22 +111,28 @@ impl<T: Ord + Clone> SetExt<T> for Set<T> {
87111 Some ( ( e, c) )
88112 }
89113
90- fn split_nth ( & self , i : usize ) -> Option < ( T , Self ) > {
114+ fn split_nth ( & self , i : usize ) -> Option < ( T , Set < T > ) > {
91115 let mut s = self . clone ( ) ;
92116 let item = self . iter ( ) . skip ( i) . next ( ) ?;
93117 let item = s. take ( item) . unwrap ( ) ;
94118 Some ( ( item, s) )
95119 }
96120
97- fn union_with ( mut self , other : Self ) -> Self {
121+ fn union_with ( mut self , other : impl Upcast < Set < T > > ) -> Set < T > {
122+ let other: Set < T > = other. upcast ( ) ;
98123 for item in other {
99124 self . insert ( item) ;
100125 }
101126 self
102127 }
103128
104- fn plus ( mut self , other : T ) -> Self {
105- self . insert ( other) ;
129+ fn with_element ( mut self , other : impl Upcast < T > ) -> Set < T > {
130+ self . insert ( other. upcast ( ) ) ;
131+ self
132+ }
133+
134+ fn without_element ( mut self , other : impl Upcast < T > ) -> Set < T > {
135+ self . remove ( & other. upcast ( ) ) ;
106136 self
107137 }
108138}
0 commit comments