Skip to content

Commit

Permalink
reimplement lowest_dword and highest_dword without unsafe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Feb 25, 2024
1 parent 54ff339 commit aba4e32
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions integer/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ pub const fn shrink_dword(dw: DoubleWord) -> Option<Word> {
/// Note that then length is only checked in the debug mode.
#[inline(always)]
pub fn lowest_dword(words: &[Word]) -> DoubleWord {
debug_assert!(words.len() >= 2);

// SAFETY: length checked by the assertion above
unsafe {
let lo = *words.get_unchecked(0);
let hi = *words.get_unchecked(1);
if let [lo, hi, ..] = *words {
double_word(lo, hi)
} else {
panic!("expected at least two words");
}
}

Expand All @@ -75,14 +72,10 @@ pub fn lowest_dword(words: &[Word]) -> DoubleWord {
/// Note that then length is only checked in the debug mode.
#[inline(always)]
pub fn highest_dword(words: &[Word]) -> DoubleWord {
let len = words.len();
debug_assert!(len >= 2);

// SAFETY: length checked by the assertion above
unsafe {
let lo = *words.get_unchecked(len - 2);
let hi = *words.get_unchecked(len - 1);
if let [.., lo, hi] = *words {
double_word(lo, hi)
} else {
panic!("expected at least two words");
}
}

Expand Down

0 comments on commit aba4e32

Please sign in to comment.