forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
744db01
commit 8820955
Showing
8 changed files
with
177 additions
and
134 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "solana-unchecked-div-by-const" | ||
description = "Compile-time-safe division." | ||
documentation = "https://docs.rs/solana-rent" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
//! Compile-time-safe division. | ||
/// Convenience macro for doing integer division where the operation's safety | ||
/// can be checked at compile-time. | ||
/// | ||
/// Since `unchecked_div_by_const!()` is supposed to fail at compile-time, abuse | ||
/// doctests to cover failure modes | ||
/// | ||
/// # Examples | ||
/// | ||
/// Literal denominator div-by-zero fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// let _ = unchecked_div_by_const!(10, 0); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Const denominator div-by-zero fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// const D: u64 = 0; | ||
/// let _ = unchecked_div_by_const!(10, D); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Non-const denominator fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// let d = 0; | ||
/// let _ = unchecked_div_by_const!(10, d); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Literal denominator div-by-zero fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// const N: u64 = 10; | ||
/// let _ = unchecked_div_by_const!(N, 0); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Const denominator div-by-zero fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// const N: u64 = 10; | ||
/// const D: u64 = 0; | ||
/// let _ = unchecked_div_by_const!(N, D); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Non-const denominator fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// # const N: u64 = 10; | ||
/// let d = 0; | ||
/// let _ = unchecked_div_by_const!(N, d); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Literal denominator div-by-zero fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// let n = 10; | ||
/// let _ = unchecked_div_by_const!(n, 0); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Const denominator div-by-zero fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// let n = 10; | ||
/// const D: u64 = 0; | ||
/// let _ = unchecked_div_by_const!(n, D); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// Non-const denominator fails: | ||
/// | ||
/// ```compile_fail | ||
/// # use solana_unchecked_div_by_const::unchecked_div_by_const; | ||
/// # fn main() { | ||
/// let n = 10; | ||
/// let d = 0; | ||
/// let _ = unchecked_div_by_const!(n, d); | ||
/// # } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! unchecked_div_by_const { | ||
($num:expr, $den:expr) => {{ | ||
// Ensure the denominator is compile-time constant | ||
let _ = [(); ($den - $den) as usize]; | ||
// Compile-time constant integer div-by-zero passes for some reason | ||
// when invoked from a compilation unit other than that where this | ||
// macro is defined. Do an explicit zero-check for now. Sorry about the | ||
// ugly error messages! | ||
// https://users.rust-lang.org/t/unexpected-behavior-of-compile-time-integer-div-by-zero-check-in-declarative-macro/56718 | ||
let _ = [(); ($den as usize) - 1]; | ||
#[allow(clippy::arithmetic_side_effects)] | ||
let quotient = $num / $den; | ||
quotient | ||
}}; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::unchecked_div_by_const; | ||
|
||
#[test] | ||
fn test_unchecked_div_by_const() { | ||
const D: u64 = 2; | ||
const N: u64 = 10; | ||
let n = 10; | ||
assert_eq!(unchecked_div_by_const!(10, 2), 5); | ||
assert_eq!(unchecked_div_by_const!(N, 2), 5); | ||
assert_eq!(unchecked_div_by_const!(n, 2), 5); | ||
assert_eq!(unchecked_div_by_const!(10, D), 5); | ||
assert_eq!(unchecked_div_by_const!(N, D), 5); | ||
assert_eq!(unchecked_div_by_const!(n, D), 5); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.