icu4x
icu4x copied to clipboard
Should we implement `pub fn to_string` on Writeable concrete types?
Currently most Writeables implement Display which provides ToString. This means that people can write foo.to_string(), which uses the slower Display-based code path instead of the faster Writeable::write_to_string code path.
We could fix this in many cases by adding a pub fn to_string on concrete types implementing Writeable. This would cause foo.to_string() to use the faster path; the slow path would need a fully qualified call site such as ToString::to_string(&foo).
Note: pub fn to_string would return String, so write_to_string, which returns Cow<str>, would still be optimal. But at least pub fn to_string would be closer to optimal.
Thoughts?
- [x] @Manishearth
- [x] @robertbastian
- [ ] @zbraniecki
Seems fine. Should we just return a Cow there too?
which uses the slower Display-based code path instead of the faster Writeable::write_to_string code path.
I'm gonna need numbers for this claim.
which uses the slower Display-based code path instead of the faster Writeable::write_to_string code path.
I'm gonna need numbers for this claim.
https://github.com/unicode-org/icu4x/pull/4649
Cool
@Manishearth:
- Is it a breaking change to add a concrete impl of a
to_stringfunction on a type that already implements Display? - Is it a breaking change to do that if the function returns
Cowinstead ofString?
Seems fine. Should we just return a Cow there too?
Cow is slightly more annoying to deal with; I lean toward returning String directly. If people want the Cow they can call write_to_string as they can now. In most of our impls we return a Cow::Owned anyway.
- Is it a breaking change to add a concrete impl of a to_string function on a type that already implements Display?
- Is it a breaking change to do that if the function returns Cow instead of String
Best avoided but this kind of thing mostly falls under not being breaking.
If it returns Cow i'd perhaps name it something appropriately cowish
OK, consensus on the following proposal then?
- All ICU4X types that implement
Writeablegain their own concretepub fn to_string(&self) -> Stringfunction. The list of types can be found here. - Mechanically, this can be done by adding a flag to
impl_display_with_writeable!to add the desired function. - To be extra safe, make this change in 2.0 (as opposed to 1.5 or 2.1)
LGTM?
LGTM with the addition: 4. Properly document this prelude-trait-method shadowing and why we do it (maybe linking to the benchmark)
LGTM with 4