From e23e4cea18753287ad4533b69ec9b2ffe4bd873a Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Mon, 17 Jul 2023 14:44:35 +0300 Subject: [PATCH] Make Rust code statements more compact and in functional style --- lib/binding_rust/lib.rs | 106 +++++++++++----------------------------- 1 file changed, 28 insertions(+), 78 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 98e247b2..69f595de 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -284,11 +284,7 @@ impl Language { #[doc(alias = "ts_language_symbol_name")] pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> { let ptr = unsafe { ffi::ts_language_symbol_name(self.0, id) }; - if ptr.is_null() { - None - } else { - Some(unsafe { CStr::from_ptr(ptr) }.to_str().unwrap()) - } + (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap()) } /// Get the numeric id for the given node kind. @@ -327,11 +323,7 @@ impl Language { #[doc(alias = "ts_language_field_name_for_id")] pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str> { let ptr = unsafe { ffi::ts_language_field_name_for_id(self.0, field_id) }; - if ptr.is_null() { - None - } else { - Some(unsafe { CStr::from_ptr(ptr) }.to_str().unwrap()) - } + (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap()) } /// Get the numerical id for the given field name. @@ -376,11 +368,7 @@ impl Language { #[doc(alias = "ts_lookahead_iterator_new")] pub fn lookahead_iterator(&self, state: u16) -> Option { let ptr = unsafe { ffi::ts_lookahead_iterator_new(self.0, state) }; - if ptr.is_null() { - None - } else { - Some(unsafe { LookaheadIterator::from_raw(ptr) }) - } + (!ptr.is_null()).then(|| unsafe { LookaheadIterator::from_raw(ptr) }) } } @@ -418,11 +406,7 @@ impl Parser { #[doc(alias = "ts_parser_language")] pub fn language(&self) -> Option { let ptr = unsafe { ffi::ts_parser_language(self.0.as_ptr()) }; - if ptr.is_null() { - None - } else { - Some(Language(ptr)) - } + (!ptr.is_null()).then(|| Language(ptr)) } /// Get the parser's current logger. @@ -511,7 +495,7 @@ impl Parser { let bytes = text.as_ref(); let len = bytes.len(); self.parse_with( - &mut |i, _| if i < len { &bytes[i..] } else { &[] }, + &mut |i, _| (i < len).then(|| &bytes[i..]).unwrap_or_default(), old_tree, ) } @@ -532,7 +516,7 @@ impl Parser { let code_points = input.as_ref(); let len = code_points.len(); self.parse_utf16_with( - &mut |i, _| if i < len { &code_points[i..] } else { &[] }, + &mut |i, _| (i < len).then(|| &code_points[i..]).unwrap_or_default(), old_tree, ) } @@ -852,11 +836,7 @@ impl Clone for Tree { impl<'tree> Node<'tree> { fn new(node: ffi::TSNode) -> Option { - if node.id.is_null() { - None - } else { - Some(Node(node, PhantomData)) - } + (!node.id.is_null()).then(|| Node(node, PhantomData)) } /// Get a numeric id for this node that is unique. @@ -1074,11 +1054,7 @@ impl<'tree> Node<'tree> { pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str> { unsafe { let ptr = ffi::ts_node_field_name_for_child(self.0, child_index); - if ptr.is_null() { - None - } else { - Some(CStr::from_ptr(ptr).to_str().unwrap()) - } + (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap()) } } @@ -1345,11 +1321,7 @@ impl<'cursor> TreeCursor<'cursor> { pub fn field_name(&self) -> Option<&'static str> { unsafe { let ptr = ffi::ts_tree_cursor_current_field_name(&self.0); - if ptr.is_null() { - None - } else { - Some(CStr::from_ptr(ptr).to_str().unwrap()) - } + (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap()) } } @@ -1442,11 +1414,7 @@ impl<'cursor> TreeCursor<'cursor> { pub fn goto_first_child_for_byte(&mut self, index: usize) -> Option { let result = unsafe { ffi::ts_tree_cursor_goto_first_child_for_byte(&mut self.0, index as u32) }; - if result < 0 { - None - } else { - Some(result as usize) - } + (result >= 0).then_some(result as usize) } /// Move this cursor to the first child of its current node that extends beyond @@ -1458,11 +1426,7 @@ impl<'cursor> TreeCursor<'cursor> { pub fn goto_first_child_for_point(&mut self, point: Point) -> Option { let result = unsafe { ffi::ts_tree_cursor_goto_first_child_for_point(&mut self.0, point.into()) }; - if result < 0 { - None - } else { - Some(result as usize) - } + (result >= 0).then_some(result as usize) } /// Re-initialize this tree cursor to start at a different node. @@ -1547,11 +1511,8 @@ impl Iterator for LookaheadNamesIterator<'_> { #[doc(alias = "ts_lookahead_iterator_advance")] fn next(&mut self) -> Option { - if !(unsafe { ffi::ts_lookahead_iterator_advance(self.0 .0.as_ptr()) }) { - None - } else { - Some(self.0.current_symbol_name()) - } + unsafe { ffi::ts_lookahead_iterator_advance(self.0 .0.as_ptr()) } + .then(|| self.0.current_symbol_name()) } } @@ -1561,11 +1522,8 @@ impl Iterator for LookaheadIterator { #[doc(alias = "ts_lookahead_iterator_advance")] fn next(&mut self) -> Option { // the first symbol is always `0` so we can safely skip it - if !(unsafe { ffi::ts_lookahead_iterator_advance(self.0.as_ptr()) }) { - None - } else { - Some(self.current_symbol()) - } + unsafe { ffi::ts_lookahead_iterator_advance(self.0.as_ptr()) } + .then(|| self.current_symbol()) } } @@ -1733,11 +1691,9 @@ 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 { - &[] - } + (length > 0) + .then(|| slice::from_raw_parts(raw_predicates, length as usize)) + .unwrap_or_default() }; let byte_offset = unsafe { ffi::ts_query_start_byte_for_pattern(ptr, i as u32) }; @@ -2134,7 +2090,7 @@ impl QueryCursor { text_provider: T, ) -> QueryCaptures<'query, 'tree, T, I> { let ptr = self.ptr.as_ptr(); - unsafe { ffi::ts_query_cursor_exec(self.ptr.as_ptr(), query.ptr.as_ptr(), node.0) }; + unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) }; QueryCaptures { ptr, query, @@ -2209,13 +2165,9 @@ impl<'tree> QueryMatch<'_, 'tree> { &self, capture_ix: u32, ) -> impl Iterator> + '_ { - self.captures.iter().filter_map(move |capture| { - if capture.index == capture_ix { - Some(capture.node) - } else { - None - } - }) + self.captures + .iter() + .filter_map(move |capture| (capture.index == capture_ix).then(|| capture.node)) } fn new(m: ffi::TSQueryMatch, cursor: *mut ffi::TSQueryCursor) -> Self { @@ -2223,16 +2175,14 @@ impl<'tree> QueryMatch<'_, 'tree> { cursor, id: m.id, pattern_index: m.pattern_index as usize, - captures: if m.capture_count > 0 { - unsafe { + captures: (m.capture_count > 0) + .then(|| unsafe { slice::from_raw_parts( m.captures as *const QueryCapture<'tree>, m.capture_count as usize, ) - } - } else { - &[] - }, + }) + .unwrap_or_default(), } } @@ -2269,7 +2219,7 @@ impl<'tree> QueryMatch<'_, 'tree> { } else if let Some(ref first_chunk) = self.first_chunk { first_chunk.as_ref() } else { - &[] + Default::default() } } } @@ -2559,7 +2509,7 @@ impl<'a> Iterator for LossyUtf8<'a> { } match std::str::from_utf8(self.bytes) { Ok(valid) => { - self.bytes = &[]; + self.bytes = Default::default(); Some(valid) } Err(error) => {