Skip to content

Commit 2b605fb

Browse files
committed
Use iterators for comment children partial fix rust-lang#166
1 parent c15e221 commit 2b605fb

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/clang.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -785,19 +785,12 @@ impl Comment {
785785
}
786786
}
787787

788-
/// Get the number of children this comment node has.
789-
pub fn num_children(&self) -> c_uint {
790-
unsafe {
791-
clang_Comment_getNumChildren(self.x)
792-
}
793-
}
794-
795-
/// Get this comment's `idx`th child comment
796-
pub fn get_child(&self, idx: c_uint) -> Option<Comment> {
797-
if idx >= self.num_children() {
798-
None
799-
} else {
800-
Some(Comment { x: unsafe { clang_Comment_getChild(self.x, idx) } })
788+
/// Get this comment's children comment
789+
pub fn get_children(&self) -> CommentChildrenIntoIterator {
790+
CommentChildrenIntoIterator {
791+
parent: self.x,
792+
length: unsafe { clang_Comment_getNumChildren(self.x) },
793+
index: 0
801794
}
802795
}
803796

@@ -834,6 +827,27 @@ impl Comment {
834827
}
835828
}
836829

830+
/// An iterator for a comment's children
831+
pub struct CommentChildrenIntoIterator {
832+
parent: CXComment,
833+
length: c_uint,
834+
index: c_uint
835+
}
836+
837+
impl Iterator for CommentChildrenIntoIterator {
838+
type Item = Comment;
839+
fn next(&mut self) -> Option<Comment> {
840+
if self.index < self.length {
841+
let idx = self.index;
842+
self.index += 1;
843+
Some( Comment { x: unsafe { clang_Comment_getChild(self.parent, idx) } } )
844+
}
845+
else {
846+
None
847+
}
848+
}
849+
}
850+
837851
/// A source file.
838852
pub struct File {
839853
x: CXFile

src/ir/annotations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ impl Annotations {
152152
}
153153
}
154154

155-
for i in 0..comment.num_children() {
156-
self.parse(&comment.get_child(i).unwrap(), matched);
155+
for child in comment.get_children() {
156+
self.parse(&child, matched);
157157
}
158158
}
159159
}

0 commit comments

Comments
 (0)