[librsvg: 1/9] node.rs: Impl DoubleEndedIterator for Children
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/9] node.rs: Impl DoubleEndedIterator for Children
- Date: Thu, 15 Mar 2018 20:52:45 +0000 (UTC)
commit dc61a094f0bc67f023a5179710fbb23326320407
Author: Ivan Molodetskikh <yalterz gmail com>
Date: Thu Mar 15 20:51:12 2018 +0300
node.rs: Impl DoubleEndedIterator for Children
rsvg_internals/src/node.rs | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
---
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index ae7a59f4..c14af5a7 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -72,6 +72,7 @@ pub struct Node {
pub struct Children<'a> {
children: Ref<'a, Vec<Rc<Node>>>,
index: usize,
+ reverse_index: usize,
}
// Keep this in sync with rsvg-private.h:RsvgNodeType
@@ -293,7 +294,12 @@ pub fn boxed_node_new(
impl<'a> Children<'a> {
fn new(children: Ref<'a, Vec<Rc<Node>>>) -> Self {
- Self { children, index: 0 }
+ let len = children.len();
+ Self {
+ children,
+ index: 0,
+ reverse_index: len,
+ }
}
}
@@ -301,7 +307,7 @@ impl<'a> Iterator for Children<'a> {
type Item = Rc<Node>;
fn next(&mut self) -> Option<Self::Item> {
- if self.index == self.children.len() {
+ if self.index == self.reverse_index {
return None;
}
@@ -311,11 +317,22 @@ impl<'a> Iterator for Children<'a> {
}
fn size_hint(&self) -> (usize, Option<usize>) {
- let count = self.children.len() - self.index;
+ let count = self.reverse_index - self.index;
(count, Some(count))
}
}
+impl<'a> DoubleEndedIterator for Children<'a> {
+ fn next_back(&mut self) -> Option<Self::Item> {
+ if self.index == self.reverse_index {
+ return None;
+ }
+
+ self.reverse_index -= 1;
+ Some(self.children[self.reverse_index].clone())
+ }
+}
+
impl<'a> ExactSizeIterator for Children<'a> {}
#[no_mangle]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]