Giter Site home page Giter Site logo

Add a mock MaybeUninit about loom HOT 4 OPEN

jschwe avatar jschwe commented on June 2, 2024
Add a mock MaybeUninit

from loom.

Comments (4)

taiki-e avatar taiki-e commented on June 2, 2024

I believe this is basically an area that Miri or sanitizer should handle. It would be very difficult to detect the okay assume_init call such as MaybeUninit<[T;N]> -> [MaybeUninit<T>;N] on the library side.

from loom.

jschwe avatar jschwe commented on June 2, 2024

It would be very difficult to detect the okay assume_init call such as MaybeUninit<[T;N]> -> [MaybeUninit;N] on the library side.

It may be difficult if we wanted to detect every possible way of interacting with the MaybeUninit value. But I think this would already be very useful when we can detect issues if users restrict themselves to a subset of the std API (i.e. don't use pointers or transmute). The advantage of loom is that it can explore all possible interleavings, which I don't think miri or sanitizers could offer.

What I have in mind boils down to the following:

function Behavior
as_ptr / as_mut_ptr Transition the state to Unknown, which effectively disables assertions as long as we are in this state.
new Create with state initialized
assume_init and friends Assert that the value is initialized (unless we are in the Unknown state)
uninit Create with state uninit
write Set state to initialized
read Assert that the value is initialized (unless we are in the Unknown state) and transition to unknown
take assert that the value is initialized and transition the state to uninit
zeroed Create with state Unknown

from loom.

Darksonn avatar Darksonn commented on June 2, 2024

assert that the value is initialized and transition the state to uninit

This is not always correct. The type could be Copy, in which case it remains initialized (and its not possible to check whether its Copy in generic code). It's also possible that the type has only Copy fields but isn't Copy itself — in this case it can still be ok for it to remain initialized. Furthermore, the user could also mem::forget the value and conceptually have this transfer the value back into the MaybeUninit.

from loom.

jschwe avatar jschwe commented on June 2, 2024

This is not always correct. The type could be Copy, in which case it remains initialized (and its not possible to check whether its Copy in generic code).

I updated the table to also include a read method which transitions to the Unknown state.
take is meant to be used when the user explicitly wants to take ownership of the value and move it out.

There are very clear limitations of what we can check, but I think it would work well for data structures like a queue or stack, where you want to insert something once, and then take the value out again exactly once and never twice.

from loom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.