]>
git.proxmox.com Git - rustc.git/blob - vendor/itertools/src/with_position.rs
1 use std
::iter
::{Fuse,Peekable}
;
3 /// An iterator adaptor that wraps each element in an [`Position`](../enum.Position.html).
5 /// Iterator element type is `Position<I::Item>`.
7 /// See [`.with_position()`](../trait.Itertools.html#method.with_position) for more information.
8 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
9 pub struct WithPosition
<I
>
13 peekable
: Peekable
<Fuse
<I
>>,
16 /// Create a new `WithPosition` iterator.
17 pub fn with_position
<I
>(iter
: I
) -> WithPosition
<I
>
22 peekable
: iter
.fuse().peekable(),
26 /// A value yielded by `WithPosition`.
27 /// Indicates the position of this element in the iterator results.
29 /// See [`.with_position()`](trait.Itertools.html#method.with_position) for more information.
30 #[derive(Copy, Clone, Debug, PartialEq)]
31 pub enum Position
<T
> {
32 /// This is the first element.
34 /// This is neither the first nor the last element.
36 /// This is the last element.
38 /// This is the only element.
43 /// Return the inner value.
44 pub fn into_inner(self) -> T
{
49 Position
::Only(x
) => x
,
54 impl<I
: Iterator
> Iterator
for WithPosition
<I
> {
55 type Item
= Position
<I
::Item
>;
57 fn next(&mut self) -> Option
<Self::Item
> {
58 match self.peekable
.next() {
60 if !self.handled_first
{
61 // Haven't seen the first item yet, and there is one to give.
62 self.handled_first
= true;
63 // Peek to see if this is also the last item,
64 // in which case tag it as `Only`.
65 match self.peekable
.peek() {
66 Some(_
) => Some(Position
::First(item
)),
67 None
=> Some(Position
::Only(item
)),
70 // Have seen the first item, and there's something left.
71 // Peek to see if this is the last item.
72 match self.peekable
.peek() {
73 Some(_
) => Some(Position
::Middle(item
)),
74 None
=> Some(Position
::Last(item
)),
78 // Iterator is finished.
83 fn size_hint(&self) -> (usize, Option
<usize>) {
84 self.peekable
.size_hint()
88 impl<I
> ExactSizeIterator
for WithPosition
<I
>
89 where I
: ExactSizeIterator
,