-
Notifications
You must be signed in to change notification settings - Fork 13k
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
MaybeUninit requires T: Sized but it should not #80158
Comments
@mahkoh Can you explain why you think |
Being The functions
occur naturally in APIs that are generic over both uninitialized and initialized arguments. For example, you want to be able to write read_into(&mut [0u8]);
read_into(&mut [MaybeUninit::uninit()]);
read_into(&mut some_data_structure_that_can_be_read_into); with the same Because of the
This is not a |
Nominating for T-lang/T-libs discussion. |
The unsizing would be valuable for allocators. In some cases, one wants to use |
Allowing unsized So, while this could in principle be implemented right now (though it would need a special case since I don't see a way to support unsized union fields in general), this would also close the door for some other potential future language extensions. |
How is |
The difference is that it may not be initialized. Determining the size of an |
I had read somewhere that That aside, you also said that there would be a problem with unsized union fields. I did not see any such problems so I opened rust-lang/rfcs#3041 yesterday. Maybe leave an explanation there. |
Would it be possible to make |
I mean, this is all hypothetical since custom DST aren't actually a thing.^^ But determining the size will be crucial at least to support things like
I don't see how that would help with the size issue. It would have to be unsafe to ask for the value's size. |
Crucially in the same step as that relaxation it could be defined that producing a value |
Having any safety (or validity!) invariant associated with |
The notion that
|
So you are basically saying, Something like that could work in principle. How practical and ergonomic it would be... that's hard to tell. |
We discussed this in the lang team meeting today, and the general consensus was that we'd like to make sure we understand the path forward for using this with custom DSTs prior to expanding the abilities of It'd be helpful to see an example using one of the previous custom DST designs combined with a hypothetically-expanded |
I ran into this when experimenting with creating an aligned slice type: #[repr(align(32))]
#[derive(Debug)]
struct AlignedSlice([u8]);
impl AlignedSlice {
pub unsafe fn new_uninit(size: usize) -> Box<MaybeUninit<AlignedSlice>> {
unsafe {
let layout = Layout::from_size_align_unchecked(size, 32);
let ptr = std::alloc::System.alloc(layout);
Box::from_raw(std::slice::from_raw_parts_mut(ptr, size) as *mut [MaybeUninit<u8>] as *mut MaybeUninit<AlignedSlice>)
}
}
// TODO use MaybeUninit::assume_init when stable for this
pub unsafe fn assume_init(this: Box<MaybeUninit<AlignedSlice>>) -> Box<AlignedSlice> {
unsafe {
Box::from_raw(this.into_raw() as *mut AlignedSlice)
}
}
pub fn new(data: &[u8]) -> Box<AlignedSlice> {
let mut this = Self::new_uninit(data.len());
std::ptr::copy_nonoverlapping(data.as_ptr(), this.as_ptr_mut(), data.len());
Self::assume_init(this)
}
}
|
If it's undesirable to make |
I tried this code:
I expected to see this happen: It compiles
Instead, this happened: It does not compile
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: