-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use NonMaxU32
instead of u32
as a length container to reduce Option<ArrayVec>
size.
#219
Comments
Sounds useful. We'd like to store the length even smaller (i.e smaller type informed by the compile time size of capacity) - and that shrinkage has higher priority - but it might not yet be possible. |
In my opinion, it is impossible to introduce such flexibility without adding yet another type. |
I believe, it will be possible when generic constants will be allowed in constant expressions. |
Out of curiosity. How? |
Using |
ArrayVec<T, const N: usize, const LEN_BYTES: usize> {
data: [MaybeUninit<T>; N],
len: [u8; LEN_BYTES],
} Perhaps I am missing something but |
const fn log_2_ceiling(n_minus_one: usize) -> usize {
let mut r = 0;
while n_minus_one >> r != 0 {
r += 1
}
r
}
struct ArrayVec<T, const N: usize> {
data: [MaybeUninit<T>; N],
len: [u8; { (log_2_ceiling(N) + 7) / 8 }],
} |
That is a really nice approach! Thank you for the explanation :) |
This type looks bad for use with |
It is very cheap operation, doesn't it? |
It's also a useless one. It'd better if Rust could use 0xffff_ffff as a niche. Alternatively, adding 1 to the length might also be better because that works with most operations. |
It can, but using a very unstable part of the language. But I believe it will be stabilized one day, |
Storing the length as
NonMaxU32
would allow compiler to optimizeOption<ArrayVec>
size by storingNone
as impossible length value (u32::MAX
).The text was updated successfully, but these errors were encountered: