]>
git.proxmox.com Git - rustc.git/blob - src/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 pub struct WithPosition
<I
>
12 peekable
: Peekable
<Fuse
<I
>>,
15 /// Create a new `WithPosition` iterator.
16 pub fn with_position
<I
>(iter
: I
) -> WithPosition
<I
>
21 peekable
: iter
.fuse().peekable(),
25 /// A value yielded by `WithPosition`.
26 /// Indicates the position of this element in the iterator results.
28 /// See [`.with_position()`](trait.Itertools.html#method.with_position) for more information.
29 #[derive(Copy, Clone, Debug, PartialEq)]
30 pub enum Position
<T
> {
31 /// This is the first element.
33 /// This is neither the first nor the last element.
35 /// This is the last element.
37 /// This is the only element.
42 /// Return the inner value.
43 pub fn into_inner(self) -> T
{
48 Position
::Only(x
) => x
,
53 impl<I
: Iterator
> Iterator
for WithPosition
<I
> {
54 type Item
= Position
<I
::Item
>;
56 fn next(&mut self) -> Option
<Self::Item
> {
57 match self.peekable
.next() {
59 if !self.handled_first
{
60 // Haven't seen the first item yet, and there is one to give.
61 self.handled_first
= true;
62 // Peek to see if this is also the last item,
63 // in which case tag it as `Only`.
64 match self.peekable
.peek() {
65 Some(_
) => Some(Position
::First(item
)),
66 None
=> Some(Position
::Only(item
)),
69 // Have seen the first item, and there's something left.
70 // Peek to see if this is the last item.
71 match self.peekable
.peek() {
72 Some(_
) => Some(Position
::Middle(item
)),
73 None
=> Some(Position
::Last(item
)),
77 // Iterator is finished.
82 fn size_hint(&self) -> (usize, Option
<usize>) {
83 self.peekable
.size_hint()
87 impl<I
> ExactSizeIterator
for WithPosition
<I
>
88 where I
: ExactSizeIterator
,