Skip to content

Commit 7318cd5

Browse files
authored
Merge pull request tree-sitter#2465 from tree-sitter/compact-func-style-calls
Make Rust code statements more compact and in functional style
2 parents cb8c89f + e23e4ce commit 7318cd5

File tree

1 file changed

+28
-78
lines changed

1 file changed

+28
-78
lines changed

lib/binding_rust/lib.rs

Lines changed: 28 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ impl Language {
284284
#[doc(alias = "ts_language_symbol_name")]
285285
pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> {
286286
let ptr = unsafe { ffi::ts_language_symbol_name(self.0, id) };
287-
if ptr.is_null() {
288-
None
289-
} else {
290-
Some(unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
291-
}
287+
(!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
292288
}
293289

294290
/// Get the numeric id for the given node kind.
@@ -327,11 +323,7 @@ impl Language {
327323
#[doc(alias = "ts_language_field_name_for_id")]
328324
pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str> {
329325
let ptr = unsafe { ffi::ts_language_field_name_for_id(self.0, field_id) };
330-
if ptr.is_null() {
331-
None
332-
} else {
333-
Some(unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
334-
}
326+
(!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
335327
}
336328

337329
/// Get the numerical id for the given field name.
@@ -376,11 +368,7 @@ impl Language {
376368
#[doc(alias = "ts_lookahead_iterator_new")]
377369
pub fn lookahead_iterator(&self, state: u16) -> Option<LookaheadIterator> {
378370
let ptr = unsafe { ffi::ts_lookahead_iterator_new(self.0, state) };
379-
if ptr.is_null() {
380-
None
381-
} else {
382-
Some(unsafe { LookaheadIterator::from_raw(ptr) })
383-
}
371+
(!ptr.is_null()).then(|| unsafe { LookaheadIterator::from_raw(ptr) })
384372
}
385373
}
386374

@@ -418,11 +406,7 @@ impl Parser {
418406
#[doc(alias = "ts_parser_language")]
419407
pub fn language(&self) -> Option<Language> {
420408
let ptr = unsafe { ffi::ts_parser_language(self.0.as_ptr()) };
421-
if ptr.is_null() {
422-
None
423-
} else {
424-
Some(Language(ptr))
425-
}
409+
(!ptr.is_null()).then(|| Language(ptr))
426410
}
427411

428412
/// Get the parser's current logger.
@@ -511,7 +495,7 @@ impl Parser {
511495
let bytes = text.as_ref();
512496
let len = bytes.len();
513497
self.parse_with(
514-
&mut |i, _| if i < len { &bytes[i..] } else { &[] },
498+
&mut |i, _| (i < len).then(|| &bytes[i..]).unwrap_or_default(),
515499
old_tree,
516500
)
517501
}
@@ -532,7 +516,7 @@ impl Parser {
532516
let code_points = input.as_ref();
533517
let len = code_points.len();
534518
self.parse_utf16_with(
535-
&mut |i, _| if i < len { &code_points[i..] } else { &[] },
519+
&mut |i, _| (i < len).then(|| &code_points[i..]).unwrap_or_default(),
536520
old_tree,
537521
)
538522
}
@@ -852,11 +836,7 @@ impl Clone for Tree {
852836

853837
impl<'tree> Node<'tree> {
854838
fn new(node: ffi::TSNode) -> Option<Self> {
855-
if node.id.is_null() {
856-
None
857-
} else {
858-
Some(Node(node, PhantomData))
859-
}
839+
(!node.id.is_null()).then(|| Node(node, PhantomData))
860840
}
861841

862842
/// Get a numeric id for this node that is unique.
@@ -1074,11 +1054,7 @@ impl<'tree> Node<'tree> {
10741054
pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str> {
10751055
unsafe {
10761056
let ptr = ffi::ts_node_field_name_for_child(self.0, child_index);
1077-
if ptr.is_null() {
1078-
None
1079-
} else {
1080-
Some(CStr::from_ptr(ptr).to_str().unwrap())
1081-
}
1057+
(!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
10821058
}
10831059
}
10841060

@@ -1345,11 +1321,7 @@ impl<'cursor> TreeCursor<'cursor> {
13451321
pub fn field_name(&self) -> Option<&'static str> {
13461322
unsafe {
13471323
let ptr = ffi::ts_tree_cursor_current_field_name(&self.0);
1348-
if ptr.is_null() {
1349-
None
1350-
} else {
1351-
Some(CStr::from_ptr(ptr).to_str().unwrap())
1352-
}
1324+
(!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
13531325
}
13541326
}
13551327

@@ -1442,11 +1414,7 @@ impl<'cursor> TreeCursor<'cursor> {
14421414
pub fn goto_first_child_for_byte(&mut self, index: usize) -> Option<usize> {
14431415
let result =
14441416
unsafe { ffi::ts_tree_cursor_goto_first_child_for_byte(&mut self.0, index as u32) };
1445-
if result < 0 {
1446-
None
1447-
} else {
1448-
Some(result as usize)
1449-
}
1417+
(result >= 0).then_some(result as usize)
14501418
}
14511419

14521420
/// Move this cursor to the first child of its current node that extends beyond
@@ -1458,11 +1426,7 @@ impl<'cursor> TreeCursor<'cursor> {
14581426
pub fn goto_first_child_for_point(&mut self, point: Point) -> Option<usize> {
14591427
let result =
14601428
unsafe { ffi::ts_tree_cursor_goto_first_child_for_point(&mut self.0, point.into()) };
1461-
if result < 0 {
1462-
None
1463-
} else {
1464-
Some(result as usize)
1465-
}
1429+
(result >= 0).then_some(result as usize)
14661430
}
14671431

14681432
/// Re-initialize this tree cursor to start at a different node.
@@ -1547,11 +1511,8 @@ impl Iterator for LookaheadNamesIterator<'_> {
15471511

15481512
#[doc(alias = "ts_lookahead_iterator_advance")]
15491513
fn next(&mut self) -> Option<Self::Item> {
1550-
if !(unsafe { ffi::ts_lookahead_iterator_advance(self.0 .0.as_ptr()) }) {
1551-
None
1552-
} else {
1553-
Some(self.0.current_symbol_name())
1554-
}
1514+
unsafe { ffi::ts_lookahead_iterator_advance(self.0 .0.as_ptr()) }
1515+
.then(|| self.0.current_symbol_name())
15551516
}
15561517
}
15571518

@@ -1561,11 +1522,8 @@ impl Iterator for LookaheadIterator {
15611522
#[doc(alias = "ts_lookahead_iterator_advance")]
15621523
fn next(&mut self) -> Option<Self::Item> {
15631524
// the first symbol is always `0` so we can safely skip it
1564-
if !(unsafe { ffi::ts_lookahead_iterator_advance(self.0.as_ptr()) }) {
1565-
None
1566-
} else {
1567-
Some(self.current_symbol())
1568-
}
1525+
unsafe { ffi::ts_lookahead_iterator_advance(self.0.as_ptr()) }
1526+
.then(|| self.current_symbol())
15691527
}
15701528
}
15711529

@@ -1733,11 +1691,9 @@ impl Query {
17331691
let mut length = 0u32;
17341692
let raw_predicates =
17351693
ffi::ts_query_predicates_for_pattern(ptr, i as u32, &mut length as *mut u32);
1736-
if length > 0 {
1737-
slice::from_raw_parts(raw_predicates, length as usize)
1738-
} else {
1739-
&[]
1740-
}
1694+
(length > 0)
1695+
.then(|| slice::from_raw_parts(raw_predicates, length as usize))
1696+
.unwrap_or_default()
17411697
};
17421698

17431699
let byte_offset = unsafe { ffi::ts_query_start_byte_for_pattern(ptr, i as u32) };
@@ -2134,7 +2090,7 @@ impl QueryCursor {
21342090
text_provider: T,
21352091
) -> QueryCaptures<'query, 'tree, T, I> {
21362092
let ptr = self.ptr.as_ptr();
2137-
unsafe { ffi::ts_query_cursor_exec(self.ptr.as_ptr(), query.ptr.as_ptr(), node.0) };
2093+
unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) };
21382094
QueryCaptures {
21392095
ptr,
21402096
query,
@@ -2209,30 +2165,24 @@ impl<'tree> QueryMatch<'_, 'tree> {
22092165
&self,
22102166
capture_ix: u32,
22112167
) -> impl Iterator<Item = Node<'tree>> + '_ {
2212-
self.captures.iter().filter_map(move |capture| {
2213-
if capture.index == capture_ix {
2214-
Some(capture.node)
2215-
} else {
2216-
None
2217-
}
2218-
})
2168+
self.captures
2169+
.iter()
2170+
.filter_map(move |capture| (capture.index == capture_ix).then(|| capture.node))
22192171
}
22202172

22212173
fn new(m: ffi::TSQueryMatch, cursor: *mut ffi::TSQueryCursor) -> Self {
22222174
QueryMatch {
22232175
cursor,
22242176
id: m.id,
22252177
pattern_index: m.pattern_index as usize,
2226-
captures: if m.capture_count > 0 {
2227-
unsafe {
2178+
captures: (m.capture_count > 0)
2179+
.then(|| unsafe {
22282180
slice::from_raw_parts(
22292181
m.captures as *const QueryCapture<'tree>,
22302182
m.capture_count as usize,
22312183
)
2232-
}
2233-
} else {
2234-
&[]
2235-
},
2184+
})
2185+
.unwrap_or_default(),
22362186
}
22372187
}
22382188

@@ -2269,7 +2219,7 @@ impl<'tree> QueryMatch<'_, 'tree> {
22692219
} else if let Some(ref first_chunk) = self.first_chunk {
22702220
first_chunk.as_ref()
22712221
} else {
2272-
&[]
2222+
Default::default()
22732223
}
22742224
}
22752225
}
@@ -2559,7 +2509,7 @@ impl<'a> Iterator for LossyUtf8<'a> {
25592509
}
25602510
match std::str::from_utf8(self.bytes) {
25612511
Ok(valid) => {
2562-
self.bytes = &[];
2512+
self.bytes = Default::default();
25632513
Some(valid)
25642514
}
25652515
Err(error) => {

0 commit comments

Comments
 (0)