const_power_of_two

Crates.ioconst_power_of_two
lib.rsconst_power_of_two
version1.0.0
sourcesrc
created_at2024-08-10 08:15:36.333589
updated_at2024-08-10 18:18:43.93868
descriptionA crate for working with constant generics that are powers of two.
homepage
repositoryhttps://github.com/seancroach/const_power_of_two
max_upload_size
id1332160
size28,318
Sean C. Roach (seancroach)

documentation

README

const_power_of_two

github crates.io docs.rs build status

A crate for working with constant generics that are powers of two.

Usage

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.

Documentation

You can view the documentation on docs.rs here.

License

Licensed under either of

at your option.

Contribution

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.

Commit count: 0

cargo fmt