Lokathor / bytemuck

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

Use safe, public APIs where possible to remove some `unsafe` blocks. #206

Closed anforowicz closed 1 year ago

anforowicz commented 1 year ago

trait CheckedBitPattern constraints the associated type Bits as follows: Bits: AnyBitPattern. This means that checked::try_from_bytes doesn't need to call unsafe internal::try_from_bytes but can instead call the (safe) public API crate::try_from_bytes (thanks to the AnyBitPattern constraint). Similar reasoning can be applied to the other snippets modified by this PR. Being able to successfully compile the crate after this PR seems to confirm this reasoning.

Lokathor commented 1 year ago

This all looks good. If someone can report a specific performance problem we could go back to directly calling the unsafe internal versions, but otherwise it's probably good policy to go through the safe interface when possible.