Skip to content

Commit 36517bf

Browse files
Fix new Miri errors for bytes_to_data while also simplifying it
1 parent ff1877a commit 36517bf

File tree

1 file changed

+4
-23
lines changed

1 file changed

+4
-23
lines changed

src/lib.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
exact_size_is_empty,
5454
generic_const_exprs,
5555
inline_const,
56+
maybe_uninit_array_assume_init,
5657
maybe_uninit_uninit_array,
5758
pattern,
5859
slice_partition_dedup,
@@ -2672,28 +2673,8 @@ impl<const N: usize> StaticVec<u8, N> {
26722673
#[doc(hidden)]
26732674
#[inline]
26742675
pub(crate) const fn bytes_to_data(values: &[u8]) -> MaybeUninit<[u8; N]> {
2675-
// What follows is an idea partially arrived at from reading the source of the `const-concat`
2676-
// crate. Note that it amounts to effectively a `const fn` compatible implementation of what
2677-
// `MaybeUninit::assume_uninit()` does, and is *only* used here due to there being no other way
2678-
// to get an instance of `[MaybeUninit<u8>; N]` that we can actually write to (and to be clear,
2679-
// *not* read from) using regular indexing in conjunction with the `const_loop` feature (which
2680-
// is itself the only way at this time to write an arbitrary number of bytes from `values` to
2681-
// the result array at compile time).
2682-
#[repr(C)]
2683-
union Convert<From: Copy, To: Copy> {
2684-
from: From,
2685-
to: To,
2686-
}
2687-
// As stated above, this is effectively doing what `MaybeUninit::assume_init()` does.
2688-
// Note that while it might "look scary", what this function actually does would be incredibly
2689-
// mundane in basically any other language: you would just declare a very normal static array,
2690-
// and use it, very normally. That's literally *all* this is.
2691-
let mut res = unsafe {
2692-
Convert::<MaybeUninit<[MaybeUninit<u8>; N]>, [MaybeUninit<u8>; N]> {
2693-
from: MaybeUninit::uninit(),
2694-
}
2695-
.to
2696-
};
2676+
// Get an uninitialized array of bytes, with `N` capacity.
2677+
let mut res = MaybeUninit::<u8>::uninit_array::<N>();
26972678
// Move `values.len()` worth of bytes from `values` to `res`. I'm unaware of any other way that
26982679
// this could be done currently that would leave us with something usable to create a StaticVec
26992680
// for which the generic `N` could be *different* from `values.len()`, so thank
@@ -2708,7 +2689,7 @@ impl<const N: usize> StaticVec<u8, N> {
27082689
}
27092690
// Convert `res` from an instance of `[MaybeUninit<u8>; N]` to one of `[u8; N]`, and then return
27102691
// it as an instance of `MaybeUninit<[u8; N]>` that can be used to construct a `StaticVec`.
2711-
MaybeUninit::new(unsafe { Convert::<[MaybeUninit<u8>; N], [u8; N]> { from: res }.to })
2692+
MaybeUninit::new(unsafe { MaybeUninit::array_assume_init(res) })
27122693
}
27132694

27142695
/// Called solely from inside the `staticstring!` macro, and so must be public. This is guaranteed

0 commit comments

Comments
 (0)