#const-generics #bounds #traits #bit-manipulation #helper #is-power-of2

nightly bit-bounds

Helper traits for const generic bitwise bounds

2 releases (1 stable)

1.0.0 Mar 19, 2024
0.1.0 Oct 15, 2022

#21 in #bit-manipulation

MIT license

4KB

License Cargo Documentation

Helper traits for const generic bitwise bounds

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use bit_bounds::{IsPowerOf2, usize::*};

struct Buffer<const N: usize> {
  inner: [usize; N],
}

impl<const N: usize> Buffer<N>
where
  Int<N>: IsPowerOf2
{
  pub const fn new() -> Self {
    Buffer { inner: [0; N] }
  }
}

fn extract_index<const N: usize>(counter: usize) -> usize
where
  Int<N>: IsPowerOf2,
  Int<N>: BitsAllClear<{ (u32::MAX as usize) << 32 }>,
{
  (counter >> 32) & (N - 1)
}

lib.rs:

Helper traits for const generic bounds

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use bit_bounds::{IsPowerOf2, usize::*};

struct Buffer<const N: usize> {
  inner: [usize; N],
}

impl<const N: usize> Buffer<N>
where
  Int<N>: IsPowerOf2
{
  pub const fn new() -> Self {
    Buffer { inner: [0; N] }
  }
}

fn extract_index<const N: usize>(counter: usize) -> usize
where
  Int<N>: IsPowerOf2,
  Int<N>: BitsAllClear<{ (u32::MAX as usize) << 32 }>,
{
  (counter >> 32) & (N - 1)
}

Dependencies

~3KB