From 19c401a47407abbb153d238b0f44af833338b454 Mon Sep 17 00:00:00 2001 From: Anton Bulakh Date: Wed, 8 Nov 2023 01:36:26 +0200 Subject: [PATCH] misc: drop a few low-hanging unsafes Remove a couple of unnecessary unsafes: - The NonZeroUsize is a constant where the unwrap will optimize away anyway and we don't have an unsafe without any good reason there :) - The other two were simply not needed, lifetimes worked fine, maybe Rust became better since that code was written? NLL? Anyway, they're gone now --- cli/src/commands/run.rs | 2 +- lib/src/default_index_store.rs | 7 ++----- lib/src/stacked_table.rs | 10 ++-------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/cli/src/commands/run.rs b/cli/src/commands/run.rs index 8c205cb008..46ab926730 100644 --- a/cli/src/commands/run.rs +++ b/cli/src/commands/run.rs @@ -63,7 +63,7 @@ pub fn cmd_run(ui: &mut Ui, command: &CommandHelper, args: &RunArgs) -> Result<( // SAFETY: // We use a internal constant of 4 threads, if it fails let available = std::thread::available_parallelism() - .unwrap_or(unsafe { NonZeroUsize::new_unchecked(4) }); + .unwrap_or(NonZeroUsize::new(4).unwrap()); available.into() }; Err(user_error("This is a stub, do not use")) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index f66ce864ab..4fcbccd071 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -839,11 +839,8 @@ impl<'a> CompositeIndex<'a> { if pos.0 >= num_parent_commits { self.0.segment_entry_by_pos(pos, pos.0 - num_parent_commits) } else { - let parent_file: &ReadonlyIndexImpl = self.0.segment_parent_file().unwrap().as_ref(); - // The parent ReadonlyIndex outlives the child - let parent_file: &'a ReadonlyIndexImpl = unsafe { std::mem::transmute(parent_file) }; - - CompositeIndex(parent_file).entry_by_pos(pos) + let parent_file = self.0.segment_parent_file().unwrap(); + CompositeIndex(&**parent_file).entry_by_pos(pos) } } diff --git a/lib/src/stacked_table.rs b/lib/src/stacked_table.rs index 698c4f5a02..75c9319f09 100644 --- a/lib/src/stacked_table.rs +++ b/lib/src/stacked_table.rs @@ -51,14 +51,8 @@ pub trait TableSegment { } fn get_value<'a>(&'a self, key: &[u8]) -> Option<&'a [u8]> { - if let Some(value) = self.segment_get_value(key) { - return Some(value); - } - let parent_file = self.segment_parent_file()?; - let parent_file: &ReadonlyTable = parent_file.as_ref(); - // The parent ReadonlyIndex outlives the child - let parent_file: &'a ReadonlyTable = unsafe { std::mem::transmute(parent_file) }; - parent_file.get_value(key) + self.segment_get_value(key) + .or_else(|| self.segment_parent_file()?.get_value(key)) } }