]>
git.proxmox.com Git - rustc.git/blob - vendor/generic-array-0.12.3/src/functional.rs
1 //! Functional programming with generic sequences
3 //! Please see `tests/generics.rs` for examples of how to best use these in your generic functions.
5 use super::ArrayLength
;
6 use core
::iter
::FromIterator
;
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
>
13 Self::Length
: ArrayLength
<U
>,
15 /// Mapped sequence type
16 type Mapped
: GenericSequence
<U
, Length
= Self::Length
>;
19 unsafe impl<'a
, T
, U
, S
: MappedGenericSequence
<T
, U
>> MappedGenericSequence
<T
, U
> for &'a S
21 &'a S
: GenericSequence
<T
>,
22 S
: GenericSequence
<T
, Length
= <&'a S
as GenericSequence
<T
>>::Length
>,
23 <S
as GenericSequence
<T
>>::Length
: ArrayLength
<U
>,
25 type Mapped
= <S
as MappedGenericSequence
<T
, U
>>::Mapped
;
28 unsafe impl<'a
, T
, U
, S
: MappedGenericSequence
<T
, U
>> MappedGenericSequence
<T
, U
> for &'a
mut S
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
>,
34 type Mapped
= <S
as MappedGenericSequence
<T
, U
>>::Mapped
;
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
;
41 /// Defines functional programming methods for generic sequences
42 pub unsafe trait FunctionalSequence
<T
>: GenericSequence
<T
> {
43 /// Maps a `GenericSequence` to another `GenericSequence`.
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
>
49 Self: MappedGenericSequence
<T
, U
>,
50 Self::Length
: ArrayLength
<U
>,
51 F
: FnMut(Self::Item
) -> U
,
53 FromIterator
::from_iter(self.into_iter().map(f
))
56 /// Combines two `GenericSequence` instances and iterates through both of them,
57 /// initializing a new `GenericSequence` with the result of the zipped mapping function.
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.
62 fn zip
<B
, Rhs
, U
, F
>(self, rhs
: Rhs
, f
: F
) -> MappedSequence
<Self, T
, U
>
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
,
70 rhs
.inverted_zip2(self, f
)
73 /// Folds (or reduces) a sequence of data into a single value.
75 /// If the fold function panics, any unused elements will be dropped.
76 fn fold
<U
, F
>(self, init
: U
, f
: F
) -> U
78 F
: FnMut(U
, Self::Item
) -> U
,
80 self.into_iter().fold(init
, f
)
84 unsafe impl<'a
, T
, S
: GenericSequence
<T
>> FunctionalSequence
<T
> for &'a S
86 &'a S
: GenericSequence
<T
>,
90 unsafe impl<'a
, T
, S
: GenericSequence
<T
>> FunctionalSequence
<T
> for &'a
mut S
92 &'a
mut S
: GenericSequence
<T
>,