diff options
| author | kennytm <kennytm@gmail.com> | 2018-01-13 02:26:31 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-13 02:26:31 +0800 |
| commit | f4ff4c01f1d47cc1fae27c63da21ad4e1f94433e (patch) | |
| tree | 22062d2e7cad3a15e54e87756e95249b949c580f | |
| parent | e40a6fb1336bb2025b2ef348c7caf4b4f39d90cd (diff) | |
| parent | ce4673df39892f415472745c0aeb695ae0ab9703 (diff) | |
| download | rust-f4ff4c01f1d47cc1fae27c63da21ad4e1f94433e.tar.gz rust-f4ff4c01f1d47cc1fae27c63da21ad4e1f94433e.zip | |
Rollup merge of #47305 - cramertj:better-calendar-alone, r=eddyb
Use copy/clone closures to simplify calendar test Split out from #47304 r? @eddyb
| -rw-r--r-- | src/test/run-pass/impl-trait/example-calendar.rs | 61 |
1 files 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 8d035bafab7..aca100591dd 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: <https://raw.githubusercontent.com/quickfur/dcal/master/dcal.d>. @@ -234,42 +236,6 @@ impl Weekday { } } -/// Wrapper for zero-sized closures. -// HACK(eddyb) Only needed because closures can't implement Copy. -struct Fn0<F>(std::marker::PhantomData<F>); - -impl<F> Copy for Fn0<F> {} -impl<F> Clone for Fn0<F> { - fn clone(&self) -> Self { *self } -} - -impl<F: FnOnce<A>, A> FnOnce<A> for Fn0<F> { - type Output = F::Output; - - extern "rust-call" fn call_once(self, args: A) -> Self::Output { - let f = unsafe { std::mem::uninitialized::<F>() }; - f.call_once(args) - } -} - -impl<F: FnMut<A>, A> FnMut<A> for Fn0<F> { - extern "rust-call" fn call_mut(&mut self, args: A) -> Self::Output { - let mut f = unsafe { std::mem::uninitialized::<F>() }; - f.call_mut(args) - } -} - -trait AsFn0<A>: Sized { - fn copyable(self) -> Fn0<Self>; -} - -impl<F: FnMut<A>, A> AsFn0<A> for F { - fn copyable(self) -> Fn0<Self> { - assert_eq!(std::mem::size_of::<F>(), 0); - Fn0(std::marker::PhantomData) - } -} - /// GroupBy implementation. struct GroupBy<It: Iterator, F> { it: std::iter::Peekable<It>, @@ -277,11 +243,15 @@ struct GroupBy<It: Iterator, F> { } impl<It, F> Clone for GroupBy<It, F> -where It: Iterator + Clone, It::Item: Clone, F: Clone { - fn clone(&self) -> GroupBy<It, F> { +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<It: Iterator, F: FnMut(&It::Item) -> G, G: Eq> Iterator for InGroup<It, F, } trait IteratorExt: Iterator + Sized { - fn group_by<G, F>(self, f: F) -> GroupBy<Self, Fn0<F>> - where F: FnMut(&Self::Item) -> G, + fn group_by<G, F>(self, f: F) -> GroupBy<Self, F> + 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<Item=NaiveDate>+Clone { InGroup { it: NaiveDate::from_ymd(year, 1, 1).., - f: (|d: &NaiveDate| d.year()).copyable(), + f: |d: &NaiveDate| d.year(), g: year } } |
