-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: Macro to initialize collections #207
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
This looks very nice; thanks for writing up the RFC! |
One option is to define
Requiring Default to provide a nice default seems minimally onerous, since basically every collection can and should. For the naming bikeshed, I will toss out the name |
Another alternative is to create a macro This also has these benefits:
|
+1 Overall, something like this is very needed. Thanks for writing the RFC! I'm in favor for the implementation via I don't think we should deprecate
To me I like |
+1 for the implementation using |
I would be very interested to see an implementation of (I'm very happy to be proved wrong.) |
@huonw Yeah, I couldn't come up with a satisfying sketch myself. Various pending RFCs might make it more feasible by constructing a MagicIter containing some As a very silly shim, this works:
|
One possibility would be to use |
@Kimundi Thanks a really nice macro, I never tought of using a fixed size array like that! I did some comparisons between Here's the summary:
I'd be great if someone can replicate these measurements, and/or check if my measurements are flawed in some way. |
I'm curious, is there any reason you went with boxed ints as your testing element? Otherwise, I'm not totally surprised that this implementation of iter is slower, since it's effectively zeroing out and padding the memory, as well as copying it with the iterator. Could you try out my naive vec!-based implementation as well? It heap allocates and copies the elements, but it doesn't have to zero out any memory or include discriminants (from my cursor skim of move_iter). I'd be interested to see the difference. I'd also be interested in benching the construction of other structures. In particular, PriorityQueue should have O(n) FromIterator (but looking at the source, no one seems to have implemented that! Gonna go work on it). |
A similar macro could initialize |
This generally looks like a nice idea. It would allow us to prototype how well extensible vector syntax would work, which I like. The name of the trait seems less than ideal. This trait does not represent the general notion of a sequence, but rather just something that can be constructed via append. I want to correct two misconceptions:
In that case, you could impl the seq interface just fine:
|
@nikomatsakis I agree that persistent strucutures can work with this scheme, but it's likely going to be much less efficient than possible with FromIterator, which opens up a whole new area of potential optimizations. Again, heapify is a great canonical example of where FromIterator can blow iterative construction out of the water. We could possibly recover this performance with a triplet of functions:
Which would basically allow the structure to work like FromIterator, but in a a truly iterative manner. In the case of priority_queue, this would look like:
I mark the methods |
I concur with @treeman's point, and to that extent I don't see why it doesn't apply to let v = seq![1u]; // Made a Vec<uint>
let d: RingBuf<_> = seq![1u]; // Alter the default to a RingBuf
let m = map![1u => 1u]; // Made a HashMap<uint, uint>
let m: RbTree<_> = map![1u => 1u]; // Alter the default This also resolves the Either way, if that's too crazy, I'm still voting for keeping |
@gankro something like "seal" would require more of a builder-style interface, I think, wherein the seal method returns a new type representing the "sealed" data. I agree it'd be nice, not quite sure what it looks like. Probably worth experimenting more. As for having multiple macros, I think if we can have only one, that seems better, particularly if we get the generic API right (and I'm convinced now that the proposed interface is not necessarily right). |
Sorry, didn't finish that thought: I meant to say that if we have a generic API, and only one macro, it's nice because you can use the macro in generic code. |
How will this interact with the generic collection traits with HKTs post 1.0? It would be a shame to have a redundant trait hanging around once those land. |
6357402
to
e0acdf4
Compare
Closing/postponing this because (a) we removed all the collections traits, (b) also removed several collections from the stdlib, and (c) not going to ship any generic collection trait with 1.0. We should revisit this when we start thinking about adding generic collection traits to the stdlib. Which is definitely post 1.0, and maybe even after we get HKT. |
add select_any impl
Rendered view
Out of tree implementation
(This is a consolidation of the ideas that have been floating around rust-lang/rust#14726 into a RFC)