Skip to content

Commit ae194f9

Browse files
committed
support upcast from Cons to construct collections
1 parent d678ede commit ae194f9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

crates/formality-core/src/collections.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,24 @@ tuple_upcast!(Vec: A, B, C);
152152
tuple_upcast!(Vec: A, B, C, D);
153153

154154
/// This type exists to be used in judgment functions.
155-
/// You can downcast a `Vec` or `Set` to `Cons(head, tail)`
155+
/// You can upcast/downcast a `Vec` or `Set` to `Cons(head, tail)`
156156
/// where `head` will be the first item in the collection
157157
/// and tail will be a collection with the remaining items.
158-
/// Both can also be downcast to `()` which matches an empty collection.
158+
/// Both can also be upcast/downcast to `()` which matches an empty collection.
159159
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
160160
pub struct Cons<T, C>(pub T, pub C);
161161

162+
impl<T> UpcastFrom<Cons<T, Set<T>>> for Set<T>
163+
where
164+
T: Ord + Clone,
165+
{
166+
fn upcast_from(term: Cons<T, Set<T>>) -> Self {
167+
let Cons(elem, mut set) = term;
168+
set.insert(elem);
169+
set
170+
}
171+
}
172+
162173
impl<T> DowncastTo<Cons<T, Set<T>>> for Set<T>
163174
where
164175
T: Ord + Clone,
@@ -174,6 +185,17 @@ where
174185
}
175186
}
176187

188+
impl<T> UpcastFrom<Cons<T, Vec<T>>> for Vec<T>
189+
where
190+
T: Ord + Clone,
191+
{
192+
fn upcast_from(term: Cons<T, Vec<T>>) -> Self {
193+
let Cons(elem, mut vec) = term;
194+
vec.insert(0, elem);
195+
vec
196+
}
197+
}
198+
177199
impl<T> DowncastTo<Cons<T, Vec<T>>> for Vec<T>
178200
where
179201
T: Ord + Clone,

0 commit comments

Comments
 (0)