Skip to content

Commit 6598f7a

Browse files
nikomatsakisNiko Matsakis
authored andcommitted
generalize the Cons upcast impls
1 parent a513555 commit 6598f7a

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

crates/formality-core/src/collections.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,15 @@ tuple_upcast!(Vec: A, B, C, D);
187187
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
188188
pub struct Cons<T, C>(pub T, pub C);
189189

190-
impl<T, U> UpcastFrom<Cons<U, Set<T>>> for Set<T>
190+
impl<T, U, V> UpcastFrom<Cons<U, Set<V>>> for Set<T>
191191
where
192192
T: Ord + Clone,
193193
U: Upcast<T>,
194+
V: Ord + Upcast<T>,
194195
{
195-
fn upcast_from(term: Cons<U, Set<T>>) -> Self {
196-
let Cons(elem, mut set) = term;
196+
fn upcast_from(term: Cons<U, Set<V>>) -> Self {
197+
let Cons(elem, set) = term;
198+
let mut set: Set<T> = set.upcast();
197199
set.insert(elem.upcast());
198200
set
199201
}
@@ -214,15 +216,16 @@ where
214216
}
215217
}
216218

217-
impl<T, U> UpcastFrom<Cons<U, Vec<T>>> for Vec<T>
219+
impl<T, U, V> UpcastFrom<Cons<U, Vec<V>>> for Vec<T>
218220
where
219-
T: Ord + Clone,
220221
U: Upcast<T>,
222+
V: Upcast<T>,
221223
{
222-
fn upcast_from(term: Cons<U, Vec<T>>) -> Self {
223-
let Cons(elem, mut vec) = term;
224-
vec.insert(0, elem.upcast());
225-
vec
224+
fn upcast_from(term: Cons<U, Vec<V>>) -> Self {
225+
let Cons(elem, vec) = term;
226+
std::iter::once(elem.upcast())
227+
.chain(vec.into_iter().upcasted())
228+
.collect()
226229
}
227230
}
228231

0 commit comments

Comments
 (0)