#const-generics #bounds #bit-manipulation #helper #traits

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

#12 in #bit-manipulation

Download history 3/week @ 2024-02-19 4/week @ 2024-02-26 177/week @ 2024-03-18 38/week @ 2024-04-01

215 downloads per month

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