Skip to content

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

Closed as not planned
@CAD97

Description

@CAD97

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions