Skip to content

Make Ptr invariants opaque and more ergonomic #1876

Closed
@joshlf

Description

@joshlf

Once we publish 0.9, which will have a sufficiently high MSRV that we have access to GAT, we implement the following design. This will permit all concrete Invariants types to be private. This, in turn, means that we can preserve the ability to make future changes to how we represent invariants, or even add new invariants.

trait Invariants {
    // The invariants encoded by `Self`
    type Aliasing: Aliasing;
    type Alignment: Alignment;
    type Validity: Validity;

    // `Self` except with one invariant modified
    type WithAliasing<A: Aliasing>: Invariants<
        Aliasing = A,
        Alignment = Self::Alignment,
        Validity = Self::Validity,
    >;
    type WithAlignment<A: Alignment>: Invariants<
        Aliasing = Self::Aliasing,
        Alignment = A,
        Validity = Self::Validity,
    >;
    type WithValidity<A: Validity>: Invariants<
        Aliasing = Self::Aliasing,
        Alignment = Self::Alignment,
        Validity = V,
    >;
}

struct Ptr<'a, T, I: Invariants> { ... }

impl<'a, T, I: Invariants> Ptr<'a, T, I> {
    // Look! No concrete types!
    pub unsafe fn assume_aligned(self) -> Ptr<'a, T,  I::WithAlignment<Aligned>> { ... }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions