Skip to content

Make ptr::from_raw_parts more general #362

Closed
@scottmcm

Description

@scottmcm

Proposal

Problem statement

Today, ptr::from_raw_parts can only accept *const () for the data_pointer.

That means that common cases -- like making a slice pointer -- have to cast.

Motivating examples or use cases

ptr::slice_from_raw_parts exists primarily so that something could be stabilized, but it's also there because if you're making a *const [T] what you really want to provide is a *const T and a usize, not a *const () and a usize.

There's really no reason, though, that ptr::from_raw_parts couldn't just take the *const T directly.

Solution sketch

Make a couple small tweaks to the signatures:

 pub const fn from_raw_parts<T: ?Sized>(
-    data_pointer: *const (),
+    data_pointer: *const impl Thin,
     metadata: <T as Pointee>::Metadata,
 ) -> *const T { … }
 pub const fn from_raw_parts_mut<T: ?Sized>(
-    data_pointer: *mut (),
+    data_pointer: *mut impl Thin,
     metadata: <T as Pointee>::Metadata,
 ) -> *mut T { … }

etc.

That way you still can't pass a fat pointer as the data_pointer, but if you happen to have a *mut u8 from an allocator, or something, that's fine.

And the turbo-fishing still lets you do from_raw_parts::<[T]> or from_raw_parts::<dyn Foo> without needing an extra _.

Alternatives

Obviously doing nothing here is a feasible option. It's not like casting is difficult.

But like with byte_offset_from (see rust-lang/rust#103489 ), there's really no particular reason to force a specific type here, and it's handy to accept multiple.

Links and related work

Inspired by rust-lang/rust#123190 since the extra casts were pushing Vec::deref over the MIR inlining threshold, but the cast to *const () also wasn't providing any value, so it would be nice to just not make people write it at all.

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions