]>
git.proxmox.com Git - rustc.git/blob - vendor/windows-bindgen/src/rust/iterators.rs
3 pub fn writer(writer
: &Writer
, def
: TypeDef
, generics
: &[Type
], ident
: &TokenStream
, constraints
: &TokenStream
, _phantoms
: &TokenStream
, cfg
: &Cfg
) -> TokenStream
{
4 match writer
.reader
.type_def_type_name(def
) {
5 // If the type is IIterator<T> then simply implement the Iterator trait over top.
6 TypeName
::IIterator
=> {
8 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::Iterator
for IIterator
<T
> {
11 fn next(&mut self) -> ::core
::option
::Option
<Self::Item
> {
12 let result
= self.Current().ok();
15 self.MoveNext().ok()?
;
23 // If the type is IIterable<T> then implement the IntoIterator trait and rely on the resulting
24 // IIterator<T> returned by first() to implement the Iterator trait.
25 TypeName
::IIterable
=> {
27 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::IntoIterator
for IIterable
<T
> {
29 type IntoIter
= IIterator
<Self::Item
>;
31 fn into_iter(self) -> Self::IntoIter
{
32 ::core
::iter
::IntoIterator
::into_iter(&self)
35 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::IntoIterator
for &IIterable
<T
> {
37 type IntoIter
= IIterator
<Self::Item
>;
39 fn into_iter(self) -> Self::IntoIter
{
40 // TODO: not sure how to avoid this unwrap, although it should always succeed in practice.
46 // If the type is IVectorView<T> then provide the VectorViewIterator fast iterator.
47 TypeName
::IVectorView
=> {
49 pub struct VectorViewIterator
<T
: ::windows_core
::RuntimeType
+ '
static> {
50 vector
: ::core
::option
::Option
<IVectorView
<T
>>,
54 impl<T
: ::windows_core
::RuntimeType
> VectorViewIterator
<T
> {
55 pub fn new(vector
: ::core
::option
::Option
<IVectorView
<T
>>) -> Self {
56 Self { vector, current: 0 }
60 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::Iterator
for VectorViewIterator
<T
> {
63 fn next(&mut self) -> ::core
::option
::Option
<Self::Item
> {
66 vector
.GetAt(self.current
).ok()
75 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::IntoIterator
for IVectorView
<T
> {
77 type IntoIter
= VectorViewIterator
<Self::Item
>;
79 fn into_iter(self) -> Self::IntoIter
{
80 ::core
::iter
::IntoIterator
::into_iter(&self)
83 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::IntoIterator
for &IVectorView
<T
> {
85 type IntoIter
= VectorViewIterator
<Self::Item
>;
87 fn into_iter(self) -> Self::IntoIter
{
88 // TODO: shouldn't need to clone - VectorViewIterator should hold a reference
89 VectorViewIterator
::new(::core
::option
::Option
::Some(::core
::clone
::Clone
::clone(self)))
94 TypeName
::IVector
=> {
96 pub struct VectorIterator
<T
: ::windows_core
::RuntimeType
+ '
static> {
97 vector
: ::core
::option
::Option
<IVector
<T
>>,
101 impl<T
: ::windows_core
::RuntimeType
> VectorIterator
<T
> {
102 pub fn new(vector
: ::core
::option
::Option
<IVector
<T
>>) -> Self {
103 Self { vector, current: 0 }
107 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::Iterator
for VectorIterator
<T
> {
110 fn next(&mut self) -> ::core
::option
::Option
<Self::Item
> {
113 vector
.GetAt(self.current
).ok()
122 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::IntoIterator
for IVector
<T
> {
124 type IntoIter
= VectorIterator
<Self::Item
>;
126 fn into_iter(self) -> Self::IntoIter
{
127 ::core
::iter
::IntoIterator
::into_iter(&self)
130 impl<T
: ::windows_core
::RuntimeType
> ::core
::iter
::IntoIterator
for &IVector
<T
> {
132 type IntoIter
= VectorIterator
<Self::Item
>;
134 fn into_iter(self) -> Self::IntoIter
{
135 // TODO: shouldn't need to clone - VectorIterator should hold a reference
136 VectorIterator
::new(::core
::option
::Option
::Some(::core
::clone
::Clone
::clone(self)))
144 let wfc
= writer
.namespace("Windows.Foundation.Collections");
145 let mut iterable
= None
;
146 let interfaces
= type_interfaces(writer
.reader
, &Type
::TypeDef(def
, generics
.to_vec()));
148 // If the class or interface is not one of the well-known collection interfaces, we then see whether it
149 // implements any one of them. Here is where we favor IVectorView/IVector over IIterable.
150 for interface
in interfaces
{
151 if let Type
::TypeDef(interface
, interface_generics
) = &interface
.ty
{
152 match writer
.reader
.type_def_type_name(*interface
) {
153 TypeName
::IVectorView
=> {
154 let item
= writer
.type_name(&interface_generics
[0]);
155 let mut cfg
= cfg
.clone();
156 type_def_cfg_combine(writer
.reader
, *interface
, interface_generics
, &mut cfg
);
157 let features
= writer
.cfg_features(&cfg
);
161 impl<#constraints> ::core::iter::IntoIterator for #ident {
163 type IntoIter
= #wfc VectorViewIterator<Self::Item>;
165 fn into_iter(self) -> Self::IntoIter
{
166 ::core
::iter
::IntoIterator
::into_iter(&self)
170 impl<#constraints> ::core::iter::IntoIterator for &#ident {
172 type IntoIter
= #wfc VectorViewIterator<Self::Item>;
174 fn into_iter(self) -> Self::IntoIter
{
175 #wfc VectorViewIterator::new(::windows_core::ComInterface::cast(self).ok())
180 TypeName
::IVector
=> {
181 let item
= writer
.type_name(&interface_generics
[0]);
182 let mut cfg
= cfg
.clone();
183 type_def_cfg_combine(writer
.reader
, *interface
, interface_generics
, &mut cfg
);
184 let features
= writer
.cfg_features(&cfg
);
188 impl<#constraints> ::core::iter::IntoIterator for #ident {
190 type IntoIter
= #wfc VectorIterator<Self::Item>;
192 fn into_iter(self) -> Self::IntoIter
{
193 ::core
::iter
::IntoIterator
::into_iter(&self)
197 impl<#constraints> ::core::iter::IntoIterator for &#ident {
199 type IntoIter
= #wfc VectorIterator<Self::Item>;
201 fn into_iter(self) -> Self::IntoIter
{
202 #wfc VectorIterator::new(::windows_core::ComInterface::cast(self).ok())
207 TypeName
::IIterable
=> {
208 iterable
= Some((*interface
, interface_generics
.to_vec()));
216 None
=> TokenStream
::new(),
217 Some((interface
, interface_generics
)) => {
218 let item
= writer
.type_name(&interface_generics
[0]);
219 let mut cfg
= cfg
.clone();
220 type_def_cfg_combine(writer
.reader
, interface
, &interface_generics
, &mut cfg
);
221 let features
= writer
.cfg_features(&cfg
);
225 impl<#constraints> ::core::iter::IntoIterator for #ident {
227 type IntoIter
= #wfc IIterator<Self::Item>;
229 fn into_iter(self) -> Self::IntoIter
{
230 ::core
::iter
::IntoIterator
::into_iter(&self)
234 impl<#constraints> ::core::iter::IntoIterator for &#ident {
236 type IntoIter
= #wfc IIterator<Self::Item>;
238 fn into_iter(self) -> Self::IntoIter
{
239 // TODO: not sure how to avoid this unwrap, although it should always succeed in practice.
240 self.First().unwrap()