-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See example usage: https://github.com/RustCrypto/AEADs/blob/d1a22983d2af3c6a1ac3644850351f0ab0a77e51/eax/src/online.rs#L352
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ extern crate std; | |
|
||
mod from_fn; | ||
mod iter; | ||
mod lib; | ||
mod sizes; | ||
mod traits; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! Functional programming with [`Array`]s. | ||
// This is modeled after `generic-array::functional` | ||
// see: <https://docs.rs/generic-array/1.0.0/generic_array/functional/index.html> | ||
|
||
use crate::{Array, ArraySize}; | ||
|
||
impl<T, U> Array<T, U> | ||
where | ||
U: ArraySize, | ||
{ | ||
/// Combines two `Array` instances and iterates through both of them, initialization a new | ||
/// `Array` with the result of the zipped mapping function. | ||
/// | ||
/// If the mapping function panics, any already initialized elements in the new array will | ||
/// be dropped, AND any unused elements in the source arrays will also be dropped. | ||
#[inline(always)] | ||
pub fn zip<Rhs, F, O>(self, rhs: Array<Rhs, U>, f: F) -> Array<O, U> | ||
where | ||
U: ArraySize, | ||
F: FnMut(T, Rhs) -> O, | ||
{ | ||
Zipper { | ||
inner: self.into_iter(), | ||
rhs: rhs.into_iter(), | ||
f, | ||
} | ||
.collect() | ||
} | ||
} | ||
|
||
struct Zipper<I, R, F> { | ||
inner: I, | ||
rhs: R, | ||
f: F, | ||
} | ||
|
||
impl<I, T, R, RT, O, F> Iterator for Zipper<I, R, F> | ||
where | ||
I: Iterator<Item = T>, | ||
R: Iterator<Item = RT>, | ||
F: FnMut(T, RT) -> O, | ||
{ | ||
type Item = O; | ||
|
||
#[inline(always)] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some((self.f)(self.inner.next()?, self.rhs.next()?)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters