Skip to content

Change uninitialized to return an array of MaybeUninit #685

Description

@jturner314

The .uninitialized() method on ArrayBase has some issues. For example, Array1::<bool>::uninitialized(2) is undefined behavior.

Rust 1.36 added std::mem::MaybeUninit for safer handling of uninitialized data. We should replace the existing .uninitialized() method on ArrayBase with one that returns an array of MaybeUninit instances. We then need to add the necessary methods to cleanly work with arrays like this (e.g. an array-level equivalent of assume_init).

Activity

  1. bluss commented on Aug 21, 2019

    @bluss
    Member

    Internally I'd kind of just like a Zip with ndproducers that would give out raw pointers to the elements (so that we avoid references and iteration is sound). That way we can use uninitialized or similar correctly using some raw pointer code, at least.

  2. bluss commented on Sep 1, 2019

    @bluss
    Member

    I'm not sure the recipe in the title of this issue is what we should do - there might be better ways to do it, for example to provide raw pointer traversals

  3. bluss commented on Apr 13, 2020

    @bluss
    Member

    Can we explain why Array1::<bool>::uninitialized(2) is UB? What's the difference to Vec::<bool>::with_capacity(2)?

    To me it looks like it can't be UB, on its own, but further method calls would be needed.

    Maybe this comment can back me up rust-lang/rust-clippy#4483 (comment)

    Edit: I have a tentative conclusion, Array1::<bool>::uninitialized(2) in itself is ok. However, if this array drops without being initialized, the &mut [bool] created in the method Vec::drop is not valid, and that's the reason this is a very hard API to work with (and it is not really panic safe, as was intended, even though it does not seem to have ill effects today. Meanwhile, I've even proposed a PR to rust to fix Vec::drop to not make a mutable slice in drop.)

  4. bluss commented on Apr 14, 2020

    @bluss
    Member

    Internal implementation of this is added in #797, that's a start.

  5. added this to the 0.14.0 milestone on Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions