Skip to content

Commit c8d8f6c

Browse files
committed
Refactored make_absolute into functionality on the Path
1 parent 1171a21 commit c8d8f6c

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/libcore/os.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -565,17 +565,13 @@ pub fn path_exists(p: &Path) -> bool {
565565
*
566566
* If the given path is relative, return it prepended with the current working
567567
* directory. If the given path is already an absolute path, return it
568-
* as is.
568+
* as is. This is a shortcut for calling os::getcwd().unsafe_join(p)
569569
*/
570570
// NB: this is here rather than in path because it is a form of environment
571571
// querying; what it does depends on the process working directory, not just
572572
// the input paths.
573573
pub fn make_absolute(p: &Path) -> Path {
574-
if p.is_absolute {
575-
copy *p
576-
} else {
577-
getcwd().push_many(p.components)
578-
}
574+
getcwd().unsafe_join(p)
579575
}
580576

581577

src/libcore/path.rs

+30
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub trait GenericPath {
6464
pure fn push_many((&[~str])) -> Self;
6565
pure fn pop() -> Self;
6666

67+
pure fn unsafe_join((&Self)) -> Self;
68+
6769
pure fn normalize() -> Self;
6870
}
6971

@@ -485,6 +487,15 @@ impl GenericPath for PosixPath {
485487
self.push_many(other.components)
486488
}
487489

490+
pure fn unsafe_join(other: &PosixPath) -> PosixPath {
491+
if other.is_absolute {
492+
PosixPath { is_absolute: true,
493+
components: copy other.components }
494+
} else {
495+
self.push_rel(other)
496+
}
497+
}
498+
488499
pure fn push_many(cs: &[~str]) -> PosixPath {
489500
let mut v = copy self.components;
490501
for cs.each |e| {
@@ -685,6 +696,25 @@ impl GenericPath for WindowsPath {
685696
self.push_many(other.components)
686697
}
687698

699+
pure fn unsafe_join(other: &WindowsPath) -> WindowsPath {
700+
if !other.is_absolute {
701+
self.push_rel(other)
702+
} else {
703+
WindowsPath {
704+
host: match other.host {
705+
None => copy self.host,
706+
Some(copy x) => Some(x)
707+
},
708+
device: match other.device {
709+
None => copy self.device,
710+
Some(copy x) => Some(x)
711+
},
712+
is_absolute: true,
713+
components: copy other.components
714+
}
715+
}
716+
}
717+
688718
pure fn push_many(cs: &[~str]) -> WindowsPath {
689719
let mut v = copy self.components;
690720
for cs.each |e| {

0 commit comments

Comments
 (0)