Make easier to add shuffle to other types #1132
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Change how shuffle is implemented and provided so that the functionality is provided by implementors of a trait.
Also, change how it works so that it mutates the existing value. But provide a
to_shuffled
function as well.Why
There's a few reasons for the different changes that were difficult to break apart which is why I've submitted them all in this one PR.
Providing the shuffle functionality via a trait allows us to define shuffle behavior on a wider variety of types, other than only types that implement
IntoVal<_, Vec<Val>>
. For example, we'd be able to provide the functionality on[T; N]
as well or some other types in the future.Providing the shuffle functionality as a mutation of the type also provides some optimizations in cases where the developers wants to shuffle something without allocating a new copy of it on the stack or elsewhere. Even though the functionality is changed to mutate, it's still possible for the Vec case for us to include a to_shuffled function that provides a copy. In the end this communicate clearer.
I implemented ToShuffled for all Shuffle, but maybe that's a mistake. Blanket implementations often come back to bite us.
Note
This is a breaking change. There isn't really a good way to deprecate this and reuse the fn names. We're still in pre-release though, so I think that's okay.