From b3c93b34f3fa81e9e2ade0cd05e0516faa7150ae Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Fri, 17 Jan 2014 23:07:53 -0800 Subject: [PATCH] Make WindowsPath::new("C:foo").root_path() return Some("C:") --- src/libstd/path/mod.rs | 2 +- src/libstd/path/windows.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 354cc10f022a6..dd245ec4f1a09 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -386,7 +386,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// Returns a Path that represents the filesystem root that `self` is rooted in. /// - /// If `self` is not absolute, or vol-relative in the case of Windows, this returns None. + /// If `self` is not absolute, or vol/cwd-relative in the case of Windows, this returns None. fn root_path(&self) -> Option; /// Pushes a path (as a byte vector or string) onto `self`. diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index a42fdabef8892..2d9d787d72de5 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -432,9 +432,12 @@ impl GenericPath for Path { } fn root_path(&self) -> Option { - if self.is_absolute() { + if self.prefix.is_some() { Some(Path::new(match self.prefix { - Some(VerbatimDiskPrefix)|Some(DiskPrefix) => { + Some(DiskPrefix) if self.is_absolute() => { + self.repr.slice_to(self.prefix_len()+1) + } + Some(VerbatimDiskPrefix) => { self.repr.slice_to(self.prefix_len()+1) } _ => self.repr.slice_to(self.prefix_len()) @@ -1683,7 +1686,7 @@ mod tests { fn test_root_path() { assert_eq!(Path::new("a\\b\\c").root_path(), None); assert_eq!(Path::new("\\a\\b\\c").root_path(), Some(Path::new("\\"))); - assert_eq!(Path::new("C:a").root_path(), None); + assert_eq!(Path::new("C:a").root_path(), Some(Path::new("C:"))); assert_eq!(Path::new("C:\\a").root_path(), Some(Path::new("C:\\"))); assert_eq!(Path::new("\\\\a\\b\\c").root_path(), Some(Path::new("\\\\a\\b"))); assert_eq!(Path::new("\\\\?\\a\\b").root_path(), Some(Path::new("\\\\?\\a")));