-
Notifications
You must be signed in to change notification settings - Fork 430
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
WeightedChoice
is hard to use with borrow checker
#142
Labels
F-new-int
Functionality: new, within Rand
Comments
Related: #90. |
dhardy
added a commit
to dhardy/rand
that referenced
this issue
Jul 29, 2017
The change to WeightedChoice is debatable, but since the argument must normally be constructed specially anyway, the reference model makes little sense. See issues rust-random#90, rust-random#142 and PR rust-random#152.
5 tasks
rfdonnelly
added a commit
to rfdonnelly/rvs
that referenced
this issue
Jan 8, 2018
Notable changes: * transform::rand::Seed got simpler * Misc minor API differences * Swap out rand::distributions::WeightedChoice with custom WeightedIndexes type WeightedChoice for released version of rand does not own the array it operates on. Because of this, it is not possible to keep around an instance of WeightedChoice and reuse its internal range. The experimental version of rand resolves this but it cannot be used by published crates. Fix by rolling our own WeightedIndexes type. See rand issues: * rust-random/rand#90 * rust-random/rand#142
This API was deprecated in #518 and the new API seems to be doing better here. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WeightedChoice
is quite hard to use with Rust's borrow checker.To create a
WeightedChoice
you need to give it an&'mut [Weighted]
. This means that your[Weighted]
is unavailable untilWeightedChoice
goes out of scope.This makes it impossible to store a
WeightedChoice
in a structure, or return it from a function. Both scenarios would require you to also store or return your[Weighted]
, which is impossible because it's also borrowed byWeightedChoice
.As such, it's only possible to use a
WeightedChoice
inside the same scope that created the[Weighted]
, which means it is impossible to hide it away in an API.It doesn't seem however, that
WeightedChoice
actually needs a mutable reference to the[Weighted]
. It only needs this during its construction.The text was updated successfully, but these errors were encountered: