Skip to content

Commit

Permalink
Merge branch 'master' into add-cargo-semver-check-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen-CH-Leung committed Jan 19, 2024
2 parents cf701f6 + f4afe79 commit 629e854
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
on:
push:
branches: [master]
paths-ignore:
- "**.md"
pull_request:
paths-ignore:
- "**.md"

name: Code Coverage

jobs:
coverage:
name: coverage
runs-on: ubuntu-latest
steps:
- name: checkout source
uses: actions/checkout@v4

- name: Install nightly toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: llvm-tools-preview

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Run llvm-cov
run: cargo llvm-cov --all-features --doctests --workspace --lcov --output-path lcov.info

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: lcov.info
14 changes: 14 additions & 0 deletions src/peeking_take_while.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::PutBack;
#[cfg(feature = "use_alloc")]
use crate::PutBackN;
use crate::RepeatN;
use std::iter::Peekable;

/// An iterator that allows peeking at an element before deciding to accept it.
Expand Down Expand Up @@ -91,6 +92,19 @@ where
}
}

impl<T: Clone> PeekingNext for RepeatN<T> {
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
where
F: FnOnce(&Self::Item) -> bool,
{
let r = self.elt.as_ref()?;
if !accept(r) {
return None;
}
self.next()
}
}

/// An iterator adaptor that takes items while a closure returns `true`.
///
/// See [`.peeking_take_while()`](crate::Itertools::peeking_take_while)
Expand Down
2 changes: 1 addition & 1 deletion src/repeatn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::iter::FusedIterator;
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone, Debug)]
pub struct RepeatN<A> {
elt: Option<A>,
pub(crate) elt: Option<A>,
n: usize,
}

Expand Down
16 changes: 16 additions & 0 deletions tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::it::izip;
use crate::it::multipeek;
use crate::it::multizip;
use crate::it::peek_nth;
use crate::it::repeat_n;
use crate::it::ExactlyOneError;
use crate::it::FoldWhile;
use crate::it::Itertools;
Expand Down Expand Up @@ -674,6 +675,21 @@ fn test_multipeek_peeking_next() {
assert_eq!(mp.peek(), None);
}

#[test]
fn test_repeat_n_peeking_next() {
use crate::it::PeekingNext;
let mut rn = repeat_n(0, 5);
assert_eq!(rn.peeking_next(|&x| x != 0), None);
assert_eq!(rn.peeking_next(|&x| x <= 0), Some(0));
assert_eq!(rn.next(), Some(0));
assert_eq!(rn.peeking_next(|&x| x <= 0), Some(0));
assert_eq!(rn.peeking_next(|&x| x != 0), None);
assert_eq!(rn.peeking_next(|&x| x >= 0), Some(0));
assert_eq!(rn.next(), Some(0));
assert_eq!(rn.peeking_next(|&x| x <= 0), None);
assert_eq!(rn.next(), None);
}

#[test]
fn test_peek_nth() {
let nums = vec![1u8, 2, 3, 4, 5];
Expand Down

0 comments on commit 629e854

Please sign in to comment.