Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper functions for Array<MaybeUninit<T>, U> #8

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ where
}
}

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

/// Extract the values from an array of `MaybeUninit` containers.
///
/// # Safety
///
/// It is up to the caller to guarantee that all elements of the array are in an initialized
/// state.
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