From e1c0d4fd5f5a41c698100c0da6e0b9d43bbed2f4 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 14 Aug 2023 22:45:41 -0700 Subject: [PATCH] cleanup: replace `x[n..n+l]` by `x[n..][..l]` This avoids repeating the `n` in these expressions. Thanks to @Dr-Emann for the suggestion. --- lib/src/default_index_store.rs | 17 +++++++---------- lib/src/stacked_table.rs | 2 +- lib/tests/test_merge_trees.rs | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 1bc70aab5..98b8289e4 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -305,14 +305,11 @@ impl CommitGraphEntry<'_> { // to better cache locality when walking it; ability to quickly find all // commits associated with a change id. fn change_id(&self) -> ChangeId { - ChangeId::new(self.data[20..20 + self.change_id_length].to_vec()) + ChangeId::new(self.data[20..][..self.change_id_length].to_vec()) } fn commit_id(&self) -> CommitId { - CommitId::from_bytes( - &self.data - [20 + self.change_id_length..20 + self.change_id_length + self.commit_id_length], - ) + CommitId::from_bytes(&self.data[20 + self.change_id_length..][..self.commit_id_length]) } } @@ -337,7 +334,7 @@ impl CommitLookupEntry<'_> { fn pos(&self) -> IndexPosition { IndexPosition( - (&self.data[self.commit_id_length..self.commit_id_length + 4]) + (&self.data[self.commit_id_length..][..4]) .read_u32::() .unwrap(), ) @@ -560,7 +557,7 @@ impl MutableIndexImpl { buf.write_u32::(pos.0).unwrap(); } - (&mut buf[parent_overflow_offset..parent_overflow_offset + 4]) + (&mut buf[parent_overflow_offset..][..4]) .write_u32::(parent_overflow.len() as u32) .unwrap(); for parent_pos in parent_overflow { @@ -1899,7 +1896,7 @@ impl ReadonlyIndexImpl { fn graph_entry(&self, local_pos: u32) -> CommitGraphEntry { let offset = (local_pos as usize) * self.commit_graph_entry_size; CommitGraphEntry { - data: &self.graph[offset..offset + self.commit_graph_entry_size], + data: &self.graph[offset..][..self.commit_graph_entry_size], commit_id_length: self.commit_id_length, change_id_length: self.change_id_length, } @@ -1908,7 +1905,7 @@ impl ReadonlyIndexImpl { fn lookup_entry(&self, lookup_pos: u32) -> CommitLookupEntry { let offset = (lookup_pos as usize) * self.commit_lookup_entry_size; CommitLookupEntry { - data: &self.lookup[offset..offset + self.commit_lookup_entry_size], + data: &self.lookup[offset..][..self.commit_lookup_entry_size], commit_id_length: self.commit_id_length, } } @@ -1916,7 +1913,7 @@ impl ReadonlyIndexImpl { fn overflow_parent(&self, overflow_pos: u32) -> IndexPosition { let offset = (overflow_pos as usize) * 4; IndexPosition( - (&self.overflow_parent[offset..offset + 4]) + (&self.overflow_parent[offset..][..4]) .read_u32::() .unwrap(), ) diff --git a/lib/src/stacked_table.rs b/lib/src/stacked_table.rs index c293421d0..698c4f5a0 100644 --- a/lib/src/stacked_table.rs +++ b/lib/src/stacked_table.rs @@ -175,7 +175,7 @@ impl<'table> ReadonlyTableIndexEntry<'table> { fn new(table: &'table ReadonlyTable, pos: usize) -> Self { let entry_size = ReadonlyTableIndexEntry::size(table.key_size); let offset = entry_size * pos; - let data = &table.index[offset..offset + entry_size]; + let data = &table.index[offset..][..entry_size]; ReadonlyTableIndexEntry { data } } diff --git a/lib/tests/test_merge_trees.rs b/lib/tests/test_merge_trees.rs index 75311a8c7..e1350ea1a 100644 --- a/lib/tests/test_merge_trees.rs +++ b/lib/tests/test_merge_trees.rs @@ -54,7 +54,7 @@ fn test_same_type(use_git: bool) { let write_tree = |index: usize| -> Tree { let mut tree_builder = store.tree_builder(store.empty_tree_id().clone()); for path in &files { - let contents = &path[index..index + 1]; + let contents = &path[index..][..1]; if contents != "_" { testutils::write_normal_file( &mut tree_builder, @@ -216,7 +216,7 @@ fn test_executable(use_git: bool) { fn contents_in_tree<'a>(files: &[&'a str], index: usize) -> Vec<(&'a str, bool)> { files .iter() - .map(|f| (*f, &f[index..index + 1] == "x")) + .map(|f| (*f, &f[index..][..1] == "x")) .collect() }