Skip to content

Commit

Permalink
Switch to u32
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Sep 29, 2023
1 parent db37eed commit d775d37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions boa_engine/src/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -729,14 +729,14 @@ impl From<PropertyKey> for JsValue {
impl From<u8> 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<u16> 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()))
}
}

Expand Down
14 changes: 5 additions & 9 deletions boa_engine/src/property/nonmaxu32.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 }
}
Expand All @@ -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
}
}

0 comments on commit d775d37

Please sign in to comment.