diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2015-04-19 10:59:06 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2015-04-19 10:59:06 -0700 |
| commit | f055054eab2a510ad3252599a015f660eef6aedd (patch) | |
| tree | 51469e5508a88c70fed0c0a6f38748ab5433c1cd /src/libcollections | |
| parent | 00978a9879d882002c59c5f3474fbe0b858963a7 (diff) | |
| download | rust-f055054eab2a510ad3252599a015f660eef6aedd.tar.gz rust-f055054eab2a510ad3252599a015f660eef6aedd.zip | |
collections: Move optimized String::from_str to String::from
This implementation is currently about 3-4 times faster than using the `.to_string()` based approach.
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/string.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 74af5783fa8..81710286fde 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1013,9 +1013,20 @@ impl AsRef<str> for String { #[stable(feature = "rust1", since = "1.0.0")] impl<'a> From<&'a str> for String { + #[cfg(not(test))] #[inline] fn from(s: &'a str) -> String { - s.to_string() + String { vec: <[_]>::to_vec(s.as_bytes()) } + } + + // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is + // required for this method definition, is not available. Since we don't + // require this method for testing purposes, I'll just stub it + // NB see the slice::hack module in slice.rs for more information + #[inline] + #[cfg(test)] + fn from(_: &str) -> String { + panic!("not available with cfg(test)"); } } |
