Git Product home page Git Product logo

Comments (4)

Lokathor avatar Lokathor commented on September 26, 2024 1

If you cast a &mut [u8] to &mut MyType and then assign a new value the padding bytes of MyType will become de-initialized during the assignment.

from bytemuck.

jgarvin avatar jgarvin commented on September 26, 2024

@Lokathor that's very surprising... I'm having trouble coming up with a mental model where that behavior makes sense. My thinking:

  • &mut [u8] already requires all the bytes of the slice be initialized
  • Once cast to &mut MyType all of its padding bytes will be initialized even though typically they wouldn't be required to be.
  • Once assigned to another instance, one of two things can happen: 1) the assignment avoids copying the padding bytes, in which case we still expect all bytes of the original slice to be initialized. 2) the assignment does copy the padding bytes, in which case rustc introduced UB I didn't ask for by touching those bytes to do the copy

So is the way that it works that uninitialized bytes have unknown value, copies include padding bytes, but padding byte copies done by rustc instead of the user are special and not UB, but also propagate the unknown'ness, and even though every bit pattern for u8 is valid, rustc/llvm want to strongly assume none of the bytes in a slice have been infected?

from bytemuck.

Lokathor avatar Lokathor commented on September 26, 2024

I must admit I'm not an expert but this is my current understanding:

Yes, assigning to &mut MyType also "assigns" all the padding bytes to be un-initialized, even if they were previously initialized. This allows the compiler to, for example, pick between an assignment being a field-by-field copy, or being a memcpy copy, or whatever other strategy.

from bytemuck.

jgarvin avatar jgarvin commented on September 26, 2024

Thanks it's surprising but it makes sense. I still think it might make sense in the docs to link to somewhere explaining this (I don't know if there's an authoritative place?) but I'll close for now. Thanks for the explanation 👍

from bytemuck.

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.