query: Fix invalid use of slice::from_raw_parts

This commit is contained in:
Max Brunsfeld 2020-10-05 12:07:16 -07:00
parent 2a3c2ad6b9
commit d1c95193c1
3 changed files with 54 additions and 2 deletions

View file

@ -138,7 +138,7 @@ pub struct QueryCaptures<'a, T: AsRef<[u8]>> {
}
/// A particular `Node` that has been captured with a particular name within a `Query`.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct QueryCapture<'a> {
pub node: Node<'a>,
@ -1272,7 +1272,11 @@ impl Query {
let mut length = 0u32;
let raw_predicates =
ffi::ts_query_predicates_for_pattern(ptr, i as u32, &mut length as *mut u32);
if length > 0 {
slice::from_raw_parts(raw_predicates, length as usize)
} else {
&[]
}
};
let byte_offset = unsafe { ffi::ts_query_start_byte_for_pattern(ptr, i as u32) };
@ -1649,11 +1653,15 @@ impl<'a> QueryMatch<'a> {
cursor,
id: m.id,
pattern_index: m.pattern_index as usize,
captures: unsafe {
captures: if m.capture_count > 0 {
unsafe {
slice::from_raw_parts(
m.captures as *const QueryCapture<'a>,
m.capture_count as usize,
)
}
} else {
&[]
},
}
}
@ -1729,6 +1737,16 @@ impl<'a, T: AsRef<[u8]>> Iterator for QueryCaptures<'a, T> {
}
}
impl<'a> fmt::Debug for QueryMatch<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"QueryMatch {{ id: {}, pattern_index: {}, captures: {:?} }}",
self.id, self.pattern_index, self.captures
)
}
}
impl PartialEq for Query {
fn eq(&self, other: &Self) -> bool {
self.ptr == other.ptr