Skip to content

Commit 0f3c87e

Browse files
committed
Revise path.rs API to not allocate ~str so much.
Note that I left dirname as returning ~str, because both of its implementations work by calling dir_path, which produces a new path, and thus we cannot borrow the result from &'a self passed to dirname (because the new path returned by dir_path will not live long enough to satisfy the lifetime 'a).
1 parent 64ff315 commit 0f3c87e

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/libstd/path.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use option::{None, Option, Some};
2828
use str::{OwnedStr, Str, StrSlice, StrVector};
2929
use to_str::ToStr;
3030
use ascii::{AsciiCast, AsciiStr};
31-
use vec::{OwnedVector, ImmutableVector, OwnedCopyableVector};
31+
use vec::{Vector, OwnedVector, ImmutableVector, OwnedCopyableVector};
3232

3333
#[cfg(windows)]
3434
pub use Path = self::WindowsPath;
@@ -65,17 +65,17 @@ pub trait GenericPath {
6565
fn dirname(&self) -> ~str;
6666
/// Returns the file component of `self`, as a string option.
6767
/// Returns None if `self` names a directory.
68-
fn filename(&self) -> Option<~str>;
68+
fn filename<'a>(&'a self) -> Option<&'a str>;
6969
/// Returns the stem of the file component of `self`, as a string option.
7070
/// The stem is the slice of a filename starting at 0 and ending just before
7171
/// the last '.' in the name.
7272
/// Returns None if `self` names a directory.
73-
fn filestem(&self) -> Option<~str>;
73+
fn filestem<'a>(&'a self) -> Option<&'a str>;
7474
/// Returns the type of the file component of `self`, as a string option.
7575
/// The file type is the slice of a filename starting just after the last
7676
/// '.' in the name and ending at the last index in the filename.
7777
/// Returns None if `self` names a directory.
78-
fn filetype(&self) -> Option<~str>;
78+
fn filetype<'a>(&'a self) -> Option<&'a str>;
7979

8080
/// Returns a new path consisting of `self` with the parent directory component replaced
8181
/// with the given string.
@@ -163,7 +163,7 @@ pub trait GenericPath {
163163
result
164164
}
165165

166-
fn components(self) -> ~[~str];
166+
fn components<'a>(&'a self) -> &'a [~str];
167167
}
168168

169169
#[cfg(target_os = "linux")]
@@ -600,31 +600,31 @@ impl GenericPath for PosixPath {
600600
}
601601
}
602602

603-
fn filename(&self) -> Option<~str> {
603+
fn filename<'a>(&'a self) -> Option<&'a str> {
604604
match self.components.len() {
605605
0 => None,
606-
n => Some(self.components[n - 1].clone()),
606+
n => Some(self.components[n - 1].as_slice()),
607607
}
608608
}
609609

610-
fn filestem(&self) -> Option<~str> {
610+
fn filestem<'a>(&'a self) -> Option<&'a str> {
611611
match self.filename() {
612612
None => None,
613613
Some(ref f) => {
614614
match f.rfind('.') {
615-
Some(p) => Some(f.slice_to(p).to_owned()),
616-
None => Some((*f).clone()),
615+
Some(p) => Some(f.slice_to(p)),
616+
None => Some((*f)),
617617
}
618618
}
619619
}
620620
}
621621

622-
fn filetype(&self) -> Option<~str> {
622+
fn filetype<'a>(&'a self) -> Option<&'a str> {
623623
match self.filename() {
624624
None => None,
625625
Some(ref f) => {
626626
match f.rfind('.') {
627-
Some(p) if p < f.len() => Some(f.slice_from(p).to_owned()),
627+
Some(p) if p < f.len() => Some(f.slice_from(p)),
628628
_ => None,
629629
}
630630
}
@@ -670,7 +670,7 @@ impl GenericPath for PosixPath {
670670
fn file_path(&self) -> PosixPath {
671671
let cs = match self.filename() {
672672
None => ~[],
673-
Some(ref f) => ~[(*f).clone()]
673+
Some(ref f) => ~[(*f).to_owned()]
674674
};
675675
PosixPath {
676676
is_absolute: false,
@@ -756,7 +756,7 @@ impl GenericPath for PosixPath {
756756
self.is_ancestor_of(&other.pop()))
757757
}
758758

759-
fn components(self) -> ~[~str] { self.components }
759+
fn components<'a>(&'a self) -> &'a [~str] { self.components.as_slice() }
760760
}
761761

762762

@@ -842,31 +842,31 @@ impl GenericPath for WindowsPath {
842842
}
843843
}
844844

845-
fn filename(&self) -> Option<~str> {
845+
fn filename<'a>(&'a self) -> Option<&'a str> {
846846
match self.components.len() {
847847
0 => None,
848-
n => Some(self.components[n - 1].clone()),
848+
n => Some(self.components[n - 1].as_slice()),
849849
}
850850
}
851851

852-
fn filestem(&self) -> Option<~str> {
852+
fn filestem<'a>(&'a self) -> Option<&'a str> {
853853
match self.filename() {
854854
None => None,
855855
Some(ref f) => {
856856
match f.rfind('.') {
857-
Some(p) => Some(f.slice_to(p).to_owned()),
858-
None => Some((*f).clone()),
857+
Some(p) => Some(f.slice_to(p)),
858+
None => Some((*f)),
859859
}
860860
}
861861
}
862862
}
863863

864-
fn filetype(&self) -> Option<~str> {
864+
fn filetype<'a>(&'a self) -> Option<&'a str> {
865865
match self.filename() {
866866
None => None,
867867
Some(ref f) => {
868868
match f.rfind('.') {
869-
Some(p) if p < f.len() => Some(f.slice_from(p).to_owned()),
869+
Some(p) if p < f.len() => Some(f.slice_from(p)),
870870
_ => None,
871871
}
872872
}
@@ -916,7 +916,7 @@ impl GenericPath for WindowsPath {
916916
is_absolute: false,
917917
components: match self.filename() {
918918
None => ~[],
919-
Some(ref f) => ~[(*f).clone()],
919+
Some(ref f) => ~[(*f).to_owned()],
920920
}
921921
}
922922
}
@@ -1049,7 +1049,7 @@ impl GenericPath for WindowsPath {
10491049
self.is_ancestor_of(&other.pop()))
10501050
}
10511051

1052-
fn components(self) -> ~[~str] { self.components }
1052+
fn components<'a>(&'a self) -> &'a [~str] { self.components.as_slice() }
10531053
}
10541054

10551055
pub fn normalize(components: &[~str]) -> ~[~str] {
@@ -1143,10 +1143,10 @@ mod tests {
11431143
#[test]
11441144
fn test_filetype_foo_bar() {
11451145
let wp = PosixPath("foo.bar");
1146-
assert_eq!(wp.filetype(), Some(~".bar"));
1146+
assert_eq!(wp.filetype(), Some(".bar"));
11471147
11481148
let wp = WindowsPath("foo.bar");
1149-
assert_eq!(wp.filetype(), Some(~".bar"));
1149+
assert_eq!(wp.filetype(), Some(".bar"));
11501150
}
11511151
11521152
#[test]

0 commit comments

Comments
 (0)