kernel/utilities/slice_uninit.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2026.
4
5/// Convert a `&mut [T]` to a `&mut [MaybeUninit<T>]`.
6///
7/// This is a safe operation, but as of June 2026, there is no safe API in the
8/// standard library to do this. So, we provide our own.
9pub fn mut_slice_as_maybeuninit<T>(buffer: &mut [T]) -> &mut [core::mem::MaybeUninit<T>] {
10 // # Safety
11 //
12 // `MaybeUninit<T>` has the same size and alignment as `T`.
13 let maybeuninit_buf: &mut [core::mem::MaybeUninit<T>] =
14 unsafe { core::slice::from_raw_parts_mut(buffer.as_mut_ptr().cast(), buffer.len()) };
15 maybeuninit_buf
16}