Skip to content

Keep UniversalRead<u8> bound, do bytemuck conversion at call site - #8953

Merged
coszio merged 1 commit into
optionalpointer-typefrom
optionalpointer-keep-u8-bound
May 7, 2026
Merged

coszio merged 1 commit into
optionalpointer-typefrom
optionalpointer-keep-u8-bound

Conversation

@generall

@generall generall commented May 7, 2026

Copy link
Copy Markdown
Member

Summary

The switch to bytemuck::Pod for OptionalPointer (and now TrackerHeader) is nice, but I think having two Ts on S (UniversalRead<u8> + UniversalRead<OptionalPointer> / UniversalWrite<u8> + UniversalWrite<OptionalPointer>) creates too much turbofish noise — every method that exists on both impls (flusher, len, populate, open) has to be disambiguated with UniversalWrite::<u8>::flusher(&self.storage) / <S as UniversalRead<u8>>::open(...) etc., which is hard to read.

I'd prefer doing the conversion manually at the call site instead. Since OptionalPointer already derives bytemuck::Pod, we can keep the storage bound as plain UniversalRead<u8> / UniversalWrite<u8> and use:

  • bytemuck::from_bytes::<OptionalPointer>(bytes.as_ref()) for reads (returns &T, runtime-checks alignment — same shape and assumption as the original transmute_from_u8)
  • bytemuck::bytes_of(&pointer) for writes

This keeps the unsafe-transmute removal from the previous two commits (and additionally drops the legacy transmute_* for TrackerHeader too), but all the explicit-turbofish noise goes away — self.storage.flusher(), self.storage.len(), S::open(...) everywhere.

Net change vs. branch HEAD: same safety improvement, no extra trait bounds, no turbofish.

Test plan

  • cargo test -p gridstore — all 58 tests pass (20 tracker, 38 gridstore)
  • cargo check -p gridstore clean
  • Decide whether the read_multi_iter &OptionalPointer -> to_option() is worth the small per-entry copy vs. the typed-storage approach (negligible at 16 bytes)

🤖 Generated with Claude Code

Drop the `+ UniversalRead<OptionalPointer>` / `+ UniversalWrite<OptionalPointer>`
trait bounds added in the previous two commits. Reads and writes of
`OptionalPointer` and `TrackerHeader` now go through `bytemuck::from_bytes` /
`bytemuck::bytes_of` on the existing `u8` byte slices instead.

This keeps the unsafe-transmute removal but avoids the turbofish noise
(`UniversalWrite::<u8>::flusher(&self.storage)`, `<S as UniversalRead<u8>>::open`,
…) that two `T`s on `S` forced everywhere.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@generall
generall requested a review from coszio May 7, 2026 22:48

@coszio coszio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I wish we didn't choose T to be part of the trait definition.

@coszio
coszio merged commit d979396 into optionalpointer-type May 7, 2026
14 checks passed
@coszio
coszio deleted the optionalpointer-keep-u8-bound branch May 7, 2026 23:45
Comment on lines +348 to +352
let bytes = self.storage.read::<Random>(ReadRange {
byte_offset: start_offset as u64,
length: size_of::<OptionalPointer>() as u64,
})?;
let opt: &OptionalPointer = bytemuck::from_bytes(bytes.as_ref());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code reads u8 slice/vec then casting it into OptionalPointer. The issue: read might return unaligned Vec<u8>.

coszio pushed a commit that referenced this pull request May 8, 2026
…8953)

Drop the `+ UniversalRead<OptionalPointer>` / `+ UniversalWrite<OptionalPointer>`
trait bounds added in the previous two commits. Reads and writes of
`OptionalPointer` and `TrackerHeader` now go through `bytemuck::from_bytes` /
`bytemuck::bytes_of` on the existing `u8` byte slices instead.

This keeps the unsafe-transmute removal but avoids the turbofish noise
(`UniversalWrite::<u8>::flusher(&self.storage)`, `<S as UniversalRead<u8>>::open`,
…) that two `T`s on `S` forced everywhere.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
generall added a commit that referenced this pull request May 8, 2026
* derive bytemuck::Pod on `OptionalPointer`, get rid of legacy transmute

* remove one layer of Option in `get_raw`

* keep `UniversalRead<u8>` bound, do bytemuck conversion at call site (#8953)

Drop the `+ UniversalRead<OptionalPointer>` / `+ UniversalWrite<OptionalPointer>`
trait bounds added in the previous two commits. Reads and writes of
`OptionalPointer` and `TrackerHeader` now go through `bytemuck::from_bytes` /
`bytemuck::bytes_of` on the existing `u8` byte slices instead.

This keeps the unsafe-transmute removal but avoids the turbofish noise
(`UniversalWrite::<u8>::flusher(&self.storage)`, `<S as UniversalRead<u8>>::open`,
…) that two `T`s on `S` forced everywhere.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>

* extra refactoring after #8955

* explicit zeroed instantiation

---------

Co-authored-by: Andrey Vasnetsov <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
generall added a commit that referenced this pull request May 22, 2026
* derive bytemuck::Pod on `OptionalPointer`, get rid of legacy transmute

* remove one layer of Option in `get_raw`

* keep `UniversalRead<u8>` bound, do bytemuck conversion at call site (#8953)

Drop the `+ UniversalRead<OptionalPointer>` / `+ UniversalWrite<OptionalPointer>`
trait bounds added in the previous two commits. Reads and writes of
`OptionalPointer` and `TrackerHeader` now go through `bytemuck::from_bytes` /
`bytemuck::bytes_of` on the existing `u8` byte slices instead.

This keeps the unsafe-transmute removal but avoids the turbofish noise
(`UniversalWrite::<u8>::flusher(&self.storage)`, `<S as UniversalRead<u8>>::open`,
…) that two `T`s on `S` forced everywhere.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>

* extra refactoring after #8955

* explicit zeroed instantiation

---------

Co-authored-by: Andrey Vasnetsov <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants