]> git.proxmox.com Git - rustc.git/blob - vendor/generic-array-0.12.3/src/functional.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / generic-array-0.12.3 / src / functional.rs
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.
4
5 use super::ArrayLength;
6 use core::iter::FromIterator;
7 use sequence::*;
8
9 /// Defines the relationship between one generic sequence and another,
10 /// for operations such as `map` and `zip`.
11 pub unsafe trait MappedGenericSequence<T, U>: GenericSequence<T>
12 where
13 Self::Length: ArrayLength<U>,
14 {
15 /// Mapped sequence type
16 type Mapped: GenericSequence<U, Length = Self::Length>;
17 }
18
19 unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a S
20 where
21 &'a S: GenericSequence<T>,
22 S: GenericSequence<T, Length = <&'a S as GenericSequence<T>>::Length>,
23 <S as GenericSequence<T>>::Length: ArrayLength<U>,
24 {
25 type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
26 }
27
28 unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a mut S
29 where
30 &'a mut S: GenericSequence<T>,
31 S: GenericSequence<T, Length = <&'a mut S as GenericSequence<T>>::Length>,
32 <S as GenericSequence<T>>::Length: ArrayLength<U>,
33 {
34 type Mapped = <S as MappedGenericSequence<T, U>>::Mapped;
35 }
36
37 /// Accessor type for a mapped generic sequence
38 pub type MappedSequence<S, T, U> =
39 <<S as MappedGenericSequence<T, U>>::Mapped as GenericSequence<U>>::Sequence;
40
41 /// Defines functional programming methods for generic sequences
42 pub unsafe trait FunctionalSequence<T>: GenericSequence<T> {
43 /// Maps a `GenericSequence` to another `GenericSequence`.
44 ///
45 /// If the mapping function panics, any already initialized elements in the new sequence
46 /// will be dropped, AND any unused elements in the source sequence will also be dropped.
47 fn map<U, F>(self, f: F) -> MappedSequence<Self, T, U>
48 where
49 Self: MappedGenericSequence<T, U>,
50 Self::Length: ArrayLength<U>,
51 F: FnMut(Self::Item) -> U,
52 {
53 FromIterator::from_iter(self.into_iter().map(f))
54 }
55
56 /// Combines two `GenericSequence` instances and iterates through both of them,
57 /// initializing a new `GenericSequence` with the result of the zipped mapping function.
58 ///
59 /// If the mapping function panics, any already initialized elements in the new sequence
60 /// will be dropped, AND any unused elements in the source sequences will also be dropped.
61 #[inline]
62 fn zip<B, Rhs, U, F>(self, rhs: Rhs, f: F) -> MappedSequence<Self, T, U>
63 where
64 Self: MappedGenericSequence<T, U>,
65 Rhs: MappedGenericSequence<B, U, Mapped = MappedSequence<Self, T, U>>,
66 Self::Length: ArrayLength<B> + ArrayLength<U>,
67 Rhs: GenericSequence<B, Length = Self::Length>,
68 F: FnMut(Self::Item, Rhs::Item) -> U,
69 {
70 rhs.inverted_zip2(self, f)
71 }
72
73 /// Folds (or reduces) a sequence of data into a single value.
74 ///
75 /// If the fold function panics, any unused elements will be dropped.
76 fn fold<U, F>(self, init: U, f: F) -> U
77 where
78 F: FnMut(U, Self::Item) -> U,
79 {
80 self.into_iter().fold(init, f)
81 }
82 }
83
84 unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a S
85 where
86 &'a S: GenericSequence<T>,
87 {
88 }
89
90 unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a mut S
91 where
92 &'a mut S: GenericSequence<T>,
93 {
94 }