From ce4673df39892f415472745c0aeb695ae0ab9703 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 9 Jan 2018 16:00:12 -0800 Subject: [PATCH] Use copy/clone closures to simplify calendar test --- .../run-pass/impl-trait/example-calendar.rs | 61 +++++-------------- 1 file changed, 14 insertions(+), 47 deletions(-) diff --git a/src/test/run-pass/impl-trait/example-calendar.rs b/src/test/run-pass/impl-trait/example-calendar.rs index 8d035bafab78d..aca100591ddec 100644 --- a/src/test/run-pass/impl-trait/example-calendar.rs +++ b/src/test/run-pass/impl-trait/example-calendar.rs @@ -15,7 +15,9 @@ universal_impl_trait, fn_traits, step_trait, - unboxed_closures + unboxed_closures, + copy_closures, + clone_closures )] //! Derived from: . @@ -234,42 +236,6 @@ impl Weekday { } } -/// Wrapper for zero-sized closures. -// HACK(eddyb) Only needed because closures can't implement Copy. -struct Fn0(std::marker::PhantomData); - -impl Copy for Fn0 {} -impl Clone for Fn0 { - fn clone(&self) -> Self { *self } -} - -impl, A> FnOnce for Fn0 { - type Output = F::Output; - - extern "rust-call" fn call_once(self, args: A) -> Self::Output { - let f = unsafe { std::mem::uninitialized::() }; - f.call_once(args) - } -} - -impl, A> FnMut for Fn0 { - extern "rust-call" fn call_mut(&mut self, args: A) -> Self::Output { - let mut f = unsafe { std::mem::uninitialized::() }; - f.call_mut(args) - } -} - -trait AsFn0: Sized { - fn copyable(self) -> Fn0; -} - -impl, A> AsFn0 for F { - fn copyable(self) -> Fn0 { - assert_eq!(std::mem::size_of::(), 0); - Fn0(std::marker::PhantomData) - } -} - /// GroupBy implementation. struct GroupBy { it: std::iter::Peekable, @@ -277,11 +243,15 @@ struct GroupBy { } impl Clone for GroupBy -where It: Iterator + Clone, It::Item: Clone, F: Clone { - fn clone(&self) -> GroupBy { +where + It: Iterator + Clone, + It::Item: Clone, + F: Clone, +{ + fn clone(&self) -> Self { GroupBy { it: self.it.clone(), - f: self.f.clone() + f: self.f.clone(), } } } @@ -331,14 +301,11 @@ impl G, G: Eq> Iterator for InGroup(self, f: F) -> GroupBy> - where F: FnMut(&Self::Item) -> G, + fn group_by(self, f: F) -> GroupBy + where F: Clone + FnMut(&Self::Item) -> G, G: Eq { - GroupBy { - it: self.peekable(), - f: f.copyable(), - } + GroupBy { it: self.peekable(), f } } fn join(mut self, sep: &str) -> String @@ -382,7 +349,7 @@ fn test_spaces() { fn dates_in_year(year: i32) -> impl Iterator+Clone { InGroup { it: NaiveDate::from_ymd(year, 1, 1).., - f: (|d: &NaiveDate| d.year()).copyable(), + f: |d: &NaiveDate| d.year(), g: year } }