Skip to content

Commit

Permalink
misc: drop a few low-hanging unsafes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
necauqua committed Nov 7, 2023
1 parent 2ac9865 commit 19c401a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
7 changes: 2 additions & 5 deletions lib/src/default_index_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
10 changes: 2 additions & 8 deletions lib/src/stacked_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 19c401a

Please sign in to comment.