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

Replace feature(layout_for_ptr) with feature(layout_for_meta) #67

Closed
CAD97 opened this issue Jul 13, 2022 · 5 comments
Closed

Replace feature(layout_for_ptr) with feature(layout_for_meta) #67

CAD97 opened this issue Jul 13, 2022 · 5 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api waiting-on-author

Comments

@CAD97
Copy link

CAD97 commented Jul 13, 2022

Proposal

Problem statement

Tracking issue: rust-lang/rust#69835

Current unstable API

// core;:mem
unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize;
unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize;

// core::alloc::Layout
unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self;

Proposed unstable API

// core;:ptr
unsafe fn size_for_pointee<T: ?Sized>(meta: <T as Pointee>::Metadata) -> usize;
unsafe fn align_for_pointee<T: ?Sized>(meta: <T as Pointee>::Metadata) -> usize;

// core::alloc::Layout
unsafe fn for_pointee<T: ?Sized>(meta: <T as Pointee>::Metadata) -> Self;

(bikeshed away)

Motivation

Per T-lang, these APIs fit nicely into the general story of ptr_metadata. The metadata is currently all that is required to calculate pointee layout, and there are potential language issues with allowing layout calculation to access the data pointer.

Solution sketches

See above for the proposed unstable API change.

Alternatively, the ptr_metadata APIs could all be adjusted to deal in a ptr::Metadata<T> wrapper type (c.f. rust-lang/rust#97052), and then size_of/align_of/layout methods could be provided like they are for DynMetadata. Unfortunately, they cannot be made (infallibly) safe, because it is perfectly safe/acceptable to cast raw pointers such that their metadata/type combo describes a pointee larger than the address space.

The former is the smaller change. The latter is potentially a more coherent design.

Of course, just providing all of the entry points is also an option.

Links and related work

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

@CAD97 CAD97 added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jul 13, 2022
@m-ou-se
Copy link
Member

m-ou-se commented May 17, 2023

This seems to assume the size of the object can fully be determined by the metadata, without the object pointer. But don't we want to support custom dynamically sized types in the future, like a thin &CStr, where the pointer part is necessary to determine the size? How would that work?

@joshtriplett
Copy link
Member

Also, it sounds like this ACP is referencing that lang team design note to say that there are problems with having types that require reading the pointee to get the size, but with my lang hat on, I don't understand how that note implies such problems.

@Amanieu Amanieu added I-libs-api-nominated Indicates that an issue has been nominated for discussion during a team meeting. and removed I-libs-api-nominated Indicates that an issue has been nominated for discussion during a team meeting. labels Sep 17, 2024
@Amanieu
Copy link
Member

Amanieu commented Sep 24, 2024

We discussed this in the libs-api meeting today and decided not to accept this for several reasons:

  • The proposed API doesn't work with type inference and forces you to write a turbofish.
  • The proposed API may not have enough information to work with custom DSTs like thin CStr pointers.

@Amanieu Amanieu closed this as completed Sep 24, 2024
@Amanieu
Copy link
Member

Amanieu commented Sep 24, 2024

Slight correction: actually this would work with custom DSTs, however the first point still stands so it doesn't make sense to remove the old API.

@CAD97
Copy link
Author

CAD97 commented Sep 24, 2024

Makes enough sense to me. AIUI these API may potentially make sense in addition to the *_of_val_raw functions, but don't logically want to replace those functions.

I don't see the requirement of a turbofish as too odd, since the sized *_of versions also require a turbofish, but the point about wanting API that works with read-requiring DST makes sense.

I might make a more constrained ACP once I've wrapped up some of my open work (I still think that a type level guarantee of no pointer access is highly useful, i.e. MetaSized vs DynSized), but that's for later when I make a new push for my ideas on supporting exotically sized types. (Actually, it'd probably be more of an MCP tbh.)

(Notably, *_for_pointee could plausibly be made safe, whereas *_for_val_raw cannot.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api waiting-on-author
Projects
None yet
Development

No branches or pull requests

4 participants