Skip to content

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

Merged
tarcieri merged 4 commits intomasterfrom
maybe-uninit-helpers
Jan 11, 2024
Merged

Add helper functions for Array<MaybeUninit<T>, U>#8
tarcieri merged 4 commits intomasterfrom
maybe-uninit-helpers

Conversation

@tarcieri
Copy link
Member

Adds the following functions:

  • uninit: construct an uninitialized Array<MaybeUninit<T>, U>
  • assume_init: converts to Array<T, U> assuming all elements are initialized

Adds the following functions:

- `uninit`: construct an uninitialized `Array<MaybeUninit<T>, U>`
- `assume_init`: converts to `Array<T, U>` assuming all elements are
  initialized
@tarcieri tarcieri force-pushed the maybe-uninit-helpers branch from 6738323 to 1582c18 Compare January 11, 2024 19:42
@tarcieri tarcieri requested a review from newpavlov January 11, 2024 20:01
Copy link
Member

@newpavlov newpavlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be safe I think we should ask on IRLO about the uninit method, but AFAIK the code should be fine. It's worth to expand the safety comment and explicitly note that we "assume init" an array of MaybeUninits.

@tarcieri
Copy link
Member Author

Expanding the safety comment sounds good. I can also add a link to the similarly-shaped uninit_array method.

This makes the bounds more complicated but makes it much clearer what's
happening, as before it was relying on an implicit definition of
`ArraySize::ArrayType`.
@tarcieri
Copy link
Member Author

@newpavlov d7f32a4 should make it much more explicit what's happening (at the cost of slightly more complicated bounds), and also clears the clippy warning

@tarcieri
Copy link
Member Author

Seems at little closer to pub const fn assume_init too:

error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
   --> src/lib.rs:245:25
    |
245 |         Array(ptr::read(self.0.as_ptr().cast()))
    |                         ^^^^^^
    |
    = note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more information

@tarcieri tarcieri merged commit 1905e76 into master Jan 11, 2024
@tarcieri tarcieri deleted the maybe-uninit-helpers branch January 11, 2024 21:07
@tarcieri tarcieri mentioned this pull request Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants