Crates.io | const_power_of_two |
lib.rs | const_power_of_two |
version | 1.0.0 |
source | src |
created_at | 2024-08-10 08:15:36.333589 |
updated_at | 2024-08-10 18:18:43.93868 |
description | A crate for working with constant generics that are powers of two. |
homepage | |
repository | https://github.com/seancroach/const_power_of_two |
max_upload_size | |
id | 1332160 |
size | 28,318 |
const_power_of_two
A crate for working with constant generics that are powers of two.
Add the following to your Cargo.toml
:
[dependencies]
const_power_of_two = "1"
Then, import the corresponding trait for your argument type, and add it to your
trait or implementation's where
bounds:
use const_power_of_two::PowerOfTwoUsize;
trait MyTrait<const ALIGNMENT: usize>
where
usize: PowerOfTwoUsize<ALIGNMENT>,
{
// ...
}
struct Test;
// NOTE: This is valid, and no error is emitted.
impl MyTrait<4> for Test {}
// NOTE: This will emit an error at compile-time.
impl MyTrait<10> for Test {}
The integer type is what implements the trait, as you can see above. It's not
the most common Rust pattern, but it's easy to work with once you've seen it
in action. At compile-time, if ALIGNMENT
isn't a power of two, an error will
get emitted.
You can view the documentation on docs.rs here.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.