Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/stackable-operator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- `iter::reverse_if` helper ([#838]).

[#838]: https://github.com/stackabletech/operator-rs/pull/838

## [0.73.0] - 2024-08-09

### Added
Expand Down
59 changes: 59 additions & 0 deletions crates/stackable-operator/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
//! Extensions for use cases that [`Iterator`] doesn't handle natively quite yet

use std::iter::Rev;

/// Either an `A` or `B`, and forwards all iterator methods accordingly.
pub enum Either<A, B> {
Left(A),
Right(B),
}

impl<A, B> Iterator for Either<A, B>
where
A: Iterator,
B: Iterator<Item = A::Item>,
{
type Item = A::Item;

fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Left(left) => left.next(),
Self::Right(right) => right.next(),
}
}
}

impl<A, B> DoubleEndedIterator for Either<A, B>
where
Self: Iterator,
A: DoubleEndedIterator<Item = Self::Item>,
B: DoubleEndedIterator<Item = Self::Item>,
{
fn next_back(&mut self) -> Option<Self::Item> {
match self {
Self::Left(left) => left.next_back(),
Self::Right(right) => right.next_back(),
}
}
}

/// Returns a reversed version of `iter` if `cond` is `true`, otherwise returns it as-is.
pub fn reverse_if<I: DoubleEndedIterator>(cond: bool, iter: I) -> Either<I, Rev<I>> {
if cond {
Either::Right(iter.rev())
} else {
Either::Left(iter)
}
}

/// Fallible version of [`Iterator::flatten`]
///
/// If the outer [`Iterator`] returns [`Ok`] then each item in the inner iterator is emitted,
Expand Down Expand Up @@ -61,6 +107,19 @@ pub trait TryFromIterator<T>: Sized {
mod tests {
use super::*;

#[test]
fn reverse_if_marble_test() {
let values = [0, 1, 2, 3];
assert_eq!(
reverse_if(false, values.iter()).collect::<Vec<_>>(),
[&0, &1, &2, &3]
);
assert_eq!(
reverse_if(true, values.iter()).collect::<Vec<_>>(),
[&3, &2, &1, &0]
);
}

#[test]
fn try_flatten_marble_test() {
let stream = vec![
Expand Down