-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Cannot move out of destructured fixed length vectors in let
and match
#16418
Comments
let
and match
let
and match
Closes #111 `FixedArray::from_fixed` cannot yet be implemented due to rust-lang/rust#16418
let
and match
let
and match
EDIT: ok, it is a bad idea, see below struct Vec2<S>(S, S);
fn from_array<S>(arr: [S, ..2]) -> Vec2<S> {
*unsafe { mem::transmute::<&[S, ..2], Box<Vec2<S>>>(&arr) }
} |
That is incorrect and it can corrupt memory/crash the app/anything; it results in calling It is more reliable to do: struct Vec2<S>(S, S);
fn from_array(arr: [S, .. 2]) -> Vec<S> {
unsafe {
let x = Vec2(ptr::read(&arr[0]), ptr::read(&arr[1]));
// stop the destructor running on the values in the array, i.e. transfer ownership:
mem::forget(arr);
x
}
} |
Function arguments working differently suggests to me that there could be some memory safety bugs that are not caught (not with moving out of fixed length arrays directly, but possibly something similar). |
Updated code sample: https://play.rust-lang.org/?gist=604564384c576d2d3862&version=nightly |
Yep, closing as duplicate of #26736. |
http://is.gd/bc1U2v
Note that it is impossible to work around this using transmute, because:
http://is.gd/BeSWOs
The text was updated successfully, but these errors were encountered: