Skip to content

Commit 8827b94

Browse files
committed
auto merge of #8978 : pnkfelix/rust/make-path-api-less-allocation-happy, r=huonw
A [dialogue](#8909 (diff)) on PR #8909 inspired me to make this change. r? anyone (It is possible that `std::path` itself will soon be replaced with a new implementation that kballard's working on, as mentioned in the dialogue linked above, but this revision is simple enough that I figured I'd offer it up.)
2 parents b161e09 + 7f834c5 commit 8827b94

File tree

10 files changed

+52
-48
lines changed

10 files changed

+52
-48
lines changed

src/compiletest/compiletest.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ pub fn make_test_name(config: &config, testfile: &Path) -> test::TestName {
307307
// Try to elide redundant long paths
308308
fn shorten(path: &Path) -> ~str {
309309
let filename = path.filename();
310-
let dir = path.pop().filename();
311-
fmt!("%s/%s", dir.unwrap_or_default(~""), filename.unwrap_or_default(~""))
310+
let p = path.pop();
311+
let dir = p.filename();
312+
fmt!("%s/%s", dir.unwrap_or_default(""), filename.unwrap_or_default(""))
312313
}
313314

314315
test::DynTestName(fmt!("[%s] %s",

src/compiletest/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ fn _arm_push_aux_shared_library(config: &config, testfile: &Path) {
880880
let dirs = os::list_dir_path(&Path(tstr));
881881
for file in dirs.iter() {
882882

883-
if (file.filetype() == Some(~".so")) {
883+
if (file.filetype() == Some(".so")) {
884884

885885
let copy_result = procsrv::run("", config.adb_path,
886886
[~"push", file.to_str(), config.adb_test_dir.clone()],

src/librust/rust.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ fn cmd_help(args: &[~str]) -> ValidUsage {
162162
fn cmd_test(args: &[~str]) -> ValidUsage {
163163
match args {
164164
[ref filename] => {
165-
let test_exec = Path(*filename).filestem().unwrap() + "test~";
165+
let p = Path(*filename);
166+
let test_exec = p.filestem().unwrap() + "test~";
166167
invoke("rustc", &[~"--test", filename.to_owned(),
167168
~"-o", test_exec.to_owned()], rustc::main_args);
168169
let exit_code = run::process_status(~"./" + test_exec, []);
@@ -175,7 +176,8 @@ fn cmd_test(args: &[~str]) -> ValidUsage {
175176
fn cmd_run(args: &[~str]) -> ValidUsage {
176177
match args {
177178
[ref filename, ..prog_args] => {
178-
let exec = Path(*filename).filestem().unwrap() + "~";
179+
let p = Path(*filename);
180+
let exec = p.filestem().unwrap() + "~";
179181
invoke("rustc", &[filename.to_owned(), ~"-o", exec.to_owned()],
180182
rustc::main_args);
181183
let exit_code = run::process_status(~"./"+exec, prog_args);

src/librustc/back/link.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,13 +952,13 @@ pub fn link_args(sess: Session,
952952
let cstore = sess.cstore;
953953
let r = cstore::get_used_crate_files(cstore);
954954
for cratepath in r.iter() {
955-
if cratepath.filetype() == Some(~".rlib") {
955+
if cratepath.filetype() == Some(".rlib") {
956956
args.push(cratepath.to_str());
957957
loop;
958958
}
959959
let dir = cratepath.dirname();
960960
if dir != ~"" { args.push(~"-L" + dir); }
961-
let libarg = unlib(sess.targ_cfg, cratepath.filestem().unwrap());
961+
let libarg = unlib(sess.targ_cfg, cratepath.filestem().unwrap().to_owned());
962962
args.push(~"-l" + libarg);
963963
}
964964

src/librustpkg/installed_packages.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ pub fn list_installed_packages(f: &fn(&PkgId) -> bool) -> bool {
1919
for p in workspaces.iter() {
2020
let binfiles = os::list_dir(&p.push("bin"));
2121
for exec in binfiles.iter() {
22-
let exec_path = Path(*exec).filestem();
22+
let p = Path(*exec);
23+
let exec_path = p.filestem();
2324
do exec_path.iter().advance |s| {
2425
f(&PkgId::new(*s))
2526
};
@@ -49,8 +50,8 @@ pub fn has_library(p: &Path) -> Option<~str> {
4950
let files = os::list_dir(p);
5051
for q in files.iter() {
5152
let as_path = Path(*q);
52-
if as_path.filetype() == Some(os::consts::DLL_SUFFIX.to_owned()) {
53-
let stuff : ~str = as_path.filestem().expect("has_library: weird path");
53+
if as_path.filetype() == Some(os::consts::DLL_SUFFIX) {
54+
let stuff : &str = as_path.filestem().expect("has_library: weird path");
5455
let mut stuff2 = stuff.split_str_iter(&"-");
5556
let stuff3: ~[&str] = stuff2.collect();
5657
// argh

src/librustpkg/package_id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl PkgId {
6868
if path.components.len() < 1 {
6969
return cond.raise((path, ~"0-length pkgid"));
7070
}
71-
let short_name = path.clone().filestem().expect(fmt!("Strange path! %s", s));
71+
let short_name = path.filestem().expect(fmt!("Strange path! %s", s));
7272

7373
let version = match given_version {
7474
Some(v) => v,
@@ -83,8 +83,8 @@ impl PkgId {
8383

8484
debug!("path = %s", path.to_str());
8585
PkgId {
86-
path: path,
87-
short_name: short_name,
86+
path: path.clone(),
87+
short_name: short_name.to_owned(),
8888
version: version
8989
}
9090
}

src/librustpkg/package_source.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl PkgSrc {
119119
return Some(local);
120120
}
121121

122-
if (self.id.path.clone()).components().len() < 2 {
122+
if self.id.path.components().len() < 2 {
123123
// If a non-URL, don't bother trying to fetch
124124
return None;
125125
}
@@ -157,7 +157,7 @@ impl PkgSrc {
157157

158158
/// True if the given path's stem is self's pkg ID's stem
159159
fn stem_matches(&self, p: &Path) -> bool {
160-
p.filestem().map_default(false, |p| { p == &self.id.short_name })
160+
p.filestem().map_default(false, |p| { p == &self.id.short_name.as_slice() })
161161
}
162162

163163
fn push_crate(cs: &mut ~[Crate], prefix: uint, p: &Path) {
@@ -182,10 +182,10 @@ impl PkgSrc {
182182
do os::walk_dir(&dir) |pth| {
183183
let maybe_known_crate_set = match pth.filename() {
184184
Some(filename) => match filename {
185-
~"lib.rs" => Some(&mut self.libs),
186-
~"main.rs" => Some(&mut self.mains),
187-
~"test.rs" => Some(&mut self.tests),
188-
~"bench.rs" => Some(&mut self.benchs),
185+
"lib.rs" => Some(&mut self.libs),
186+
"main.rs" => Some(&mut self.mains),
187+
"test.rs" => Some(&mut self.tests),
188+
"bench.rs" => Some(&mut self.benchs),
189189
_ => None
190190
},
191191
_ => None

src/librustpkg/path_util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ pub fn library_in_workspace(path: &Path, short_name: &str, where: Target,
234234
Some(j) => {
235235
debug!("Maybe %s equals %s", f_name.slice(0, j), lib_prefix);
236236
if f_name.slice(0, j) == lib_prefix {
237-
result_filename = Some(p_path);
237+
result_filename = Some(p_path.clone());
238238
}
239239
break;
240240
}
241241
None => break
242242
}
243243
}
244-
_ => { f_name = f_name.slice(0, i).to_owned(); }
244+
_ => { f_name = f_name.slice(0, i); }
245245
}
246246
}
247247
None => break

src/librustpkg/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ fn touch_source_file(workspace: &Path, pkgid: &PkgId) {
396396
let pkg_src_dir = workspace.push("src").push(pkgid.to_str());
397397
let contents = os::list_dir_path(&pkg_src_dir);
398398
for p in contents.iter() {
399-
if p.filetype() == Some(~".rs") {
399+
if p.filetype() == Some(".rs") {
400400
// should be able to do this w/o a process
401401
if run::process_output("touch", [p.to_str()]).status != 0 {
402402
let _ = cond.raise((pkg_src_dir.clone(), ~"Bad path"));
@@ -413,7 +413,7 @@ fn frob_source_file(workspace: &Path, pkgid: &PkgId) {
413413
let contents = os::list_dir_path(&pkg_src_dir);
414414
let mut maybe_p = None;
415415
for p in contents.iter() {
416-
if p.filetype() == Some(~".rs") {
416+
if p.filetype() == Some(".rs") {
417417
maybe_p = Some(p);
418418
break;
419419
}

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)