-
Notifications
You must be signed in to change notification settings - Fork 19
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
Make ptr::from_raw_parts
more general
#362
Comments
…ts, r=<try> Avoid a cast in `ptr::slice_from_raw_parts(_mut)` Casting to `*const ()` or `*mut ()` is no longer needed after rust-lang#123840 so let's make the MIR smaller (and more inline-able, as seen in the tests). If [ACP#362](rust-lang/libs-team#362) goes through we can keep calling `ptr::from_raw_parts(_mut)` in these also without the cast, but that hasn't had any libs-api attention yet, so I'm not waiting on it.
…ts, r=joboet Avoid a cast in `ptr::slice_from_raw_parts(_mut)` Casting to `*const ()` or `*mut ()` is no longer needed after rust-lang#123840 so let's make the MIR smaller (and more inline-able, as seen in the tests). If [ACP#362](rust-lang/libs-team#362) goes through we can keep calling `ptr::from_raw_parts(_mut)` in these also without the cast, but that hasn't had any libs-api attention yet, so I'm not waiting on it.
Nominating because it seems like it fell off the "new ACPs" agenda without comment. It's not urgent, though; address it only after any more-pressing nominees. @rustbot labels +I-libs-api-nominated |
Ideally we'd do better with something like |
Interesting, @the8472 . I think that'd be particularly useful for For constructing, I do wonder about things like the "flat vector of dyn", where it might in practice have I guess we could always experiment with a TL/DR: |
We discussed this in the libs-api meeting today and we're mostly happy to accept it. We can defer the final decision to stabilization since this depends on several unstable features (e.g. trait aliases for |
…=WaffleLapkin [ACP 362] genericize `ptr::from_raw_parts` This implements rust-lang/libs-team#362 As such, it can partially undo rust-lang#124795 , letting `slice_from_raw_parts` just call `from_raw_parts` again without re-introducing the unnecessary cast to MIR. By doing this it also removes a spurious cast from `str::from_raw_parts`. And I think it does a good job of showing the value of the ACP, since the only thing that needed new turbofishing because of this is inside `ptr::null(_mut)`, but only because `ptr::without_provenance(_mut)` doesn't support pointers to extern types, which it absolutely could (without even changing the implementation).
…=WaffleLapkin [ACP 362] genericize `ptr::from_raw_parts` This implements rust-lang/libs-team#362 As such, it can partially undo rust-lang#124795 , letting `slice_from_raw_parts` just call `from_raw_parts` again without re-introducing the unnecessary cast to MIR. By doing this it also removes a spurious cast from `str::from_raw_parts`. And I think it does a good job of showing the value of the ACP, since the only thing that needed new turbofishing because of this is inside `ptr::null(_mut)`, but only because `ptr::without_provenance(_mut)` doesn't support pointers to extern types, which it absolutely could (without even changing the implementation).
Rollup merge of rust-lang#125701 - scottmcm:generic-from-raw-parts, r=WaffleLapkin [ACP 362] genericize `ptr::from_raw_parts` This implements rust-lang/libs-team#362 As such, it can partially undo rust-lang#124795 , letting `slice_from_raw_parts` just call `from_raw_parts` again without re-introducing the unnecessary cast to MIR. By doing this it also removes a spurious cast from `str::from_raw_parts`. And I think it does a good job of showing the value of the ACP, since the only thing that needed new turbofishing because of this is inside `ptr::null(_mut)`, but only because `ptr::without_provenance(_mut)` doesn't support pointers to extern types, which it absolutely could (without even changing the implementation).
…pkin [ACP 362] genericize `ptr::from_raw_parts` This implements rust-lang/libs-team#362 As such, it can partially undo rust-lang/rust#124795 , letting `slice_from_raw_parts` just call `from_raw_parts` again without re-introducing the unnecessary cast to MIR. By doing this it also removes a spurious cast from `str::from_raw_parts`. And I think it does a good job of showing the value of the ACP, since the only thing that needed new turbofishing because of this is inside `ptr::null(_mut)`, but only because `ptr::without_provenance(_mut)` doesn't support pointers to extern types, which it absolutely could (without even changing the implementation).
Proposal
Problem statement
Today,
ptr::from_raw_parts
can only accept*const ()
for thedata_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 ausize
, not a*const ()
and ausize
.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:
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]>
orfrom_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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: