summaryrefslogtreecommitdiffstats
path: root/vendor/windows-bindgen/src/rust/iterators.rs
blob: 76483129c6e44490bca6f04e93ad2c22285738ee (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::*;

pub fn writer(writer: &Writer, def: TypeDef, generics: &[Type], ident: &TokenStream, constraints: &TokenStream, _phantoms: &TokenStream, cfg: &Cfg) -> TokenStream {
    match def.type_name() {
        // If the type is IIterator<T> then simply implement the Iterator trait over top.
        TypeName::IIterator => {
            return quote! {
                impl<T: ::windows_core::RuntimeType> ::core::iter::Iterator for IIterator<T> {
                    type Item = T;

                    fn next(&mut self) -> ::core::option::Option<Self::Item> {
                        let result = self.Current().ok();

                        if result.is_some() {
                            self.MoveNext().ok()?;
                        }

                        result
                    }
                }
            };
        }
        // If the type is IIterable<T> then implement the IntoIterator trait and rely on the resulting
        // IIterator<T> returned by first() to implement the Iterator trait.
        TypeName::IIterable => {
            return quote! {
                impl<T: ::windows_core::RuntimeType> ::core::iter::IntoIterator for IIterable<T> {
                    type Item = T;
                    type IntoIter = IIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        ::core::iter::IntoIterator::into_iter(&self)
                    }
                }
                impl<T: ::windows_core::RuntimeType> ::core::iter::IntoIterator for &IIterable<T> {
                    type Item = T;
                    type IntoIter = IIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        // TODO: not sure how to avoid this unwrap, although it should always succeed in practice.
                        self.First().unwrap()
                    }
                }
            };
        }
        // If the type is IVectorView<T> then provide the VectorViewIterator fast iterator.
        TypeName::IVectorView => {
            return quote! {
                pub struct VectorViewIterator<T: ::windows_core::RuntimeType + 'static> {
                    vector: ::core::option::Option<IVectorView<T>>,
                    current: u32,
                }

                impl<T: ::windows_core::RuntimeType> VectorViewIterator<T> {
                    pub fn new(vector: ::core::option::Option<IVectorView<T>>) -> Self {
                        Self { vector, current: 0 }
                    }
                }

                impl<T: ::windows_core::RuntimeType> ::core::iter::Iterator for VectorViewIterator<T> {
                    type Item = T;

                    fn next(&mut self) -> ::core::option::Option<Self::Item> {
                        self.vector.as_ref()
                            .and_then(|vector| {
                                vector.GetAt(self.current).ok()
                            })
                            .and_then(|result| {
                                self.current += 1;
                                Some(result)
                            })
                    }
                }

                impl<T: ::windows_core::RuntimeType> ::core::iter::IntoIterator for IVectorView<T> {
                    type Item = T;
                    type IntoIter = VectorViewIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        ::core::iter::IntoIterator::into_iter(&self)
                    }
                }
                impl<T: ::windows_core::RuntimeType> ::core::iter::IntoIterator for &IVectorView<T> {
                    type Item = T;
                    type IntoIter = VectorViewIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        // TODO: shouldn't need to clone - VectorViewIterator should hold a reference
                        VectorViewIterator::new(::core::option::Option::Some(::core::clone::Clone::clone(self)))
                    }
                }
            };
        }
        TypeName::IVector => {
            return quote! {
                pub struct VectorIterator<T: ::windows_core::RuntimeType + 'static> {
                    vector: ::core::option::Option<IVector<T>>,
                    current: u32,
                }

                impl<T: ::windows_core::RuntimeType> VectorIterator<T> {
                    pub fn new(vector: ::core::option::Option<IVector<T>>) -> Self {
                        Self { vector, current: 0 }
                    }
                }

                impl<T: ::windows_core::RuntimeType> ::core::iter::Iterator for VectorIterator<T> {
                    type Item = T;

                    fn next(&mut self) -> ::core::option::Option<Self::Item> {
                        self.vector.as_ref()
                            .and_then(|vector| {
                                vector.GetAt(self.current).ok()
                            })
                            .and_then(|result| {
                                self.current += 1;
                                Some(result)
                            })
                    }
                }

                impl<T: ::windows_core::RuntimeType> ::core::iter::IntoIterator for IVector<T> {
                    type Item = T;
                    type IntoIter = VectorIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        ::core::iter::IntoIterator::into_iter(&self)
                    }
                }
                impl<T: ::windows_core::RuntimeType> ::core::iter::IntoIterator for &IVector<T> {
                    type Item = T;
                    type IntoIter = VectorIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        // TODO: shouldn't need to clone - VectorIterator should hold a reference
                        VectorIterator::new(::core::option::Option::Some(::core::clone::Clone::clone(self)))
                    }
                }
            };
        }
        _ => {}
    }

    let wfc = writer.namespace("Windows.Foundation.Collections");
    let mut iterable = None;
    let interfaces = type_interfaces(&Type::TypeDef(def, generics.to_vec()));

    // If the class or interface is not one of the well-known collection interfaces, we then see whether it
    // implements any one of them. Here is where we favor IVectorView/IVector over IIterable.
    for interface in interfaces {
        if let Type::TypeDef(interface, interface_generics) = &interface.ty {
            match interface.type_name() {
                TypeName::IVectorView => {
                    let item = writer.type_name(&interface_generics[0]);
                    let mut cfg = cfg.clone();
                    type_def_cfg_combine(*interface, interface_generics, &mut cfg);
                    let features = writer.cfg_features(&cfg);

                    return quote! {
                        #features
                        impl<#constraints> ::core::iter::IntoIterator for #ident {
                            type Item = #item;
                            type IntoIter = #wfc VectorViewIterator<Self::Item>;

                            fn into_iter(self) -> Self::IntoIter {
                                ::core::iter::IntoIterator::into_iter(&self)
                            }
                        }
                        #features
                        impl<#constraints> ::core::iter::IntoIterator for &#ident {
                            type Item = #item;
                            type IntoIter = #wfc VectorViewIterator<Self::Item>;

                            fn into_iter(self) -> Self::IntoIter {
                                #wfc VectorViewIterator::new(::windows_core::ComInterface::cast(self).ok())
                            }
                        }
                    };
                }
                TypeName::IVector => {
                    let item = writer.type_name(&interface_generics[0]);
                    let mut cfg = cfg.clone();
                    type_def_cfg_combine(*interface, interface_generics, &mut cfg);
                    let features = writer.cfg_features(&cfg);

                    return quote! {
                        #features
                        impl<#constraints> ::core::iter::IntoIterator for #ident {
                            type Item = #item;
                            type IntoIter = #wfc VectorIterator<Self::Item>;

                            fn into_iter(self) -> Self::IntoIter {
                                ::core::iter::IntoIterator::into_iter(&self)
                            }
                        }
                        #features
                        impl<#constraints> ::core::iter::IntoIterator for &#ident {
                            type Item = #item;
                            type IntoIter = #wfc VectorIterator<Self::Item>;

                            fn into_iter(self) -> Self::IntoIter {
                                #wfc VectorIterator::new(::windows_core::ComInterface::cast(self).ok())
                            }
                        }
                    };
                }
                TypeName::IIterable => {
                    iterable = Some((*interface, interface_generics.to_vec()));
                }
                _ => {}
            }
        }
    }

    match iterable {
        None => TokenStream::new(),
        Some((interface, interface_generics)) => {
            let item = writer.type_name(&interface_generics[0]);
            let mut cfg = cfg.clone();
            type_def_cfg_combine(interface, &interface_generics, &mut cfg);
            let features = writer.cfg_features(&cfg);

            quote! {
                #features
                impl<#constraints> ::core::iter::IntoIterator for #ident {
                    type Item = #item;
                    type IntoIter = #wfc IIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        ::core::iter::IntoIterator::into_iter(&self)
                    }
                }
                #features
                impl<#constraints> ::core::iter::IntoIterator for &#ident {
                    type Item = #item;
                    type IntoIter = #wfc IIterator<Self::Item>;

                    fn into_iter(self) -> Self::IntoIter {
                        // TODO: not sure how to avoid this unwrap, although it should always succeed in practice.
                        self.First().unwrap()
                    }
                }
            }
        }
    }
}