Skip to content

Commit cd578bc

Browse files
committed
Implement new functional system
1 parent 7350c00 commit cd578bc

File tree

7 files changed

+302
-127
lines changed

7 files changed

+302
-127
lines changed

src/functional.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Functional programming with generic sequences
2+
3+
use core::iter::FromIterator;
4+
5+
use super::ArrayLength;
6+
use sequence::*;
7+
8+
/// Defines the relationship between one generic sequence and another,
9+
/// for operations such as `map` and `zip`.
10+
pub unsafe trait MappedGenericSequence<T, U>: GenericSequence<T>
11+
where
12+
Self::Length: ArrayLength<U>,
13+
{
14+
/// Mapped sequence type
15+
type Mapped: GenericSequence<U, Length=Self::Length>;
16+
}
17+
18+
unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a S
19+
where
20+
&'a S: GenericSequence<T>,
21+
S: GenericSequence<T, Length=<&'a S as GenericSequence<T>>::Length>,
22+
<S as GenericSequence<T>>::Length: ArrayLength<U>,
23+
{
24+
type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
25+
}
26+
27+
/// Accessor type for a mapped generic sequence
28+
pub type MappedSequence<S, T, U> = <<S as MappedGenericSequence<T, U>>::Mapped as GenericSequence<U>>::Sequence;
29+
30+
/// Defines functional programming methods for generic sequences
31+
pub unsafe trait FunctionalSequence<T>: GenericSequence<T> {
32+
/// Maps a `GenericSequence` to another `GenericSequence`.
33+
///
34+
/// If the mapping function panics, any already initialized elements in the new sequence
35+
/// will be dropped, AND any unused elements in the source sequence will also be dropped.
36+
fn map<U, F>(self, f: F) -> MappedSequence<Self, T, U>
37+
where
38+
Self: MappedGenericSequence<T, U>,
39+
Self::Length: ArrayLength<U>,
40+
F: Fn(SequenceItem<Self>) -> U,
41+
{
42+
FromIterator::from_iter(self.into_iter().map(f))
43+
}
44+
45+
/// Combines two `GenericSequence` instances and iterates through both of them,
46+
/// initializing a new `GenericSequence` with the result of the zipped mapping function.
47+
///
48+
/// If the mapping function panics, any already initialized elements in the new sequence
49+
/// 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, f: F) -> MappedSequence<Self, T, U>
51+
where
52+
Self: MappedGenericSequence<T, U>,
53+
Self::Length: ArrayLength<B> + ArrayLength<U>,
54+
Rhs: GenericSequence<B>,
55+
F: Fn(SequenceItem<Self>, SequenceItem<Rhs>) -> U,
56+
{
57+
FromIterator::from_iter(self.into_iter().zip(rhs.into_iter()).map(|(l, r)| f(l, r) ))
58+
}
59+
}
60+
61+
unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a S
62+
where
63+
&'a S: GenericSequence<T>,
64+
{}
65+
66+
unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a mut S
67+
where
68+
&'a mut S: GenericSequence<T>,
69+
{}

src/impls.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use super::{ArrayLength, GenericArray};
21
use core::borrow::{Borrow, BorrowMut};
32
use core::cmp::Ordering;
43
use core::fmt::{self, Debug};
54
use core::hash::{Hash, Hasher};
65

6+
use super::{ArrayLength, GenericArray};
7+
use sequence::*;
8+
use functional::*;
9+
710
impl<T: Default, N> Default for GenericArray<T, N>
811
where
912
N: ArrayLength<T>,
@@ -19,7 +22,7 @@ where
1922
N: ArrayLength<T>,
2023
{
2124
fn clone(&self) -> GenericArray<T, N> {
22-
self.map_ref(|x| x.clone())
25+
self.map(|x| x.clone())
2326
}
2427
}
2528

src/iter.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub struct GenericArrayIter<T, N: ArrayLength<T>> {
1414
index_back: usize,
1515
}
1616

17+
unsafe impl<T: Send, N: ArrayLength<T>> Send for GenericArrayIter<T, N> {}
18+
unsafe impl<T: Sync, N: ArrayLength<T>> Sync for GenericArrayIter<T, N> {}
19+
1720
impl<T, N> IntoIterator for GenericArray<T, N>
1821
where
1922
N: ArrayLength<T>,
@@ -34,6 +37,7 @@ impl<T, N> Drop for GenericArrayIter<T, N>
3437
where
3538
N: ArrayLength<T>,
3639
{
40+
#[inline]
3741
fn drop(&mut self) {
3842
// Drop values that are still alive.
3943
for p in &mut self.array[self.index..self.index_back] {
@@ -50,23 +54,28 @@ where
5054
{
5155
type Item = T;
5256

57+
#[inline]
5358
fn next(&mut self) -> Option<T> {
54-
if self.len() > 0 {
55-
unsafe {
56-
let p = self.array.get_unchecked(self.index);
57-
self.index += 1;
58-
Some(ptr::read(p))
59-
}
59+
if self.index < self.index_back {
60+
let p = unsafe {
61+
Some(ptr::read(self.array.get_unchecked(self.index)))
62+
};
63+
64+
self.index += 1;
65+
66+
p
6067
} else {
6168
None
6269
}
6370
}
6471

72+
#[inline]
6573
fn size_hint(&self) -> (usize, Option<usize>) {
6674
let len = self.len();
6775
(len, Some(len))
6876
}
6977

78+
#[inline]
7079
fn count(self) -> usize {
7180
self.len()
7281
}
@@ -95,11 +104,11 @@ where
95104
N: ArrayLength<T>,
96105
{
97106
fn next_back(&mut self) -> Option<T> {
98-
if self.len() > 0 {
107+
if self.index < self.index_back {
99108
self.index_back -= 1;
109+
100110
unsafe {
101-
let p = self.array.get_unchecked(self.index_back);
102-
Some(ptr::read(p))
111+
Some(ptr::read(self.array.get_unchecked(self.index_back)))
103112
}
104113
} else {
105114
None
@@ -114,4 +123,4 @@ where
114123
fn len(&self) -> usize {
115124
self.index_back - self.index
116125
}
117-
}
126+
}

0 commit comments

Comments
 (0)