Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Macro to initialize collections #207
Conversation
japaric
referenced this pull request
Aug 21, 2014
Closed
Add hashmap, hashset, treemap, and treeset macros #14726
This comment has been minimized.
This comment has been minimized.
Valloric
commented
Aug 21, 2014
|
This looks very nice; thanks for writing up the RFC! |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
Another alternative is to create a macro This also has these benefits:
|
This comment has been minimized.
This comment has been minimized.
treeman
commented
Aug 21, 2014
|
+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 |
This comment has been minimized.
This comment has been minimized.
netvl
commented
Aug 21, 2014
|
+1 for the implementation using |
This comment has been minimized.
This comment has been minimized.
|
I would be very interested to see an implementation of (I'm very happy to be proved wrong.) |
This comment has been minimized.
This comment has been minimized.
|
@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:
|
This comment has been minimized.
This comment has been minimized.
|
One possibility would be to use |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
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). |
This comment has been minimized.
This comment has been minimized.
pczarn
commented
Aug 22, 2014
|
A similar macro could initialize |
This comment has been minimized.
This comment has been minimized.
|
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:
|
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
SiegeLord
commented
Aug 23, 2014
|
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 defaultThis also resolves the Either way, if that's too crazy, I'm still voting for keeping |
This comment has been minimized.
This comment has been minimized.
|
@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). |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
nrc
assigned
nikomatsakis
Sep 4, 2014
alexcrichton
force-pushed the
rust-lang:master
branch
from
6357402
to
e0acdf4
Sep 11, 2014
aturon
force-pushed the
rust-lang:master
branch
from
4c0bebf
to
b1d1bfd
Sep 16, 2014
alexcrichton
force-pushed the
rust-lang:master
branch
from
b9e2b8c
to
5020131
Oct 29, 2014
This comment has been minimized.
This comment has been minimized.
|
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. |
japaric commentedAug 21, 2014
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)