#const-generics #generics #const #traits #no-alloc #no-std

no-std const_power_of_two

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

3 releases (1 stable)

1.0.0 Aug 10, 2024
0.1.1 Aug 10, 2024
0.1.0 Aug 10, 2024

#1365 in Rust patterns

MIT/Apache

17KB
279 lines

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.

No runtime deps

~91KB