@@ -70,13 +70,37 @@ impl<T: Ord + Clone> VecExt<T> for Vec<T> {
70
70
}
71
71
72
72
pub trait SetExt < T > : Sized {
73
- fn split_first ( self ) -> Option < ( T , Self ) > ;
73
+ fn split_first ( self ) -> Option < ( T , Set < T > ) > ;
74
74
75
- fn union_with ( self , other : Self ) -> Self ;
75
+ fn union_with ( self , other : impl Upcast < Set < T > > ) -> Set < T > ;
76
76
77
- fn plus ( self , other : T ) -> Self ;
77
+ fn with_element ( self , other : impl Upcast < T > ) -> Set < T > ;
78
78
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
+ }
80
104
}
81
105
82
106
impl < T : Ord + Clone > SetExt < T > for Set < T > {
@@ -87,22 +111,28 @@ impl<T: Ord + Clone> SetExt<T> for Set<T> {
87
111
Some ( ( e, c) )
88
112
}
89
113
90
- fn split_nth ( & self , i : usize ) -> Option < ( T , Self ) > {
114
+ fn split_nth ( & self , i : usize ) -> Option < ( T , Set < T > ) > {
91
115
let mut s = self . clone ( ) ;
92
116
let item = self . iter ( ) . skip ( i) . next ( ) ?;
93
117
let item = s. take ( item) . unwrap ( ) ;
94
118
Some ( ( item, s) )
95
119
}
96
120
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 ( ) ;
98
123
for item in other {
99
124
self . insert ( item) ;
100
125
}
101
126
self
102
127
}
103
128
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 ( ) ) ;
106
136
self
107
137
}
108
138
}
0 commit comments