Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TryFromBytes for UnsafeCell #905

Closed
joshlf opened this issue Feb 17, 2024 · 1 comment
Closed

Implement TryFromBytes for UnsafeCell #905

joshlf opened this issue Feb 17, 2024 · 1 comment

Comments

@joshlf
Copy link
Member

joshlf commented Feb 17, 2024

TODO: Expand based on the following ideas:

  • We can't implement is_bit_valid for UnsafeCell because it requires reading from memory which is behind an UnsafeCell. There's no way to do this without raw pointers, and even with raw pointers, we don't know how other code is accessing the same memory (e.g., it might be using atomics, which would cause UB if we accessed it via non-atomic accesses).
    • We should modify is_bit_valid to have a where Self: NoCell bound.
  • We still want to be able to use TryFromBytes for UnsafeCell.
  • We should add a separate method: TryFromBytes::is_bit_valid_mut. Like is_bit_valid, but its Ptr argument has invariant::Exclusive.
    • We can call this method from try_read_from
  • Inside of the is_bit_valid_mut impl for UnsafeCell, we do the following:
    • Cast to UnsafeCell<Unalign<MaybeValid<T>>> (see below for discussion of MaybeValid)
    • Use Ptr::as_mut to get a &mut UnsafeCell<Unalign<MaybeValid<T>>>
    • Use UnsafeCell::get_mut to get a &mut Unalign<MaybeValid<T>>
    • Delegate to <Unalign<MaybeValid<T>> as TryFromBytes>::is_bit_valid_mut
  • Add MaybeValid<T>, which is just MaybeUninit<T> but with the "as initialized" invariant.
@joshlf
Copy link
Member Author

joshlf commented Feb 27, 2024

Closed by #912

@joshlf joshlf closed this as completed Feb 27, 2024
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

No branches or pull requests

1 participant