-
Notifications
You must be signed in to change notification settings - Fork 544
custom-set: Use &[...]
instead of vec![...]
#457
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
Conversation
The current tests of CustomSet pass in an owned Vec to `CustomSet::new`. This actually seems to make sense, as it allows a FromIterator approach and avoids cloning. Note that this doesn't obviate the need for the T to be Clone, because unions will surely require cloning. The alternative approach expressed by this PR is to state that `new` will not consume the input, instead cloning its elements. The example had to be reworked to make this work, so this affects the interface students are expected to adhere to. I (the author of this commit) am no longer sure this is a good idea, so let's have some discussion on it.
Given that passing in an owned data structure is reasonable for the constructor for custom set. Given that it is better to pass a slice than a Vec to enable other sliceable types. Might it work to pass an owned slice to the constructor? I honestly don't know if rustc allows this. Sorry I haven't simply tested this myself; time remains precious. If it compiles, I think that might be the way to go. |
Thinking about it, I'm fairly certain that there are certain intrinsic difficulties with the concept of an owned slice, and rustc will not allow it. One might imagine an implementation in which I think that this approach, passing in a slice and cloning as required, is as close to ideal as we can reasonably expect. It does require cloning on set creation, but for |
I'll take this Approval to mean that the proposed state is better than the current state. I've been reflecting more and if I decide I agree (I mentioned I wasn't sure), I will indeed merge. I will also discuss whether this is the desired final state, OR if there is another change that should be made after this. I'm trying to answer this by answering the question "Does this match the API of the standard Rust containers?" At least for I now see that pursuing that strategy would lead back into the original as shown in #19 ! I believe the change was made in #114 because it was not clear that |
I have decided this is better than the original, and the reason that convinces me will be:
Emphasis mine. I acknowledge that this API is still significantly different from https://doc.rust-lang.org/std/collections/struct.HashSet.html . We can revisit whether it's better to return to it later. |
(the stub was empty, so I did not need to rebase to perform a stub compilation check) |
I think this should be stated differently. We don't provide a stub (yet! but we could!). If we did, then we would probably make the stub suggest In the absence of any suggestion that a stub makes, students were probably able to write some implementation that uses IntoIterator: #489 works before this PR. The equivalent after this PR is #456. |
I decided not to spend a particularly long time thinking about this. In my view, the important part of this exercise is implementing the interesting operations on the set, not how the set itself is constructed. Thus, we should make set construction relatively convenient, which implies we should not use the original API of #19, and that it does not particularly matter whether this PR was applied. If wanting to demonstrate converting various iterable types can/should convert to/from one another, it would be interesting to have another exercise that shows that. |
The current tests of CustomSet pass in an owned Vec to
CustomSet::new
.This actually seems to make sense, as it allows a FromIterator approach
and avoids cloning.
Note that this doesn't obviate the need for the T to be Clone, because
unions will surely require cloning.
The alternative approach expressed by this PR is to state that
new
will not consume the input, instead cloning its elements.
The example had to be reworked to make this work, so this affects the
interface students are expected to adhere to.
I (the author of this commit) am no longer sure this is a good idea, so
let's have some discussion on it.