[Draft] Added const power of two check for capacity#62
[Draft] Added const power of two check for capacity#62zimward wants to merge 0 commit intoNULLx76:masterfrom zimward:master
Conversation
src/with_const_generics.rs
Outdated
| pub struct Assert<const COND: bool> {} | ||
| pub trait IsTrue {} | ||
| impl IsTrue for Assert<true> {} | ||
| pub const fn is_power_of_two(n: usize) -> bool { | ||
| n != 0 && n & (n - 1) == 0 | ||
| } |
There was a problem hiding this comment.
This is smart. Although it feels something forced to be used here.
Not sure if it can be done but imo, what would be better would be to make sure constructors are const so const expressions can be used. Use a const expr to assert the size in the constructors. That way you do not need to spread that where Assert<...>: IsTrue everywhere.
But again, just a curious traveling through. Since I felt the same when I read about this in the documentation and had the same idea.
There was a problem hiding this comment.
Actually, just saw it is almost implemented here
There was a problem hiding this comment.
To be fair i only saw a simmilar pattern used somewhere and copied it for my self. I didn't test the PR you've linked, but it seems to be what i intended.
Implements #61 causing a compile error when trying to construct a
ConstGenericRingBufferwith a capacity of zero or non power of two value.This is only a draft and requires generic_const_exprs and should not be merged.