From d775d379e80189acf6b76b3ea58f4a4844430fea Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Fri, 29 Sep 2023 21:39:23 +0200 Subject: [PATCH] Switch to u32 --- boa_engine/src/property/mod.rs | 8 ++++---- boa_engine/src/property/nonmaxu32.rs | 14 +++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/boa_engine/src/property/mod.rs b/boa_engine/src/property/mod.rs index 23ab3834730..39fe0158808 100644 --- a/boa_engine/src/property/mod.rs +++ b/boa_engine/src/property/mod.rs @@ -636,7 +636,7 @@ where if byte == CHAR_ZERO { if len == 1 { // SAFETY: `0` is not `u32::MAX`. - return unsafe { Some(NonMaxU32::new_unchecked(0)) }; + return Some(NonMaxU32::new_unchecked(0)); } // String "012345" is not a valid index. @@ -660,7 +660,7 @@ where // SAFETY: `result` cannot be `u32::MAX`, // because the length of the input is smaller than `MAX_CHAR_COUNT`. - unsafe { Some(NonMaxU32::new_unchecked(result)) } + Some(NonMaxU32::new_unchecked(result)) } } @@ -729,14 +729,14 @@ impl From for JsValue { impl From for PropertyKey { fn from(value: u8) -> Self { // SAFETY: `u8` can never be `u32::MAX`. - unsafe { Self::Index(NonMaxU32::new_unchecked(value.into())) } + Self::Index(NonMaxU32::new_unchecked(value.into())) } } impl From for PropertyKey { fn from(value: u16) -> Self { // SAFETY: `u16` can never be `u32::MAX`. - unsafe { Self::Index(NonMaxU32::new_unchecked(value.into())) } + Self::Index(NonMaxU32::new_unchecked(value.into())) } } diff --git a/boa_engine/src/property/nonmaxu32.rs b/boa_engine/src/property/nonmaxu32.rs index b9ebe3d8665..9fd06c412b0 100644 --- a/boa_engine/src/property/nonmaxu32.rs +++ b/boa_engine/src/property/nonmaxu32.rs @@ -1,9 +1,7 @@ -use std::num::NonZeroU32; - /// An integer that is not `u32::MAX`. #[derive(PartialEq, Debug, Clone, Copy, Eq, Hash)] pub struct NonMaxU32 { - inner: NonZeroU32, + inner: u32, } impl NonMaxU32 { @@ -13,9 +11,8 @@ impl NonMaxU32 { /// /// The caller must ensure that the given value is not `u32::MAX`. #[must_use] - pub const unsafe fn new_unchecked(inner: u32) -> Self { - // SAFETY: The caller must ensure that `inner` is not `u32::MAX`. - let inner = unsafe { NonZeroU32::new_unchecked(inner.wrapping_add(1)) }; + pub const fn new_unchecked(inner: u32) -> Self { + debug_assert!(inner != u32::MAX); Self { inner } } @@ -27,13 +24,12 @@ impl NonMaxU32 { return None; } - // SAFETY: We checked that `inner` is not `u32::MAX`. - unsafe { Some(Self::new_unchecked(inner)) } + Some(Self::new_unchecked(inner)) } /// Returns the value as a primitive type. #[must_use] pub const fn get(&self) -> u32 { - self.inner.get().wrapping_sub(1) + self.inner } }