From aba4e32328077cdf09588a52fd08235f42b5d78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Sun, 22 Oct 2023 13:13:24 +0200 Subject: [PATCH] reimplement `lowest_dword` and `highest_dword` without `unsafe` blocks --- integer/src/primitive.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/integer/src/primitive.rs b/integer/src/primitive.rs index e2ce3d5..6950330 100644 --- a/integer/src/primitive.rs +++ b/integer/src/primitive.rs @@ -60,13 +60,10 @@ pub const fn shrink_dword(dw: DoubleWord) -> Option { /// 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"); } } @@ -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"); } }