-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
double_ended_iterator.rs
40 lines (37 loc) · 1.28 KB
/
double_ended_iterator.rs
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
use crate::utils::*;
pub(crate) const NAME: &[&str] = &["DoubleEndedIterator"];
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
#[cfg(feature = "try_trait")]
let try_trait = quote! {
#[inline]
fn try_rfold<__U, __F, __R>(&mut self, init: __U, f: __F) -> __R
where
__F: ::core::ops::FnMut(__U, Self::Item) -> __R,
__R: ::core::ops::Try<Ok = __U>;
};
// It is equally efficient if `try_rfold` can be used.
#[cfg(not(feature = "try_trait"))]
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;
};
derive_trait!(
data,
Some(ident("Item")),
parse_quote!(::core::iter::DoubleEndedIterator)?,
parse_quote! {
trait DoubleEndedIterator: ::core::iter::Iterator {
#[inline]
fn next_back(&mut self) -> ::core::option::Option<Self::Item>;
#try_trait
}
}?,
)
.map(|item| items.push(item))
}