@@ -28,7 +28,7 @@ use option::{None, Option, Some};
28
28
use str:: { OwnedStr , Str , StrSlice , StrVector } ;
29
29
use to_str:: ToStr ;
30
30
use ascii:: { AsciiCast , AsciiStr } ;
31
- use vec:: { OwnedVector , ImmutableVector , OwnedCopyableVector } ;
31
+ use vec:: { Vector , OwnedVector , ImmutableVector , OwnedCopyableVector } ;
32
32
33
33
#[ cfg( windows) ]
34
34
pub use Path = self :: WindowsPath ;
@@ -65,17 +65,17 @@ pub trait GenericPath {
65
65
fn dirname ( & self ) -> ~str ;
66
66
/// Returns the file component of `self`, as a string option.
67
67
/// Returns None if `self` names a directory.
68
- fn filename ( & self ) -> Option < ~ str > ;
68
+ fn filename < ' a > ( & ' a self ) -> Option < & ' a str > ;
69
69
/// Returns the stem of the file component of `self`, as a string option.
70
70
/// The stem is the slice of a filename starting at 0 and ending just before
71
71
/// the last '.' in the name.
72
72
/// Returns None if `self` names a directory.
73
- fn filestem ( & self ) -> Option < ~ str > ;
73
+ fn filestem < ' a > ( & ' a self ) -> Option < & ' a str > ;
74
74
/// Returns the type of the file component of `self`, as a string option.
75
75
/// The file type is the slice of a filename starting just after the last
76
76
/// '.' in the name and ending at the last index in the filename.
77
77
/// Returns None if `self` names a directory.
78
- fn filetype ( & self ) -> Option < ~ str > ;
78
+ fn filetype < ' a > ( & ' a self ) -> Option < & ' a str > ;
79
79
80
80
/// Returns a new path consisting of `self` with the parent directory component replaced
81
81
/// with the given string.
@@ -163,7 +163,7 @@ pub trait GenericPath {
163
163
result
164
164
}
165
165
166
- fn components ( self ) -> ~ [ ~str ] ;
166
+ fn components < ' a > ( & ' a self ) -> & ' a [ ~str ] ;
167
167
}
168
168
169
169
#[ cfg( target_os = "linux" ) ]
@@ -600,31 +600,31 @@ impl GenericPath for PosixPath {
600
600
}
601
601
}
602
602
603
- fn filename ( & self ) -> Option < ~ str > {
603
+ fn filename < ' a > ( & ' a self ) -> Option < & ' a str > {
604
604
match self . components . len ( ) {
605
605
0 => None ,
606
- n => Some ( self . components [ n - 1 ] . clone ( ) ) ,
606
+ n => Some ( self . components [ n - 1 ] . as_slice ( ) ) ,
607
607
}
608
608
}
609
609
610
- fn filestem ( & self ) -> Option < ~ str > {
610
+ fn filestem < ' a > ( & ' a self ) -> Option < & ' a str > {
611
611
match self . filename ( ) {
612
612
None => None ,
613
613
Some ( ref f) => {
614
614
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) ) ,
617
617
}
618
618
}
619
619
}
620
620
}
621
621
622
- fn filetype ( & self ) -> Option < ~ str > {
622
+ fn filetype < ' a > ( & ' a self ) -> Option < & ' a str > {
623
623
match self . filename ( ) {
624
624
None => None ,
625
625
Some ( ref f) => {
626
626
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) ) ,
628
628
_ => None ,
629
629
}
630
630
}
@@ -670,7 +670,7 @@ impl GenericPath for PosixPath {
670
670
fn file_path ( & self ) -> PosixPath {
671
671
let cs = match self . filename ( ) {
672
672
None => ~[ ] ,
673
- Some ( ref f) => ~[ ( * f) . clone ( ) ]
673
+ Some ( ref f) => ~[ ( * f) . to_owned ( ) ]
674
674
} ;
675
675
PosixPath {
676
676
is_absolute : false ,
@@ -756,7 +756,7 @@ impl GenericPath for PosixPath {
756
756
self . is_ancestor_of ( & other. pop ( ) ) )
757
757
}
758
758
759
- fn components ( self ) -> ~ [ ~str ] { self . components }
759
+ fn components < ' a > ( & ' a self ) -> & ' a [ ~str ] { self . components . as_slice ( ) }
760
760
}
761
761
762
762
@@ -842,31 +842,31 @@ impl GenericPath for WindowsPath {
842
842
}
843
843
}
844
844
845
- fn filename ( & self ) -> Option < ~ str > {
845
+ fn filename < ' a > ( & ' a self ) -> Option < & ' a str > {
846
846
match self . components . len ( ) {
847
847
0 => None ,
848
- n => Some ( self . components [ n - 1 ] . clone ( ) ) ,
848
+ n => Some ( self . components [ n - 1 ] . as_slice ( ) ) ,
849
849
}
850
850
}
851
851
852
- fn filestem ( & self ) -> Option < ~ str > {
852
+ fn filestem < ' a > ( & ' a self ) -> Option < & ' a str > {
853
853
match self . filename ( ) {
854
854
None => None ,
855
855
Some ( ref f) => {
856
856
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) ) ,
859
859
}
860
860
}
861
861
}
862
862
}
863
863
864
- fn filetype ( & self ) -> Option < ~ str > {
864
+ fn filetype < ' a > ( & ' a self ) -> Option < & ' a str > {
865
865
match self . filename ( ) {
866
866
None => None ,
867
867
Some ( ref f) => {
868
868
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) ) ,
870
870
_ => None ,
871
871
}
872
872
}
@@ -916,7 +916,7 @@ impl GenericPath for WindowsPath {
916
916
is_absolute : false ,
917
917
components : match self . filename ( ) {
918
918
None => ~[ ] ,
919
- Some ( ref f) => ~[ ( * f) . clone ( ) ] ,
919
+ Some ( ref f) => ~[ ( * f) . to_owned ( ) ] ,
920
920
}
921
921
}
922
922
}
@@ -1049,7 +1049,7 @@ impl GenericPath for WindowsPath {
1049
1049
self . is_ancestor_of ( & other. pop ( ) ) )
1050
1050
}
1051
1051
1052
- fn components ( self ) -> ~ [ ~str ] { self . components }
1052
+ fn components < ' a > ( & ' a self ) -> & ' a [ ~str ] { self . components . as_slice ( ) }
1053
1053
}
1054
1054
1055
1055
pub fn normalize( components : & [ ~str ] ) -> ~[ ~str ] {
@@ -1143,10 +1143,10 @@ mod tests {
1143
1143
#[test]
1144
1144
fn test_filetype_foo_bar() {
1145
1145
let wp = PosixPath(" foo. bar");
1146
- assert_eq!(wp.filetype(), Some(~ " . bar"));
1146
+ assert_eq!(wp.filetype(), Some(" . bar"));
1147
1147
1148
1148
let wp = WindowsPath(" foo. bar");
1149
- assert_eq!(wp.filetype(), Some(~ " . bar"));
1149
+ assert_eq!(wp.filetype(), Some(" . bar"));
1150
1150
}
1151
1151
1152
1152
#[test]
0 commit comments