Rollup merge of #47305 - cramertj:better-calendar-alone, r=eddyb

Use copy/clone closures to simplify calendar test

Split out from #47304

r? @eddyb
This commit is contained in:
kennytm 2018-01-13 02:26:31 +08:00 committed by GitHub
commit f4ff4c01f1

View file

@ -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
}
}