@@ -45,19 +45,36 @@ where
4545/// The first component of the value yielded by `WithPosition`.
4646/// Indicates the position of this element in the iterator results.
4747///
48+ /// When handling the [first](Position::is_first) or [last](Position::is_last) position,
49+ /// remember to consider the special case of [`Position::Only`].
50+ ///
4851/// See [`.with_position()`](crate::Itertools::with_position) for more information.
4952#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
5053pub enum Position {
51- /// This is the first element.
54+ /// This is the first element, and there is more than one element .
5255 First ,
5356 /// This is neither the first nor the last element.
5457 Middle ,
55- /// This is the last element.
58+ /// This is the last element, and there was more than one element .
5659 Last ,
5760 /// This is the only element.
61+ ///
62+ /// Makes [`.is_first()`](Position::is_first) and [`.is_last()`](Position::is_last) true at the same time.
5863 Only ,
5964}
6065
66+ impl Position {
67+ /// Unlike `position == First`, it's also true for [`Position::Only`]
68+ pub fn is_first ( self ) -> bool {
69+ self == Self :: First || self == Self :: Only
70+ }
71+
72+ /// Unlike `position == Last`, it's also true for [`Position::Only`]
73+ pub fn is_last ( self ) -> bool {
74+ self == Self :: Last || self == Self :: Only
75+ }
76+ }
77+
6178impl < I : Iterator > Iterator for WithPosition < I > {
6279 type Item = ( Position , I :: Item ) ;
6380
0 commit comments