Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/generic-array/src/functional.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / generic-array / src / functional.rs` 95 lines ยท 3448 bytes Raw Blame History 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 8 use crate::sequence::*; 9 10 /// Defines the relationship between one generic sequence and another, 11 /// for operations such as `map` and `zip`. 12 pub unsafe trait MappedGenericSequence: GenericSequence 13 where 14 Self::Length: ArrayLength, 15 { 16 /// Mapped sequence type 17 type Mapped: GenericSequence; 18 } 19 20 unsafe impl<'a, T, U, S: MappedGenericSequence> MappedGenericSequence for &'a S 21 where 22 &'a S: GenericSequence, 23 S: GenericSequence>::Length>, 24 >::Length: ArrayLength, 25 { 26 type Mapped = >::Mapped; 27 } 28 29 unsafe impl<'a, T, U, S: MappedGenericSequence> MappedGenericSequence for &'a mut S 30 where 31 &'a mut S: GenericSequence, 32 S: GenericSequence>::Length>, 33 >::Length: ArrayLength, 34 { 35 type Mapped = >::Mapped; 36 } 37 38 /// Accessor type for a mapped generic sequence 39 pub type MappedSequence = 40 <>::Mapped as GenericSequence>::Sequence; 41 42 /// Defines functional programming methods for generic sequences 43 pub unsafe trait FunctionalSequence: GenericSequence { 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. 48 fn map(self, f: F) -> MappedSequence 49 where 50 Self: MappedGenericSequence, 51 Self::Length: ArrayLength, 52 F: FnMut(Self::Item) -> U, 53 { 54 FromIterator::from_iter(self.into_iter().map(f)) 55 } 56 57 /// 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] 63 fn zip(self, rhs: Rhs, f: F) -> MappedSequence 64 where 65 Self: MappedGenericSequence, 66 Rhs: MappedGenericSequence>, 67 Self::Length: ArrayLength + ArrayLength, 68 Rhs: GenericSequence, 69 F: FnMut(Self::Item, Rhs::Item) -> U, 70 { 71 rhs.inverted_zip2(self, f) 72 } 73 74 /// Folds (or reduces) a sequence of data into a single value. 75 /// 76 /// If the fold function panics, any unused elements will be dropped. 77 fn fold(self, init: U, f: F) -> U 78 where 79 F: FnMut(U, Self::Item) -> U, 80 { 81 self.into_iter().fold(init, f) 82 } 83 } 84 85 unsafe impl<'a, T, S: GenericSequence> FunctionalSequence for &'a S 86 where 87 &'a S: GenericSequence, 88 { 89 } 90 91 unsafe impl<'a, T, S: GenericSequence> FunctionalSequence for &'a mut S 92 where 93 &'a mut S: GenericSequence, 94 { 95 }