Skip to content

Commit b3c93b3

Browse files
committed
Make WindowsPath::new("C:foo").root_path() return Some("C:")
1 parent f4498c7 commit b3c93b3

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/libstd/path/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
386386

387387
/// Returns a Path that represents the filesystem root that `self` is rooted in.
388388
///
389-
/// If `self` is not absolute, or vol-relative in the case of Windows, this returns None.
389+
/// If `self` is not absolute, or vol/cwd-relative in the case of Windows, this returns None.
390390
fn root_path(&self) -> Option<Self>;
391391

392392
/// Pushes a path (as a byte vector or string) onto `self`.

src/libstd/path/windows.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,12 @@ impl GenericPath for Path {
432432
}
433433

434434
fn root_path(&self) -> Option<Path> {
435-
if self.is_absolute() {
435+
if self.prefix.is_some() {
436436
Some(Path::new(match self.prefix {
437-
Some(VerbatimDiskPrefix)|Some(DiskPrefix) => {
437+
Some(DiskPrefix) if self.is_absolute() => {
438+
self.repr.slice_to(self.prefix_len()+1)
439+
}
440+
Some(VerbatimDiskPrefix) => {
438441
self.repr.slice_to(self.prefix_len()+1)
439442
}
440443
_ => self.repr.slice_to(self.prefix_len())
@@ -1683,7 +1686,7 @@ mod tests {
16831686
fn test_root_path() {
16841687
assert_eq!(Path::new("a\\b\\c").root_path(), None);
16851688
assert_eq!(Path::new("\\a\\b\\c").root_path(), Some(Path::new("\\")));
1686-
assert_eq!(Path::new("C:a").root_path(), None);
1689+
assert_eq!(Path::new("C:a").root_path(), Some(Path::new("C:")));
16871690
assert_eq!(Path::new("C:\\a").root_path(), Some(Path::new("C:\\")));
16881691
assert_eq!(Path::new("\\\\a\\b\\c").root_path(), Some(Path::new("\\\\a\\b")));
16891692
assert_eq!(Path::new("\\\\?\\a\\b").root_path(), Some(Path::new("\\\\?\\a")));

0 commit comments

Comments
 (0)