summary refs log tree commit diff
path: root/src/libstd/path/mod.rs
diff options
context:
space:
mode:
authorKevin Ballard <kevin@sb.org>2014-02-07 19:45:48 -0800
committerKevin Ballard <kevin@sb.org>2014-02-07 22:31:52 -0800
commit1d17c2129ec696d81e6c6caee8b1740dd9509090 (patch)
tree82db5e94279972bd3d12d4fa5fc765b7c0bc891e /src/libstd/path/mod.rs
parent086c0dd33febb752b036fba62dcfb8aa22a51642 (diff)
downloadrust-1d17c2129ec696d81e6c6caee8b1740dd9509090.tar.gz
rust-1d17c2129ec696d81e6c6caee8b1740dd9509090.zip
Rewrite path::Display to reduce unnecessary allocation
Diffstat (limited to 'src/libstd/path/mod.rs')
-rw-r--r--src/libstd/path/mod.rs32
1 files changed, 12 insertions, 20 deletions
diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs
index fd75a486e8d..ed0ce201750 100644
--- a/src/libstd/path/mod.rs
+++ b/src/libstd/path/mod.rs
@@ -70,7 +70,7 @@ use fmt;
 use iter::Iterator;
 use option::{Option, None, Some};
 use str;
-use str::{OwnedStr, Str, StrSlice};
+use str::{MaybeOwned, OwnedStr, Str, StrSlice, from_utf8_lossy};
 use to_str::ToStr;
 use vec;
 use vec::{CloneableVector, OwnedCloneableVector, OwnedVector, Vector};
@@ -495,7 +495,7 @@ pub struct Display<'a, P> {
 
 impl<'a, P: GenericPath> fmt::Show for Display<'a, P> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        self.with_str(|s| f.pad(s))
+        self.as_maybe_owned().as_slice().fmt(f)
     }
 }
 
@@ -505,33 +505,25 @@ impl<'a, P: GenericPath> ToStr for Display<'a, P> {
     /// If the path is not UTF-8, invalid sequences with be replaced with the
     /// unicode replacement char. This involves allocation.
     fn to_str(&self) -> ~str {
-        if self.filename {
-            match self.path.filename() {
-                None => ~"",
-                Some(v) => str::from_utf8_lossy(v).into_owned()
-            }
-        } else {
-            str::from_utf8_lossy(self.path.as_vec()).into_owned()
-        }
+        self.as_maybe_owned().into_owned()
     }
 }
 
 impl<'a, P: GenericPath> Display<'a, P> {
-    /// Provides the path as a string to a closure
+    /// Returns the path as a possibly-owned string.
     ///
     /// If the path is not UTF-8, invalid sequences will be replaced with the
     /// unicode replacement char. This involves allocation.
     #[inline]
-    pub fn with_str<T>(&self, f: |&str| -> T) -> T {
-        let opt = if self.filename { self.path.filename_str() }
-                  else { self.path.as_str() };
-        match opt {
-            Some(s) => f(s),
-            None => {
-                let s = self.to_str();
-                f(s.as_slice())
+    pub fn as_maybe_owned(&self) -> MaybeOwned<'a> {
+        from_utf8_lossy(if self.filename {
+            match self.path.filename() {
+                None => &[],
+                Some(v) => v
             }
-        }
+        } else {
+            self.path.as_vec()
+        })
     }
 }