You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@liigo it needs to be dynamic constructed (i.e. &str is completely inappropriate for this), and 128 bytes is way way way too much when there is only 1 or 3 bytes actually used (like in the two examples).
It would put a minor road-block before you could call to_string() on an arbitrary type implementing Show, but it seems like a pretty simple way to get most of what is desired here.
Good ideas. Should we use the upper bound for all integers? What about different bases? I think that integers shouldn't implement size_hint, because leading_zeros (Int trait) and size_of are sufficient for determining the number of digits in all radixes.
The size_hint could take a &fmt::Formatter as well as self to be able to answer the question more precisely.
I think that integers shouldn't implement size_hint, because leading_zeros (Int trait) and size_of are sufficient for determining the number of digits in all radixes.
How will they convey the computed number of digits if they don't implement that trait and method?
Rust was just picking 128 as an arbitrary amount to reserve up front before. Not using a size hint will have a severe impact on performance, but clamping to 128 was worse.
Activity
liigo commentedon Aug 11, 2014
String is a buffer, later appends will benefits from it. If don't need a
buffer, why not use &str?
huonw commentedon Aug 11, 2014
@liigo it needs to be dynamic constructed (i.e.
&stris completely inappropriate for this), and 128 bytes is way way way too much when there is only 1 or 3 bytes actually used (like in the two examples).pnkfelix commentedon Aug 11, 2014
Just so it does not get lost in the shuffle, here is my current half-baked idea to address this:
trait ToStringSupport : fmt::Show { fn size_hint(&self) -> Option<uint> { None } }, and then,impl<T: ToStringSupport> ToString for T(and then usewrite!into a buffer of appropriate capacity instead offormat!)see related bit of discussion here: https://botbot.me/mozilla/rust-internals/2014-08-11/?msg=19511520&page=2
It would put a minor road-block before you could call
to_string()on an arbitrary type implementingShow, but it seems like a pretty simple way to get most of what is desired here.eddyb commentedon Aug 12, 2014
@pnkfelix adding the hint method to
Showas a default method would break no existing code, I would prefer that.pczarn commentedon Aug 15, 2014
Good ideas. Should we use the upper bound for all integers? What about different bases? I think that integers shouldn't implement
size_hint, becauseleading_zeros(Inttrait) andsize_ofare sufficient for determining the number of digits in all radixes.huonw commentedon Aug 15, 2014
The
size_hintcould take a&fmt::Formatteras well asselfto be able to answer the question more precisely.How will they convey the computed number of digits if they don't implement that trait and method?
pczarn commentedon Aug 15, 2014
I've just found out that
Showis implemented forRadixFmt<T, Radix>,not directly on integers. This resolves my doubts.By using either
size_hintorleading_zeros. Looks like it was a bad idea. Or by implementingsize_hintforT: Show + Int.huonw commentedon Aug 15, 2014
It is.
pczarn commentedon Aug 15, 2014
Then it's as simple as extending these macros. I can do that.
barosl commentedon Dec 2, 2014
It seems that this issue can be closed now.
5u32.to_string().capacity()-> 1"foo".to_string().capacity()-> 4"hello".to_string().capacity()-> 8Now the buffer size is incremented to fit the minimum 2^n.
thestinger commentedon Dec 2, 2014
That makes it seem like it's just pushing them without using
with_capacityat all...barosl commentedon Dec 2, 2014
@thestinger Uhm, yeah, you're right. Now I'm worried that not using
with_capacity()could impact the performance...thestinger commentedon Dec 2, 2014
Rust was just picking 128 as an arbitrary amount to reserve up front before. Not using a size hint will have a severe impact on performance, but clamping to 128 was worse.
Auto merge of rust-lang#16415 - Waqar144:work/make-try-to-nav-pub, r=…
Assume that any external function might return a type alias (rust-lan…