Skip to content

Commit 5157b05

Browse files
committed
Use iterators for comment children partial fix rust-lang#166
1 parent 8504570 commit 5157b05

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/clang.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -791,19 +791,12 @@ impl Comment {
791791
}
792792
}
793793

794-
/// Get the number of children this comment node has.
795-
pub fn num_children(&self) -> c_uint {
796-
unsafe {
797-
clang_Comment_getNumChildren(self.x)
798-
}
799-
}
800-
801-
/// Get this comment's `idx`th child comment
802-
pub fn get_child(&self, idx: c_uint) -> Option<Comment> {
803-
if idx >= self.num_children() {
804-
None
805-
} else {
806-
Some(Comment { x: unsafe { clang_Comment_getChild(self.x, idx) } })
794+
/// Get this comment's children comment
795+
pub fn get_children(&self) -> CommentChildrenIterator {
796+
CommentChildrenIterator {
797+
parent: self.x,
798+
length: unsafe { clang_Comment_getNumChildren(self.x) },
799+
index: 0
807800
}
808801
}
809802

@@ -840,6 +833,26 @@ impl Comment {
840833
}
841834
}
842835

836+
/// An iterator for a comment's children
837+
pub struct CommentChildrenIterator {
838+
parent: CXComment,
839+
length: c_uint,
840+
index: c_uint
841+
}
842+
843+
impl Iterator for CommentChildrenIterator {
844+
type Item = Comment;
845+
fn next(&mut self) -> Option<Comment> {
846+
if self.index < self.length {
847+
let idx = self.index;
848+
self.index += 1;
849+
Some( Comment { x: unsafe { clang_Comment_getChild(self.parent, idx) } } )
850+
} else {
851+
None
852+
}
853+
}
854+
}
855+
843856
/// A source file.
844857
pub struct File {
845858
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)