Lokathor / bytemuck

A crate for mucking around with piles of bytes
https://docs.rs/bytemuck
Apache License 2.0
697 stars 77 forks source link

Update TransparentWrapper asserts #232

Closed zachs18 closed 5 months ago

zachs18 commented 5 months ago
  1. Fixed the {wrap/peel}_slice methods asserts: previously they were comparing the layouts of *const Inner and *const Self, which is are always the same when they're both Sized; now they check that Inner and Self have the same layout.
    • This could cause panics for existing users with incorrect TransparentWrapper implementations.
  2. Added asserts to wrap and peel.
    • This could cause panics for existing users with incorrect TransparentWrapper implementations.
  3. Added comments explaining why we compare size of *const Self in {wrap/peel}_{ref/mut/rc/arc/box}.
  4. Moved some of the asserts and safety comments before the unsafe blocks (no semantic change, I just prefer it).

(I was going to also change the assert!s to debug_assert!s, but since the conditions are all const-foldable anyway, the only affect that would have would be hiding UB in release mode, with no benefit for correct implementations, so I left it using assert!)