From 0320559a6474959a2c85ff91d02a55cbf62f75a3 Mon Sep 17 00:00:00 2001 From: xmo-odoo Date: Wed, 12 Feb 2025 08:20:32 +0100 Subject: [PATCH] Add is_first/is_last helpers to Position They're just convenience method, but avoid having to dereference individual variants, and may mitigate missing the `Only` case. --- src/with_position.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/with_position.rs b/src/with_position.rs index 2d56bb9b2..a86454ba1 100644 --- a/src/with_position.rs +++ b/src/with_position.rs @@ -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 Iterator for WithPosition { type Item = (Position, I::Item);