Impl From<String> for ColoredString#126
Impl From<String> for ColoredString#126kurtlawrence merged 1 commit intocolored-rs:masterfrom mahor1221:master
Conversation
744b5dd to
1921f20
Compare
|
Why's this needed @mahor1221? Running |
|
I frequently use the fn paint(vec: Vec<String>) -> ColoredString {
vec
.iter()
.map(|_| todo!())
.collect::<String>()
.into()
}In the code above, if I change |
|
Is there anything blocking this? This functionality would be nice and I'm very surprised that this has not happened yet. From<&str> is not good enough since it requires both an additional copy and a new heap allocation where From would simply consume the underlying String into the ColoredString struct, maintaining the allocation and avoiding copying/moving the underlying str data. As @hwittenborn mentioned, it is possible to call any of the ColoredString-creating methods on a String but this simply goes down the Deref to the str underneath which once again results in an inherent heap allocation plus a memcpy for the data. I understand that it's rare that you're creating lots of ColoredStrings from Strings but I looked into this based on a use case where exactly that happens and this extremely small and simple addition of functionality could lead to vastly improved performance on these plaintext String to ColoredString conversions. |
|
Thanks @mahor1221! |
Complements
impl<'a> From<&'a str> for ColoredString.