Uh oh!
There was an error while loading. Please reload this page.
Add the formatter_len_hint method to Show and use it in to_string - #16544
Add the formatter_len_hint method to Show and use it in to_string#16544pczarn wants to merge 1 commit into
formatter_len_hint method to Show and use it in to_string#16544Conversation
There was a problem hiding this comment.
I think this should actually be a lower bound, or just a loosely defined "conservative estimate" (since a true upper bound can be much larger than the real value, leading to the same overallocation problems). In this case, returning 0 by default makes sense.
alexcrichton
commented
Aug 17, 2014
In general, can you provide data to support a change such as this? The issue only claims that it may be an issue, I don't see any concrete numbers one way or the other. I'd like to see some analysis which compares the heap usage before and afterwards of projects like rustc (and perhaps servo) to see if a change such as this reaps the theoretical benefits. In the past the code size generated by |
huonw
commented
Aug 17, 2014
(This can be addressed by storing a single pointer to a struct/tuple/vtable containing the two function pointers.) |
There was a problem hiding this comment.
To adjust for the fact that zero has one digit, so that
- there are at most
width - log2base - 1leading zeros - binary_digits is at least
log2base + 1 binary_digits / log2baseis never zero
pnkfelix
commented
Aug 17, 2014
Note also that instead of changing the definition of |
pczarn
commented
Aug 17, 2014
@pnkfelix, I agree. The change to I've just made these measurements. |
pczarn
commented
Aug 17, 2014
Here's why I forgot about it before. |
size_hint method to Show as a default methodsize_hint method to Show as a default method and use it in to_stringsize_hint method to Show as a default method and use it in to_stringsize_hint method to Show as a default method and use it in to_stringlilyball
commented
Aug 20, 2014
pubtraitShow{// ...fnsize_hint(val:&Self) -> Option<uint>{None}}thus turning usage into e.g. letmut output = io::MemWriter::with_capacity(Show::size_hint(self).unwrap_or(128));Although I am unsure how UFCS affects static trait methods. |
lilyball
commented
Aug 20, 2014
Edit: What am I thinking, third-party types can't implement |
pczarn
commented
Aug 20, 2014
This method has to work for many types. I agree that a method used once internally shouldn't have a generic name. Well, at least iterators don't implement How about changing it to |
eddyb
commented
Sep 13, 2014
pczarn
commented
Sep 13, 2014
Renamed to |
91ccbd5 to
fbb2981Comparepczarn
commented
Sep 22, 2014
Updated with a simplified implementation for floats. Added two tests. |
pczarn
commented
Sep 29, 2014
6cfef4e to
0e2ba80Comparesize_hint method to Show as a default method and use it in to_stringformatter_len_hint method to Show and use it in to_stringImplements `formatter_len_hint` for several simple types. Length of the formatted string is approximated for floats and integers.
0e2ba80 to
fbc04e6Comparealexcrichton
commented
Oct 1, 2014
Note that there has been talk of separating Committing to this style of API is taking us pretty hard down the road of using I'm curious, @aturon, do you have an opinion on this? This is something that can always be added backwards-compatibly later on, and I'd almost rather consider the stabilization of |
eddyb
commented
Oct 1, 2014
I believe the win is bigger, but most of it is already in (using |
aturon
commented
Oct 1, 2014
@pczarn, thanks for taking this on! I agree with @alexcrichton that, while this seems like a reasonable extension, it seems wise to resolve the basic question about the role of I'll try to get a draft together in the next day or two, and ping you for feedback before posting it. |
alexcrichton
commented
Oct 24, 2014
Closing to help clear out the queue, @aturon do you have an update on the RFC you were planning to write? |
SimonSapin
commented
Dec 11, 2014
|
…ykril Add completions to show only traits in trait `impl` statement This is prerequisite PR for adding the assist mentioned in rust-lang#12500 P.S: If wanted, I will add the implementation of the assist in this PR as well.
Fix links linking to non-existent pages. changelog: none

I'm trying to balance potential code bloat and precision of the upper bound.
size_hintdefault to 0 instead of None?size_hintfor floats? I've yet to think about it.size_hintalso that useful for the general case offormat!, as implemented in this PR, not onlyto_string?UFCS would make this addition prettier.Examples
Fixes#16415