Skip to content

Commit

Permalink
Add PartiallyInitialized::new function
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Mar 4, 2023
1 parent dacc6a7 commit 3bb2d36
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl<T: Default, const N: usize> Default for Array<T, N> {
fn default() -> Self {
// TODO use array::from_fn once the MSRV allows stuff from 1.63.0
let arr = {
let mut arr: PartiallyInitialized<T, N> =
PartiallyInitialized(Some(MaybeUninit::uninit()), 0);
let mut arr = PartiallyInitialized::<T, N>::new();
unsafe {
{
let p = arr.0.as_mut().unwrap();
Expand Down
10 changes: 8 additions & 2 deletions src/const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ pub(crate) struct PartiallyInitialized<T, const N: usize>(
pub(crate) usize,
);

impl<T, const N: usize> PartiallyInitialized<T, N> {
#[inline]
pub(crate) fn new() -> Self {
PartiallyInitialized(Some(MaybeUninit::uninit()), 0)
}
}

impl<T, const N: usize> Drop for PartiallyInitialized<T, N> {
fn drop(&mut self) {
if !core::mem::needs_drop::<T>() {
Expand Down Expand Up @@ -87,8 +94,7 @@ impl<'de, T, const N: usize> BigArray<'de, T> for [T; N] {
A: SeqAccess<'de>,
{
unsafe {
let mut arr: PartiallyInitialized<T, N> =
PartiallyInitialized(Some(MaybeUninit::uninit()), 0);
let mut arr = PartiallyInitialized::<T, N>::new();
{
let p = arr.0.as_mut().unwrap();
for i in 0..N {
Expand Down

0 comments on commit 3bb2d36

Please sign in to comment.