diff --git a/Cargo.toml b/Cargo.toml index e1ce01e..68d0183 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,10 @@ borsh = ["dep:borsh"] schemars = ["dep:schemars", "std"] +# Provide a soundness promixe to the compiler that the unerlying value is always within range +# This optimizes e.g. indexing range checks when passed in an API +hint = [] + [dependencies] num-traits = { version = "0.2.19", default-features = false, optional = true } defmt = { version = "0.3.8", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 8880f46..300b8ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -190,6 +190,11 @@ macro_rules! uint_impl_num { #[inline] fn value(self) -> $type { + #[cfg(feature = "hint")] + unsafe { + core::hint::assert_unchecked(self.value <= Self::MAX.value); + } + self.value } } @@ -230,6 +235,11 @@ macro_rules! uint_impl_num { #[inline] fn value(self) -> $type { + #[cfg(feature = "hint")] + unsafe { + core::hint::assert_unchecked(self.value <= Self::MAX.value); + } + self.value } }