Skip to content

Commit

Permalink
Add is_first/is_last helpers to Position
Browse files Browse the repository at this point in the history
They're just convenience method, but avoid having to dereference individual variants, and may mitigate missing the `Only` case.
  • Loading branch information
xmo-odoo authored Feb 12, 2025
1 parent 9238090 commit 0320559
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/with_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ pub enum Position {
Only,
}

impl Position {
/// Whether the current element is the first of the iterator,
/// regardless of length (includes both [`Position::First`]
/// and [`Position::Only`]).
pub fn is_first(&self) -> bool {
matches!(self, Self::First | Self::Only)
}
/// Whether the current element is the last of the iterator,
/// regardless of length (includes both [`Position::Last`]
/// and [`Position::Only`]).
pub fn is_last(&self) -> bool {
matches!(self, Self::Only | Self::Last)
}
}

impl<I: Iterator> Iterator for WithPosition<I> {
type Item = (Position, I::Item);

Expand Down

0 comments on commit 0320559

Please sign in to comment.