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
pub(crate) mod iterator {
use crate::derive::*;
pub(crate) const NAME: &[&str] = &["Iterator"];
pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
let try_trait = quote! {
#[inline]
fn fold<__U, __F>(self, init: __U, f: __F) -> __U
where
__F: ::core::ops::FnMut(__U, Self::Item) -> __U;
#[inline]
fn find<__P>(&mut self, predicate: __P) -> ::core::option::Option<Self::Item>
where
__P: ::core::ops::FnMut(&Self::Item) -> bool;
#[inline]
fn find_map<__U, __F>(&mut self, f: __F) -> ::core::option::Option<__U>
where
__F: ::core::ops::FnMut(Self::Item) -> ::core::option::Option<__U>;
};
Ok(derive_trait(data, parse_quote!(::core::iter::Iterator), None, parse_quote! {
trait Iterator {
type Item;
#[inline]
fn next(&mut self) -> ::core::option::Option<Self::Item>;
#[inline]
fn size_hint(&self) -> (usize, ::core::option::Option<usize>);
#[inline]
fn count(self) -> usize;
#[inline]
fn last(self) -> ::core::option::Option<Self::Item>;
#[inline]
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
fn collect<__U: ::core::iter::FromIterator<Self::Item>>(self) -> __U;
#try_trait
}
}))
}
}
pub(crate) mod double_ended_iterator {
use crate::derive::*;
pub(crate) const NAME: &[&str] = &["DoubleEndedIterator"];
pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
let try_trait = quote! {
#[inline]
fn rfold<__U, __F>(self, accum: __U, f: __F) -> __U
where
__F: ::core::ops::FnMut(__U, Self::Item) -> __U;
#[inline]
fn rfind<__P>(&mut self, predicate: __P) -> ::core::option::Option<Self::Item>
where
__P: ::core::ops::FnMut(&Self::Item) -> bool;
};
Ok(derive_trait(
data,
parse_quote!(::core::iter::DoubleEndedIterator),
Some(format_ident!("Item")),
parse_quote! {
trait DoubleEndedIterator: ::core::iter::Iterator {
#[inline]
fn next_back(&mut self) -> ::core::option::Option<Self::Item>;
#try_trait
}
},
))
}
}
pub(crate) mod exact_size_iterator {
use crate::derive::*;
pub(crate) const NAME: &[&str] = &["ExactSizeIterator"];
pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(
data,
parse_quote!(::core::iter::ExactSizeIterator),
Some(format_ident!("Item")),
parse_quote! {
trait ExactSizeIterator: ::core::iter::Iterator {
#[inline]
fn len(&self) -> usize;
}
},
))
}
}
pub(crate) mod fused_iterator {
use crate::derive::*;
pub(crate) const NAME: &[&str] = &["FusedIterator"];
pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(
data,
parse_quote!(::core::iter::FusedIterator),
Some(format_ident!("Item")),
parse_quote! {
trait FusedIterator: ::core::iter::Iterator {}
},
))
}
}
#[cfg(feature = "trusted_len")]
pub(crate) mod trusted_len {
use crate::derive::*;
pub(crate) const NAME: &[&str] = &["TrustedLen"];
pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(
data,
parse_quote!(::core::iter::TrustedLen),
Some(format_ident!("Item")),
parse_quote! {
unsafe trait TrustedLen: ::core::iter::Iterator {}
},
))
}
}
pub(crate) mod extend {
use crate::derive::*;
pub(crate) const NAME: &[&str] = &["Extend"];
pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(data, parse_quote!(::core::iter::Extend), None, parse_quote! {
trait Extend<__A> {
#[inline]
fn extend<__T: ::core::iter::IntoIterator<Item = __A>>(&mut self, iter: __T);
}
}))
}
}