-
Notifications
You must be signed in to change notification settings - Fork 20
Add a function to convert &T
to &MaybeUninit<T>
#561
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
Comments
The motivation only mentions chaining with |
Frankly i'd consider I don't want to focus on whether |
This makes it sound like some kind of language support might be useful? |
This operation is unsound in combination with #![feature(maybe_uninit_as_bytes)]
use std::cell::Cell;
use std::mem::MaybeUninit;
fn as_uninit<T>(v: &T) -> &MaybeUninit<T> {
unsafe { &*(v as *const T as *const MaybeUninit<T>) }
}
// `b` is `noalias`, which means that any byte that is *accessed* (read or write)
// through `b` must not be written to for the duration of this call, except via
// pointers derived from `b`.
//
// In this example
// - `a` and `b` alias (they point to the same byte)
// - `a` is not derived from `b`
// - `b` is accessed (read)
// - `a` is written to
// => UB
fn foo(a: &Cell<u8>, b: &MaybeUninit<u8>) {
a.set(1);
let _v = *b;
}
fn main() {
let v = Cell::new(0);
foo(&v, &as_uninit(&v).as_bytes()[0]);
} And miri correctly flags this as UB for both aliasing models: stacked borrows
tree borrows
To make this sound, there would have to be a |
Similar to All of these are cases of sub/supertyping that Rust doesn't natively support. (If the language were to support this, we'd also have |
cc @oli-obk - At FOSDEM we talked about MaybeUninit in the context of pattern types, which might allow conversions like this. |
We discussed this in today's @rust-lang/libs-api. We agreed that there should be a @Amanieu suggested the name |
Proposal
Problem statement
See motivating examples.
Motivating examples or use cases
MaybeUninit::as_bytes
(unstable) can see any readonly maybe-uninit value as a readonly sequence of maybe-uninit u8. If there's a function that can see any&T
as&MaybeUnit<T>
, it can work together with that function.Solution sketch
Add a function to convert
&T
to&MaybeUninit<T>
. I don't have any naming suggestions.Alternatives
Add a function to directly convert
&T
to&[MaybeUninit<u8>]
, which is less flexibleWhat 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: