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

Ptr invariant conversions could be smarter #1122

Open
joshlf opened this issue Apr 18, 2024 · 0 comments
Open

Ptr invariant conversions could be smarter #1122

joshlf opened this issue Apr 18, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented Apr 18, 2024

See also: #1359

Consider Ptr::transparent_wrapper_into_inner:

zerocopy/src/pointer/ptr.rs

Lines 609 to 627 in be56a3a

/// `Ptr<'a, T = Wrapper<U>>` → `Ptr<'a, U>`
impl<'a, T, I> Ptr<'a, T, I>
where
T: 'a + TransparentWrapper<I, UnsafeCellVariance = Covariant> + ?Sized,
I: Invariants,
{
/// Converts the `Ptr` to a transparent wrapper type into a `Ptr` to the
/// wrapped inner type.
pub(crate) fn transparent_wrapper_into_inner(
self,
) -> Ptr<
'a,
T::Inner,
(
I::Aliasing,
<T::AlignmentVariance as AlignmentVariance<I::Alignment>>::Applied,
<T::ValidityVariance as ValidityVariance<I::Validity>>::Applied,
),
> {

Currently, if the validity invariant is Initialized and the validity variance is Invariant, the returned pointer will have the validity invariant Any. However, this throws away information: regardless of the specific validity requirements of a type, if all of the outer type's bytes are initialized, then the same is true of the inner type's bytes. In other words, the Initialized validity invariant should be preserved regardless of the validity variance.

In the general case, what we really need is the ability to express a generic mapping (Invariant, Variance) -> Invariant, which would allow us to express this behavior.

In other conversion functions, each invariant can be mapped as either:

  • Preserve the invariant
  • Map to a fixed invariant

These conversion functions could similarly benefit from a generic mapping Invariant -> Invariant.

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