-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Liballoc IntoIter use as_mut_slice directly #74144
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
@RalfJung I didn't realized it removes part of that. But this is to reduce duplicates, I saw that it could just use the public function directly since it is the same to use |
It is wrong. The old code created To give a simpler example (too simplistic for fn okay_function() -> *mut [i32] {
ptr::slice_from_raw_parts_mut(0 as *mut _, 42)
}
fn very_not_okay_function() -> &mut [i32] {
slice::from_raw_parts_mut(0 as *mut _, 42)
} Calling the second function is Undefined Behavior. |
But it is still using |
Yes but you are also creating a reference. When I inline fn very_not_okay_function() -> &mut [i32] {
&mut *ptr::slice_from_raw_parts_mut(0 as *mut _, 42)
} That's your code. References in Rust are meaningful. Turning a raw pointer to a reference is a big promise you are making to the compiler. In this case, that promise is not justified. |
Specifically, if the contents of the vector are not properly initialized, your patch violates one of the core rules of Rust: You must not, under any circumstances,
|
Ah, I think I kinda understand it now, turning a raw pointer to a reference is bad. But I see that both place uses unsafe, and the build still passes, I didn't thought it was bad. |
"unsafe" doesn't mean "you can do bad things". "unsafe" means "it is your responsibility to make sure that you are not doing bad things". In safe code, the compiler can ensure you are not doing anything bad. By writing This is why |
But we are not pointing to an invalid value here right? |
It points to the buffer with the |
No description provided.