Skip to content

Commit 6738323

Browse files
committed
Add helper functions for Array<MaybeUninit<T>, U>
Adds the following functions: - `uninit`: construct an uninitialized `Array<MaybeUninit<T>, U>` - `assume_init`: converts to `Array<T, U>` assuming all elements are initialized
1 parent cf933f4 commit 6738323

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ where
217217
}
218218
}
219219

220+
impl<T, U> Array<MaybeUninit<T>, U>
221+
where
222+
U: ArraySize,
223+
{
224+
/// Create an uninitialized array of [`MaybeUninit`]s for the given type.
225+
pub unsafe fn uninit() -> Self {
226+
MaybeUninit::uninit().assume_init()
227+
}
228+
229+
/// Create an uninitialized array of [`MaybeUninit`]s for the given type.
230+
pub unsafe fn assume_init(self) -> Array<T, U> {
231+
// TODO(tarcieri): use `MaybeUninit::array_assume_init` when stable
232+
ptr::read(self.as_ptr().cast())
233+
}
234+
}
235+
220236
impl<T, U> AsRef<[T]> for Array<T, U>
221237
where
222238
U: ArraySize,

0 commit comments

Comments
 (0)