Open jaudiger opened 2 months ago
for
loops desugar to a call to into_iter
, which is not a const
function, and are therefore currently not allowed in const
context.
It's standard practice to write a while
loop over the indices instead, even if it is not very idiomatic:
#[must_use]
const fn from_slice_unchecked(from_slice: &[u8]) -> [MaybeUninit<u8>; 1]
{
let mut vec = [MaybeUninit::uninit(); 1];
let mut i = 0;
while i < from_slice.len() {
vec[i] = MaybeUninit::new(from_slice[i]);
i += 1;
}
vec
}
(this version actually only uses stable features)
Edit: here is a cool example of the crc
crate using this pattern to generate a lookup-table.
The error for this should explain that.
@joboet Works perfectly in stable indeed, thanks for pointing me out in this direction. I'm now able to remove in my code the following feature flag: #![feature(const_for)]
, but I then encounter new blockers during the road to const_mut_refs
. I'll focus on those new ones.
Anyway, thanks for your help. Since the trait IntoIterator is not available in const context, should I leave this issue open or close it if there is a duplicate already somewhere?
PS: thanks for the link, it was a good source of interesting line of code.
I didn't find a clear issue that was talking about that, and I'm clearly in the "nightly scape" of Rust here. I'm trying to build the following code (don't take attention to the possible unsafe part of this code, I omitted voluntary some part for better clarity):
Playground link:
It does build if I don't const inline the call to
from_slice_unchecked()
though. But here, I'm wanted to create this buffer at build time and I don't find a way to do it "easily".The closer issue I could find is https://github.com/rust-lang/rust/issues/103265 which contains this closed PR https://github.com/rust-lang/rust/pull/102225.