Skip to content

Commit

Permalink
Add hint to value() (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens authored Dec 2, 2024
1 parent 0a54994 commit 680553d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 680553d

Please sign in to comment.