-
Notifications
You must be signed in to change notification settings - Fork 432
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
Implementing SliceRandom::choose_multiple(_weighted)
is impossible if you aren't simply forwarding the calls to an inner [T]
#1307
Comments
rand::seq::SliceRandom::choose_multiple(_weighted)
is impossible if you aren't simply forwarding the calls to an inner [T]
SliceRandom::choose_multiple(_weighted)
is impossible if you aren't simply forwarding the calls to an inner [T]
You're right; we didn't design this for extension by third-party crates. Changes to Rand are required. Some possibilities:
|
Can we return |
@vks no. I think this is one of Niko Matsaksis's goals, but it's not possible yet. |
@dhardy I'm unsure why you would need GATs for your second suggestion. If the intent is to add an associated type that translates to an Iterator, you can do that without GATs - look at how pub trait IntoIterator {
type Item;
type IntoIter: Iterator<Item = Self::Item>;
fn into_iter(self) -> Self::IntoIter;
} That all said, I can't help but feel |
@LikeLakers2 I think you're right; it doesn't need GATs. Lets go back to your original motivation:
If we move the latter two methods to a separate trait we should be able to implement all the former for any |
Sounds good to me! :) Shame that it can't be done without a breaking change, but hey, better now than past 1.x.
Indeed it does not, as there's no good way (to my knowledge) to return a |
Okay; we're already merging breaking changes so I think the plan should be something like this:
Assuming that your container implements |
How will If I might suggest an alternative, perhaps P.S. Lest you worry, this would not require consuming the container to turn it into an iterator: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a58d633964ef138956d804e28b15bf2e |
Good point. The only standard trait providing a The
So I don't like your proposal. An alternative might be to provide a trait like |
Ah, thank you for clarifying that. Admittedly, I've only taken a cursory glance at That sounds like a fine compromise to me! In the worst case, where a collection doesn't have an impl for One last thing though: There are some |
Hi! I'm trying to implement
rand::seq::SliceRandom
for a custom Vec-like collection I'm building. However, I found myself stuck in trying to implementSliceRandom::choose_multiple
.The collection is set up as follows:
Getting a
T
from this collection involves mapping ausize
obtained fromself.items
, to aT
obtained fromself.palette
. In most cases, I can useIterator::map
orOption::map
for this.However,
SliceRandom::choose_multiple
andSliceRandom::choose_multiple_weighted
require me to return arand::seq::SliceChooseIter
. I do not see a way to construct this on my own - the only way to make one is viaself.items.choose_multiple()
, which would return ausize
rather than aT
- and I especially do not see a way to implement this such that it maps theusize
s toT
s before returning values.Because I can't return a
SliceChooseIter
, I can't implementSliceRandom
- not unless I decide to putunimplemented!()
in the methods that return aSliceChooseIter
. This feels like a deficiency in therand
crate, one that I'm unsure how to solve - but I'm also wondering if there's a way around it that I'm unaware of.So, any help with this, please? Or is this something that would require
rand
to change howSliceRandom
is implemented?The text was updated successfully, but these errors were encountered: