1//! Functional programming with generic sequences
2//!
3//! Please see `tests/generics.rs` for examples of how to best use these in your generic functions.
45use super::ArrayLength;
6use core::iter::FromIterator;
78use crate::sequence::*;
910/// Defines the relationship between one generic sequence and another,
11/// for operations such as `map` and `zip`.
12pub unsafe trait MappedGenericSequence<T, U>: GenericSequence<T>
13where
14Self::Length: ArrayLength<U>,
15{
16/// Mapped sequence type
17type Mapped: GenericSequence<U, Length = Self::Length>;
18}
1920unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a S
21where
22&'a S: GenericSequence<T>,
23 S: GenericSequence<T, Length = <&'a S as GenericSequence<T>>::Length>,
24 <S as GenericSequence<T>>::Length: ArrayLength<U>,
25{
26type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
27}
2829unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a mut S
30where
31&'a mut S: GenericSequence<T>,
32 S: GenericSequence<T, Length = <&'a mut S as GenericSequence<T>>::Length>,
33 <S as GenericSequence<T>>::Length: ArrayLength<U>,
34{
35type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
36}
3738/// Accessor type for a mapped generic sequence
39pub type MappedSequence<S, T, U> =
40 <<S as MappedGenericSequence<T, U>>::Mapped as GenericSequence<U>>::Sequence;
4142/// Defines functional programming methods for generic sequences
43pub unsafe trait FunctionalSequence<T>: GenericSequence<T> {
44/// Maps a `GenericSequence` to another `GenericSequence`.
45 ///
46 /// If the mapping function panics, any already initialized elements in the new sequence
47 /// will be dropped, AND any unused elements in the source sequence will also be dropped.
48fn map<U, F>(self, f: F) -> MappedSequence<Self, T, U>
49where
50Self: MappedGenericSequence<T, U>,
51Self::Length: ArrayLength<U>,
52 F: FnMut(Self::Item) -> U,
53 {
54 FromIterator::from_iter(self.into_iter().map(f))
55 }
5657/// Combines two `GenericSequence` instances and iterates through both of them,
58 /// initializing a new `GenericSequence` with the result of the zipped mapping function.
59 ///
60 /// If the mapping function panics, any already initialized elements in the new sequence
61 /// will be dropped, AND any unused elements in the source sequences will also be dropped.
62#[inline]
63fn zip<B, Rhs, U, F>(self, rhs: Rhs, f: F) -> MappedSequence<Self, T, U>
64where
65Self: MappedGenericSequence<T, U>,
66 Rhs: MappedGenericSequence<B, U, Mapped = MappedSequence<Self, T, U>>,
67Self::Length: ArrayLength<B> + ArrayLength<U>,
68 Rhs: GenericSequence<B, Length = Self::Length>,
69 F: FnMut(Self::Item, Rhs::Item) -> U,
70 {
71 rhs.inverted_zip2(self, f)
72 }
7374/// Folds (or reduces) a sequence of data into a single value.
75 ///
76 /// If the fold function panics, any unused elements will be dropped.
77fn fold<U, F>(self, init: U, f: F) -> U
78where
79F: FnMut(U, Self::Item) -> U,
80 {
81self.into_iter().fold(init, f)
82 }
83}
8485unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a S
86where
87&'a S: GenericSequence<T>,
88{
89}
9091unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a mut S
92where
93&'a mut S: GenericSequence<T>,
94{
95}