Skip to content

Commit 2744b23

Browse files
committed
Use iterators for comment children partial fix rust-lang#166
1 parent ebfa083 commit 2744b23

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

@@ -852,6 +845,26 @@ impl Comment {
852845
}
853846
}
854847

848+
/// An iterator for a comment's children
849+
pub struct CommentChildrenIterator {
850+
parent: CXComment,
851+
length: c_uint,
852+
index: c_uint
853+
}
854+
855+
impl Iterator for CommentChildrenIterator {
856+
type Item = Comment;
857+
fn next(&mut self) -> Option<Comment> {
858+
if self.index < self.length {
859+
let idx = self.index;
860+
self.index += 1;
861+
Some( Comment { x: unsafe { clang_Comment_getChild(self.parent, idx) } } )
862+
} else {
863+
None
864+
}
865+
}
866+
}
867+
855868
/// A source file.
856869
pub struct File {
857870
x: CXFile

src/ir/annotations.rs

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

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

0 commit comments

Comments
 (0)