1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Wrapper over COM iteration ability
//!
//! I could not manage to make the COM API `_NewEnum` work, so I'm implementing iterators myself instead

use std::marker::PhantomData;

use crate::wrappers::Iterable;
use super::LONG;

pub struct Iterator<'a, Obj, Item> {
    data: &'a Obj,
    count: LONG,
    current: LONG,
    items: PhantomData<Item>,
}

impl<'a, Obj, Items> Iterator<'a, Obj, Items>
where Obj: Iterable + Iterable<Item = Items>
{
    pub(crate) fn new(data: &'a Obj) -> windows::core::Result<Self> {
        let count = data.Count()?;

        Ok(Self {
            data,
            count,
            current: 0,
            items: PhantomData,
        })
    }
}

impl<'a, Obj, Items> std::iter::Iterator for Iterator<'a, Obj, Items>
where Obj: Iterable + Iterable<Item = Items>
{
    type Item = Items;

    fn next(&mut self) -> Option<Self::Item> {
        // COM iterators (or at least iTunes iterators) are 1-based.
        // Let's increment the index _before_ we access it.
        self.current += 1;
        self.data.item(self.current).ok()
    }
}

impl<'a, Obj, Items> std::iter::ExactSizeIterator for Iterator<'a, Obj, Items>
where Obj: Iterable + Iterable<Item = Items>
{
    fn len(&self) -> usize {
        self.count as usize
    }
}

impl<'a, Obj, Items> std::iter::DoubleEndedIterator for Iterator<'a, Obj, Items>
where Obj: Iterable + Iterable<Item = Items>
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.current -= 1;
        self.data.item(self.current).ok()
    }
}