Skip to content

Commit 4985a7c

Browse files
committed
Specialize GenericSequence and zip more
1 parent 5c8d021 commit 4985a7c

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/functional.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ where
2424
type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
2525
}
2626

27+
unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a mut S
28+
where
29+
&'a mut S: GenericSequence<T>,
30+
S: GenericSequence<T, Length=<&'a mut S as GenericSequence<T>>::Length>,
31+
<S as GenericSequence<T>>::Length: ArrayLength<U>,
32+
{
33+
type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
34+
}
35+
2736
/// Accessor type for a mapped generic sequence
2837
pub type MappedSequence<S, T, U> = <<S as MappedGenericSequence<T, U>>::Mapped as GenericSequence<U>>::Sequence;
2938

@@ -47,15 +56,16 @@ pub unsafe trait FunctionalSequence<T>: GenericSequence<T> {
4756
///
4857
/// If the mapping function panics, any already initialized elements in the new sequence
4958
/// will be dropped, AND any unused elements in the source sequences will also be dropped.
50-
fn zip<B, Rhs, U, F>(self, rhs: Rhs, mut f: F) -> MappedSequence<Self, T, U>
59+
#[inline]
60+
fn zip<B, Rhs, U, F>(self, rhs: Rhs, f: F) -> MappedSequence<Self, T, U>
5161
where
5262
Self: MappedGenericSequence<T, U>,
5363
Rhs: MappedGenericSequence<B, U, Mapped=MappedSequence<Self, T, U>>,
5464
Self::Length: ArrayLength<B> + ArrayLength<U>,
5565
Rhs: GenericSequence<B, Length=Self::Length>,
5666
F: FnMut(SequenceItem<Self>, SequenceItem<Rhs>) -> U,
5767
{
58-
FromIterator::from_iter(self.into_iter().zip(rhs.into_iter()).map(|(l, r)| f(l, r) ))
68+
rhs.inverted_zip2(self, f)
5969
}
6070
}
6171

src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,26 @@ where
324324
f(left_value, right_value)
325325
}))
326326
}
327+
328+
fn inverted_zip2<B, Lhs, U, F>(self, lhs: Lhs, mut f: F) -> MappedSequence<Lhs, B, U>
329+
where
330+
Lhs: GenericSequence<B, Length=Self::Length> + MappedGenericSequence<B, U>,
331+
Self: MappedGenericSequence<T, U>,
332+
Self::Length: ArrayLength<B> + ArrayLength<U>,
333+
F: FnMut(SequenceItem<Lhs>, SequenceItem<Self>) -> U
334+
{
335+
let mut right = ArrayConsumer::new(self);
336+
337+
let ArrayConsumer { array: ref right_array, position: ref mut right_position } = right;
338+
339+
FromIterator::from_iter(lhs.into_iter().zip(right_array.iter()).map(|(left_value, r)| {
340+
let right_value = unsafe { ptr::read(r) };
341+
342+
*right_position += 1;
343+
344+
f(left_value, right_value)
345+
}))
346+
}
327347
}
328348

329349
unsafe impl<T, U, N> MappedGenericSequence<T, U> for GenericArray<T, N>
@@ -506,9 +526,9 @@ mod test {
506526
use functional::*;
507527

508528
let a = black_box(arr![i32; 1, 3, 5, 7]);
509-
let b = black_box(arr![i32; 2, 4, 6, 8]);
529+
let mut b = black_box(arr![i32; 2, 4, 6, 8]);
510530

511-
let c = a.zip(b, |l, r| l + r);
531+
let c = (a).zip(&mut b, |l, r| l + *r);
512532

513533
assert_eq!(c, arr![i32; 3, 7, 11, 15]);
514534
}

src/sequence.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ pub unsafe trait GenericSequence<T>: Sized + IntoIterator {
4545
f(left_value, right_value)
4646
}))
4747
}
48+
49+
#[doc(hidden)]
50+
fn inverted_zip2<B, Lhs, U, F>(self, lhs: Lhs, mut f: F) -> MappedSequence<Lhs, B, U>
51+
where
52+
Lhs: GenericSequence<B, Length=Self::Length> + MappedGenericSequence<B, U>,
53+
Self: MappedGenericSequence<T, U>,
54+
Self::Length: ArrayLength<B> + ArrayLength<U>,
55+
F: FnMut(SequenceItem<Lhs>, SequenceItem<Self>) -> U
56+
{
57+
FromIterator::from_iter(lhs.into_iter().zip(self.into_iter()).map(|(l, r)| f(l, r) ))
58+
}
4859
}
4960

5061
/// Accessor type for iteration items from `GenericSequence`

0 commit comments

Comments
 (0)