Skip to content

Commit

Permalink
Add helper functions for Array<MaybeUninit<T>, U>
Browse files Browse the repository at this point in the history
Adds the following functions:

- `uninit`: construct an uninitialized `Array<MaybeUninit<T>, U>`
- `assume_init`: converts to `Array<T, U>` assuming all elements are
  initialized
  • Loading branch information
tarcieri committed Jan 11, 2024
1 parent cf933f4 commit 6738323
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ where
}
}

impl<T, U> Array<MaybeUninit<T>, U>
where
U: ArraySize,
{
/// Create an uninitialized array of [`MaybeUninit`]s for the given type.
pub unsafe fn uninit() -> Self {
MaybeUninit::uninit().assume_init()
}

/// Create an uninitialized array of [`MaybeUninit`]s for the given type.
pub unsafe fn assume_init(self) -> Array<T, U> {
// TODO(tarcieri): use `MaybeUninit::array_assume_init` when stable
ptr::read(self.as_ptr().cast())
}
}

impl<T, U> AsRef<[T]> for Array<T, U>
where
U: ArraySize,
Expand Down

0 comments on commit 6738323

Please sign in to comment.