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

Extend Ptr to store unboxed values #1875

Open
joshlf opened this issue Oct 11, 2024 · 0 comments
Open

Extend Ptr to store unboxed values #1875

joshlf opened this issue Oct 11, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented Oct 11, 2024

Overview

Extend Ptr to support storing unboxed Ts by-value. Retain existing invariant transformations which make sense in the context of values (e.g., transformations on bit validity).

Motivation

Many of the behaviors of Ptr aren't actually specific to pointers. For example, a hypothetical MaybeValue<T> could keep track of whether it owns a bit-valid T just as Ptr does, and could provide various invariant state transitions.

Instead of introducing a new MaybeValue type, we could just teach Ptr to also store values. This would permit us to re-use existing Ptr conversions which are not specific to pointers and apply those conversions to values.

This would also permit us to replace or unify some existing abstractions:

It would also dovetail nicely with extending Ptr to support other pointer types.

Design

Building on #1797, we can make this support T: ?Sized + KnownLayout like so:

struct Ptr<'a, T, I>
where
    T: ?Sized,
    I: Invariants,
    I::Aliasing: Aliasing<'a, T>
{
    inner: <I::Aliasing as Aliasing<'a, T>>::Inner,
}

trait Invariants {
    type Aliasing;
}

trait Aliasing<'a, T: ?Sized> {
    type Inner;
}

enum Value {}

impl<'a, T: ?Sized + KnownLayout> Aliasing<'a, T> for Value {
    type Inner = T::MaybeUninit; // New associated type on `KnownLayout`; see #1797, #1822
}

enum Shared {}

impl<'a, T: ?Sized + KnownLayout> Aliasing<'a, T> for Shared {
    type Inner = NonNull<T>;
}

enum Exclusive {}

impl<'a, T: ?Sized + KnownLayout> Aliasing<'a, T> for Exclusive {
    type Inner = NonNull<T>;
}

This design is prototyped here, although it looks slightly different since we have to redefine various zerocopy internals to get it to work on the Rust playground.

This dovetails with #1839, which may require us to store something other than a NonNull<T> for Exclusive-aliased Ptrs.

Open questions

  • How do we support Drop? If we're supporting boxed (Box<T>) or unboxed (T) values, we need to support Drop, but only when certain invariants (e.g., bit validity) are satisfied.
@joshlf joshlf changed the title Teach Ptr to store unboxed values Extend Ptr to store unboxed values Oct 11, 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