Comments (4)
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.
@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.
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.
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)
- `ASSERT_SIZE_MULTIPLE_OF` when casting `&[u8]` to `&[[u8; 16]]` HOT 1
- Crate documentation is misleading, implies `Pod` required for all functions. HOT 3
- the `FooBits` type generated by `#[derive(CheckedBitPattern)]` should implement `NoUninit` if `Self` is `NoUninit` HOT 3
- False positive derive error for `TransparentWrapper` around types like `Box` HOT 1
- Potential rustc bug related to deriving ByteEq HOT 2
- no-uninit trait with interior mutability HOT 6
- Deriving Pod for arbitrary generic types HOT 1
- Why use from_bytes when you can use pod_read_unaligned? HOT 5
- Build error on nightly-2024-02-05 HOT 1
- Bytemuck 1.14.2 does not build on stable HOT 1
- Feature request: Casting owned slices or vec, I.e. Box<[u64]> to Box<[u8]> or Vec<u64> to Vec<u8> HOT 4
- conversion from `&[u8]` to `&[T]`? HOT 4
- Allow deriving `Zeroable` for enums which are `#[repr(u32, i8, etc..)]` HOT 2
- Address safety invariants in documentation HOT 1
- No documentation for the FooBits public type generated by derive(CheckedBitPattern) HOT 1
- Casts and conversions for owned, boxed slices HOT 5
- `#[derive(TransparentWrapper)]` doesn't work with `where` clause generic bounds. HOT 1
- Unsound usages of unsafe implementation from different generics HOT 2
- Unsound usages of unsafe implementation from `[u8]` to `T` HOT 2
- impl `ZeroableInOption` for all `Zeroable`? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bytemuck.