Skip to content

Commit 36c6c8a

Browse files
authored
Merge pull request tree-sitter#2423 from amaanq/nonzero-field-id
feat!: use `Option<NonZeroU16>` for TSFieldIds
2 parents 0787c24 + 99366f3 commit 36c6c8a

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

cli/src/tests/parser_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ fn test_parsing_with_a_timeout() {
665665
assert!(start_time.elapsed().as_micros() < 2000);
666666

667667
#[cfg(target_arch = "sparc64")]
668-
assert!(start_time.elapsed().as_micros() < 4000);
668+
assert!(start_time.elapsed().as_micros() < 8000);
669669

670670
// Continue parsing, but pause after 1 ms of processing.
671671
parser.set_timeout_micros(5000);

lib/binding_rust/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
fmt, hash, iter,
1111
marker::PhantomData,
1212
mem::MaybeUninit,
13+
num::NonZeroU16,
1314
ops,
1415
os::raw::{c_char, c_void},
1516
ptr::{self, NonNull},
@@ -93,6 +94,8 @@ pub enum LogType {
9394
Lex,
9495
}
9596

97+
type FieldId = NonZeroU16;
98+
9699
/// A callback that receives log messages during parser.
97100
type Logger<'a> = Box<dyn FnMut(LogType, &str) + 'a>;
98101

@@ -319,7 +322,7 @@ impl Language {
319322

320323
/// Get the numerical id for the given field name.
321324
#[doc(alias = "ts_language_field_id_for_name")]
322-
pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option<u16> {
325+
pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option<FieldId> {
323326
let field_name = field_name.as_ref();
324327
let id = unsafe {
325328
ffi::ts_language_field_id_for_name(
@@ -331,7 +334,7 @@ impl Language {
331334
if id == 0 {
332335
None
333336
} else {
334-
Some(id)
337+
Some(FieldId::new(id).unwrap())
335338
}
336339
}
337340
}
@@ -1060,23 +1063,23 @@ impl<'tree> Node<'tree> {
10601063
cursor: &'a mut TreeCursor<'tree>,
10611064
) -> impl Iterator<Item = Node<'tree>> + 'a {
10621065
let field_id = self.language().field_id_for_name(field_name);
1063-
self.children_by_field_id(field_id.unwrap_or(0), cursor)
1066+
self.children_by_field_id(field_id, cursor)
10641067
}
10651068

10661069
/// Iterate over this node's children with a given field id.
10671070
///
10681071
/// See also [Node::children_by_field_name].
10691072
pub fn children_by_field_id<'a>(
10701073
&self,
1071-
field_id: u16,
1074+
field_id: Option<FieldId>,
10721075
cursor: &'a mut TreeCursor<'tree>,
10731076
) -> impl Iterator<Item = Node<'tree>> + 'a {
10741077
cursor.reset(*self);
10751078
cursor.goto_first_child();
10761079
let mut done = false;
10771080
iter::from_fn(move || {
10781081
while !done {
1079-
while cursor.field_id() != Some(field_id) {
1082+
while cursor.field_id() != field_id {
10801083
if !cursor.goto_next_sibling() {
10811084
return None;
10821085
}
@@ -1242,13 +1245,13 @@ impl<'a> TreeCursor<'a> {
12421245
///
12431246
/// See also [field_name](TreeCursor::field_name).
12441247
#[doc(alias = "ts_tree_cursor_current_field_id")]
1245-
pub fn field_id(&self) -> Option<u16> {
1248+
pub fn field_id(&self) -> Option<FieldId> {
12461249
unsafe {
12471250
let id = ffi::ts_tree_cursor_current_field_id(&self.0);
12481251
if id == 0 {
12491252
None
12501253
} else {
1251-
Some(id)
1254+
Some(FieldId::new(id).unwrap())
12521255
}
12531256
}
12541257
}

0 commit comments

Comments
 (0)