forked from availproject/avail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request availproject#96 from availproject/miguel/halborn_a…
…udit/hal_02 ChunkSize as Constant and some checks
- Loading branch information
Showing
19 changed files
with
315 additions
and
91 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
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,59 @@ | ||
use core::mem::size_of; | ||
|
||
pub struct UsizeNonZero<const N: usize>; | ||
impl<const N: usize> UsizeNonZero<N> { | ||
#[allow(dead_code)] | ||
pub const OK: () = assert!(N != 0, "must be non-zero"); | ||
} | ||
|
||
pub struct UsizeEven<const N: usize>; | ||
impl<const N: usize> UsizeEven<N> { | ||
#[allow(dead_code)] | ||
pub const OK: () = assert!(N % 2 == 0, "must be even"); | ||
} | ||
|
||
pub struct USizeSafeCastToU32<const N: usize>; | ||
impl<const N: usize> USizeSafeCastToU32<N> { | ||
#[allow(dead_code)] | ||
pub const OK: () = assert!( | ||
size_of::<usize>() <= size_of::<u32>() || N <= u32::MAX as usize, | ||
"must be safe to cast to u32" | ||
); | ||
} | ||
|
||
pub struct USizeGreaterOrEq<const N: usize, const M: usize>; | ||
impl<const N: usize, const M: usize> USizeGreaterOrEq<N, M> { | ||
#[allow(dead_code)] | ||
pub const OK: () = assert!(N >= M, "must be greater or equal"); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn non_zero() { | ||
let t = trybuild::TestCases::new(); | ||
t.pass("tests/const_generics/usize_non_zero.rs"); | ||
t.compile_fail("tests/const_generics/usize_non_zero_fail.rs"); | ||
} | ||
|
||
#[test] | ||
fn even() { | ||
let t = trybuild::TestCases::new(); | ||
t.pass("tests/const_generics/usize_even.rs"); | ||
t.compile_fail("tests/const_generics/usize_even_fail.rs"); | ||
} | ||
|
||
#[test] | ||
fn safe_cast_to_u32() { | ||
let t = trybuild::TestCases::new(); | ||
t.pass("tests/const_generics/usize_safe_cast_to_u32.rs"); | ||
t.compile_fail("tests/const_generics/usize_safe_cast_to_u32_fail.rs"); | ||
} | ||
|
||
#[test] | ||
fn greater_or_eq() { | ||
let t = trybuild::TestCases::new(); | ||
t.pass("tests/const_generics/usize_greater_or_eq.rs"); | ||
t.compile_fail("tests/const_generics/usize_greater_or_eq_fail.rs"); | ||
} | ||
} |
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,10 @@ | ||
use avail_core::const_generic_asserts::UsizeEven; | ||
|
||
fn const_generic_even<const N: usize>() { | ||
let () = UsizeEven::<N>::OK; | ||
} | ||
|
||
fn main() { | ||
const_generic_even::<2>(); | ||
const_generic_even::<4>(); | ||
} |
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,9 @@ | ||
use avail_core::const_generic_asserts::UsizeEven; | ||
|
||
fn const_generic_even<const N: usize> () { | ||
let () = UsizeEven::<N>::OK; | ||
} | ||
|
||
fn main () { | ||
const_generic_even::<1>(); | ||
} |
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,13 @@ | ||
error[E0080]: evaluation of `avail_core::const_generic_asserts::UsizeEven::<1>::OK` failed | ||
--> src/const_generic_asserts.rs | ||
| | ||
| pub const OK: () = assert!(N % 2 == 0, "must be even"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'must be even', $DIR/src/const_generic_asserts.rs:12:24 | ||
| | ||
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
note: the above error was encountered while instantiating `fn const_generic_even::<1>` | ||
--> tests/const_generics/usize_even_fail.rs:8:2 | ||
| | ||
8 | const_generic_even::<1>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ |
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,15 @@ | ||
use avail_core::const_generic_asserts::USizeGreaterOrEq; | ||
|
||
fn const_generic_ge<const N: usize, const M: usize>() { | ||
let () = USizeGreaterOrEq::<N, M>::OK; | ||
} | ||
|
||
fn main() { | ||
const_generic_ge::<0, 0>(); | ||
const_generic_ge::<1, 0>(); | ||
const_generic_ge::<1, 1>(); | ||
const_generic_ge::<100, 99>(); | ||
const_generic_ge::<100, 100>(); | ||
const_generic_ge::<{ usize::MAX }, { usize::MAX - 1 }>(); | ||
const_generic_ge::<{ usize::MAX }, { usize::MAX }>(); | ||
} |
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,9 @@ | ||
use avail_core::const_generic_asserts::USizeGreaterOrEq; | ||
|
||
fn const_generic_ge<const N: usize, const M: usize> () { | ||
let () = USizeGreaterOrEq::<N,M>::OK; | ||
} | ||
|
||
fn main () { | ||
const_generic_ge::<1, 2>(); | ||
} |
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,13 @@ | ||
error[E0080]: evaluation of `avail_core::const_generic_asserts::USizeGreaterOrEq::<1, 2>::OK` failed | ||
--> src/const_generic_asserts.rs | ||
| | ||
| pub const OK: () = assert!(N >= M, "must be greater or equal"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'must be greater or equal', $DIR/src/const_generic_asserts.rs:27:24 | ||
| | ||
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
note: the above error was encountered while instantiating `fn const_generic_ge::<1, 2>` | ||
--> tests/const_generics/usize_greater_or_eq_fail.rs:8:2 | ||
| | ||
8 | const_generic_ge::<1, 2>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
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,10 @@ | ||
use avail_core::const_generic_asserts::UsizeNonZero; | ||
|
||
fn const_generic_non_zero<const N: usize>() { | ||
let () = UsizeNonZero::<N>::OK; | ||
} | ||
|
||
fn main() { | ||
const_generic_non_zero::<1>(); | ||
const_generic_non_zero::<2>(); | ||
} |
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,9 @@ | ||
use avail_core::const_generic_asserts::UsizeNonZero; | ||
|
||
fn const_generic_non_zero<const N: usize> () { | ||
let () = UsizeNonZero::<N>::OK; | ||
} | ||
|
||
fn main () { | ||
const_generic_non_zero::<0>(); | ||
} |
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,13 @@ | ||
error[E0080]: evaluation of `avail_core::const_generic_asserts::UsizeNonZero::<0>::OK` failed | ||
--> src/const_generic_asserts.rs | ||
| | ||
| pub const OK: () = assert!(N != 0, "must be non-zero"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'must be non-zero', $DIR/src/const_generic_asserts.rs:6:24 | ||
| | ||
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
note: the above error was encountered while instantiating `fn const_generic_non_zero::<0>` | ||
--> tests/const_generics/usize_non_zero_fail.rs:8:2 | ||
| | ||
8 | const_generic_non_zero::<0>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
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,11 @@ | ||
use avail_core::const_generic_asserts::USizeSafeCastToU32; | ||
|
||
fn const_generic_safe_cast_to_u32<const N: usize>() { | ||
let () = USizeSafeCastToU32::<N>::OK; | ||
} | ||
|
||
fn main() { | ||
const_generic_safe_cast_to_u32::<0>(); | ||
const_generic_safe_cast_to_u32::<{ (u32::MAX - 1) as usize }>(); | ||
const_generic_safe_cast_to_u32::<{ u32::MAX as usize }>(); | ||
} |
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,9 @@ | ||
use avail_core::const_generic_asserts::USizeSafeCastToU32; | ||
|
||
fn const_generic_safe_cast_to_u32<const N: usize>() { | ||
let () = USizeSafeCastToU32::<N>::OK; | ||
} | ||
|
||
fn main() { | ||
const_generic_safe_cast_to_u32::<{ u32::MAX as usize + 1 }>(); | ||
} |
17 changes: 17 additions & 0 deletions
17
core/tests/const_generics/usize_safe_cast_to_u32_fail.stderr
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,17 @@ | ||
error[E0080]: evaluation of `avail_core::const_generic_asserts::USizeSafeCastToU32::<4294967296>::OK` failed | ||
--> src/const_generic_asserts.rs | ||
| | ||
| pub const OK: () = assert!( | ||
| ________________________^ | ||
| | size_of::<usize>() <= size_of::<u32>() || N <= u32::MAX as usize, | ||
| | "must be safe to cast to u32" | ||
| | ); | ||
| |_____^ the evaluated program panicked at 'must be safe to cast to u32', $DIR/src/const_generic_asserts.rs:18:24 | ||
| | ||
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
note: the above error was encountered while instantiating `fn const_generic_safe_cast_to_u32::<4294967296>` | ||
--> tests/const_generics/usize_safe_cast_to_u32_fail.rs:8:2 | ||
| | ||
8 | const_generic_safe_cast_to_u32::<{ u32::MAX as usize + 1 }>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "kate" | ||
version = "0.8.2" | ||
version = "0.9.0" | ||
authors = ["Denis Ermolin <[email protected]>"] | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
|
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
Oops, something went wrong.