Skip to content
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

move_into: Split into methods .move_into() and .move_into_uninit(). #997

Merged
merged 2 commits into from May 14, 2021

Conversation

bluss
Copy link
Member

@bluss bluss commented May 9, 2021

Two methods for Array:

  • .move_into(impl Into<ArrayViewMut<A, ..>>)
  • .move_into_uninit(impl Into<ArrayViewMut<MaybeUninit<A>, ..>>)

The uninit version is an optimization - it is more efficient since we don't need to keep track of the overwriting of existing values in the array. However, we optimize so that if A has no drop, they should be equivalent. So the user can use whichever fits best without worrying so much.

The new move_into turned out to be very simple: just swap the elements in the old and new array. This means we never have a partially moved-from array to keep track of and no headaches. We simply delegate over to the non-drop method if we can. We also use a needs_drop check in move_into_uninit to skip all the complicated dropping code if the element type doesn't have drop.

@bluss bluss changed the title move_into: Skip dropping if element doesn't have drop move_into: Skip dropping and split function into .move_into_uninit/_overwrite May 9, 2021
@bluss
Copy link
Member Author

bluss commented May 9, 2021

The comment on why a public into_maybe_uninit() conversion is problematic, it disappeared after the force push:

If we have an Array1<Option<&i32>> and make an ArrayViewMut1<MaybeUninit<Option<&i32>> from it, we can fill it with uninit or zeroed elements in safe code, which ends up messing up the original Array1. 🙁

(Something in type theory about mutability leading to that the type constructor having to be invariant.. something like that.)

@bluss bluss added this to the 0.15.2 milestone May 10, 2021
@bluss bluss changed the title move_into: Skip dropping and split function into .move_into_uninit/_overwrite move_into: Split into methods .move_into() and .move_into_uninit(). May 14, 2021
@bluss bluss merged commit 74c7994 into master May 14, 2021
@bluss bluss deleted the move-into-fixes branch May 14, 2021 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant