Skip to content

Commit f4ff4c0

Browse files
authored
Rollup merge of rust-lang#47305 - cramertj:better-calendar-alone, r=eddyb
Use copy/clone closures to simplify calendar test Split out from rust-lang#47304 r? @eddyb
2 parents e40a6fb + ce4673d commit f4ff4c0

File tree

1 file changed

+14
-47
lines changed

1 file changed

+14
-47
lines changed

src/test/run-pass/impl-trait/example-calendar.rs

+14-47
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
universal_impl_trait,
1616
fn_traits,
1717
step_trait,
18-
unboxed_closures
18+
unboxed_closures,
19+
copy_closures,
20+
clone_closures
1921
)]
2022

2123
//! Derived from: <https://raw.githubusercontent.com/quickfur/dcal/master/dcal.d>.
@@ -234,54 +236,22 @@ impl Weekday {
234236
}
235237
}
236238

237-
/// Wrapper for zero-sized closures.
238-
// HACK(eddyb) Only needed because closures can't implement Copy.
239-
struct Fn0<F>(std::marker::PhantomData<F>);
240-
241-
impl<F> Copy for Fn0<F> {}
242-
impl<F> Clone for Fn0<F> {
243-
fn clone(&self) -> Self { *self }
244-
}
245-
246-
impl<F: FnOnce<A>, A> FnOnce<A> for Fn0<F> {
247-
type Output = F::Output;
248-
249-
extern "rust-call" fn call_once(self, args: A) -> Self::Output {
250-
let f = unsafe { std::mem::uninitialized::<F>() };
251-
f.call_once(args)
252-
}
253-
}
254-
255-
impl<F: FnMut<A>, A> FnMut<A> for Fn0<F> {
256-
extern "rust-call" fn call_mut(&mut self, args: A) -> Self::Output {
257-
let mut f = unsafe { std::mem::uninitialized::<F>() };
258-
f.call_mut(args)
259-
}
260-
}
261-
262-
trait AsFn0<A>: Sized {
263-
fn copyable(self) -> Fn0<Self>;
264-
}
265-
266-
impl<F: FnMut<A>, A> AsFn0<A> for F {
267-
fn copyable(self) -> Fn0<Self> {
268-
assert_eq!(std::mem::size_of::<F>(), 0);
269-
Fn0(std::marker::PhantomData)
270-
}
271-
}
272-
273239
/// GroupBy implementation.
274240
struct GroupBy<It: Iterator, F> {
275241
it: std::iter::Peekable<It>,
276242
f: F,
277243
}
278244

279245
impl<It, F> Clone for GroupBy<It, F>
280-
where It: Iterator + Clone, It::Item: Clone, F: Clone {
281-
fn clone(&self) -> GroupBy<It, F> {
246+
where
247+
It: Iterator + Clone,
248+
It::Item: Clone,
249+
F: Clone,
250+
{
251+
fn clone(&self) -> Self {
282252
GroupBy {
283253
it: self.it.clone(),
284-
f: self.f.clone()
254+
f: self.f.clone(),
285255
}
286256
}
287257
}
@@ -331,14 +301,11 @@ impl<It: Iterator, F: FnMut(&It::Item) -> G, G: Eq> Iterator for InGroup<It, F,
331301
}
332302

333303
trait IteratorExt: Iterator + Sized {
334-
fn group_by<G, F>(self, f: F) -> GroupBy<Self, Fn0<F>>
335-
where F: FnMut(&Self::Item) -> G,
304+
fn group_by<G, F>(self, f: F) -> GroupBy<Self, F>
305+
where F: Clone + FnMut(&Self::Item) -> G,
336306
G: Eq
337307
{
338-
GroupBy {
339-
it: self.peekable(),
340-
f: f.copyable(),
341-
}
308+
GroupBy { it: self.peekable(), f }
342309
}
343310

344311
fn join(mut self, sep: &str) -> String
@@ -382,7 +349,7 @@ fn test_spaces() {
382349
fn dates_in_year(year: i32) -> impl Iterator<Item=NaiveDate>+Clone {
383350
InGroup {
384351
it: NaiveDate::from_ymd(year, 1, 1)..,
385-
f: (|d: &NaiveDate| d.year()).copyable(),
352+
f: |d: &NaiveDate| d.year(),
386353
g: year
387354
}
388355
}

0 commit comments

Comments
 (0)