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

Remove MaybeUninit::slice_as_(mut_)ptr and optionally add *const/*mut MaybeUninit<T> -> *const/*mut T type safe conversions #245

Closed
SUPERCILEX opened this issue Jun 26, 2023 · 2 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@SUPERCILEX
Copy link

Proposal

This ACP breaks off a small piece of #122 that wasn't discussed in depth.

Problem statement

The MaybeUninit::slice_as_(mut_)ptr methods force you to elide bounds checks to go from MaybeUninit<T> to T.

fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T

Motivating examples or use cases

There is evidence of unnecessary bounds elision occurring in the stdlib: notice the places that were switched to using array indexing in rust-lang/rust@f2e9b40.

Solution sketch

Remove MaybeUninit::slice_as_(mut_)ptr.

Optionally, I'd like to add methods for *const/*mut MaybeUninit<T> -> *const/*mut T. This restores the type safety provided by slice_as_ptr and enables improved type safety in other cases where one has a raw pointer to a MaybeUninit and wishes to get at the wrapped type. Without these methods, you'd just have to do the cast manually which isn't the end of the world.

impl<T> *const MaybeUninit<T> {
    pub const fn raw_as_ptr(self) -> *const T
}

impl<T> *mut MaybeUninit<T> {
    pub const fn raw_as_mut_ptr(self) -> *mut T
}

Links and related work

rust-lang/rust#103133

@SUPERCILEX SUPERCILEX added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jun 26, 2023
@m-ou-se
Copy link
Member

m-ou-se commented Jun 27, 2023

We discussed this in the libs-api meeting. We're on board with removing MaybeUninit::slice_as_ptr.

We did not reach consensus on adding the newly proposed methods, so that part of this ACP is not accepted.

@m-ou-se m-ou-se closed this as completed Jun 27, 2023
@SUPERCILEX
Copy link
Author

Sounds good! Will update the PR.

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
Projects
None yet
Development

No branches or pull requests

2 participants