3
3
4
4
use std:: collections:: { BTreeMap , BTreeSet } ;
5
5
6
- use crate :: cast:: { DowncastTo , Upcast , UpcastFrom , Upcasted } ;
6
+ use crate :: {
7
+ cast:: { DowncastTo , Upcast , UpcastFrom , Upcasted } ,
8
+ Downcast ,
9
+ } ;
7
10
8
11
pub type Map < K , V > = BTreeMap < K , V > ;
9
12
pub type Set < E > = BTreeSet < E > ;
@@ -136,6 +139,31 @@ impl<T> DowncastTo<()> for Set<T> {
136
139
}
137
140
}
138
141
142
+ impl < A , T > DowncastTo < ( A , ) > for Set < T >
143
+ where
144
+ T : DowncastTo < A > + Ord ,
145
+ {
146
+ fn downcast_to ( & self ) -> Option < ( A , ) > {
147
+ if self . len ( ) == 1 {
148
+ let a: A = self . first ( ) . unwrap ( ) . downcast ( ) ?;
149
+ Some ( ( a, ) )
150
+ } else {
151
+ None
152
+ }
153
+ }
154
+ }
155
+
156
+ impl < A , T > UpcastFrom < ( A , ) > for Set < T >
157
+ where
158
+ A : Clone + Upcast < T > ,
159
+ T : Ord + Clone ,
160
+ {
161
+ fn upcast_from ( term : ( A , ) ) -> Self {
162
+ let ( a, ) = term;
163
+ set ! [ a. upcast( ) ]
164
+ }
165
+ }
166
+
139
167
impl < T > DowncastTo < ( ) > for Vec < T > {
140
168
fn downcast_to ( & self ) -> Option < ( ) > {
141
169
if self . is_empty ( ) {
@@ -146,6 +174,31 @@ impl<T> DowncastTo<()> for Vec<T> {
146
174
}
147
175
}
148
176
177
+ impl < A , T > DowncastTo < ( A , ) > for Vec < T >
178
+ where
179
+ T : DowncastTo < A > ,
180
+ {
181
+ fn downcast_to ( & self ) -> Option < ( A , ) > {
182
+ if self . len ( ) == 1 {
183
+ let a: A = self . first ( ) . unwrap ( ) . downcast ( ) ?;
184
+ Some ( ( a, ) )
185
+ } else {
186
+ None
187
+ }
188
+ }
189
+ }
190
+
191
+ impl < A , T > UpcastFrom < ( A , ) > for Vec < T >
192
+ where
193
+ A : Clone + Upcast < T > ,
194
+ T : Clone ,
195
+ {
196
+ fn upcast_from ( term : ( A , ) ) -> Self {
197
+ let ( a, ) = term;
198
+ vec ! [ a. upcast( ) ]
199
+ }
200
+ }
201
+
149
202
macro_rules! tuple_upcast {
150
203
( $coll: ident : $( $name: ident) ,* ) => {
151
204
/// Upcast from a tuple of iterable things into a collection.
0 commit comments