From 467bea04fa1d5fd894d64b2b2901d94260301631 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 10 Sep 2014 22:26:41 -0700 Subject: librustc: Forbid inherent implementations that aren't adjacent to the type they provide an implementation for. This breaks code like: mod foo { struct Foo { ... } } impl foo::Foo { ... } Change this code to: mod foo { struct Foo { ... } impl Foo { ... } } Additionally, if you used the I/O path extension methods `stat`, `lstat`, `exists`, `is_file`, or `is_dir`, note that these methods have been moved to the the `std::io::fs::PathExtensions` trait. This breaks code like: fn is_it_there() -> bool { Path::new("/foo/bar/baz").exists() } Change this code to: use std::io::fs::PathExtensions; fn is_it_there() -> bool { Path::new("/foo/bar/baz").exists() } Closes #17059. RFC #155. [breaking-change] --- src/libstd/io/fs.rs | 39 +++++++++++++++++++++++++-------------- src/libstd/io/mod.rs | 1 + src/libstd/path/mod.rs | 2 ++ 3 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index 3d6bb96063e..b7b7d1fb93d 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -31,6 +31,7 @@ particular bits of it, etc. ```rust # #![allow(unused_must_use)] +use std::io::fs::PathExtensions; use std::io::{File, fs}; let path = Path::new("foo.txt"); @@ -622,8 +623,9 @@ pub fn rmdir(path: &Path) -> IoResult<()> { /// # Example /// /// ```rust -/// use std::io; +/// use std::io::fs::PathExtensions; /// use std::io::fs; +/// use std::io; /// /// // one possible implementation of fs::walk_dir only visiting files /// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> { @@ -868,13 +870,14 @@ impl Seek for File { } } -impl path::Path { +/// Utility methods for paths. +pub trait PathExtensions { /// Get information on the file, directory, etc at this path. /// /// Consult the `fs::stat` documentation for more info. /// /// This call preserves identical runtime/error semantics with `file::stat`. - pub fn stat(&self) -> IoResult { stat(self) } + fn stat(&self) -> IoResult; /// Get information on the file, directory, etc at this path, not following /// symlinks. @@ -882,31 +885,39 @@ impl path::Path { /// Consult the `fs::lstat` documentation for more info. /// /// This call preserves identical runtime/error semantics with `file::lstat`. - pub fn lstat(&self) -> IoResult { lstat(self) } + fn lstat(&self) -> IoResult; /// Boolean value indicator whether the underlying file exists on the local /// filesystem. Returns false in exactly the cases where `fs::stat` fails. - pub fn exists(&self) -> bool { - self.stat().is_ok() - } + fn exists(&self) -> bool; /// Whether the underlying implementation (be it a file path, or something /// else) points at a "regular file" on the FS. Will return false for paths /// to non-existent locations or directories or other non-regular files /// (named pipes, etc). Follows links when making this determination. - pub fn is_file(&self) -> bool { - match self.stat() { - Ok(s) => s.kind == io::TypeFile, - Err(..) => false - } - } + fn is_file(&self) -> bool; /// Whether the underlying implementation (be it a file path, or something /// else) is pointing at a directory in the underlying FS. Will return /// false for paths to non-existent locations or if the item is not a /// directory (eg files, named pipes, etc). Follows links when making this /// determination. - pub fn is_dir(&self) -> bool { + fn is_dir(&self) -> bool; +} + +impl PathExtensions for path::Path { + fn stat(&self) -> IoResult { stat(self) } + fn lstat(&self) -> IoResult { lstat(self) } + fn exists(&self) -> bool { + self.stat().is_ok() + } + fn is_file(&self) -> bool { + match self.stat() { + Ok(s) => s.kind == io::TypeFile, + Err(..) => false + } + } + fn is_dir(&self) -> bool { match self.stat() { Ok(s) => s.kind == io::TypeDirectory, Err(..) => false diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 0d0c9e933b0..7a2bcab8706 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1721,6 +1721,7 @@ pub enum FileType { /// # Example /// /// ``` +/// # use std::io::fs::PathExtensions; /// # fn main() {} /// # fn foo() { /// let info = match Path::new("foo.txt").stat() { diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 5a5068f4d01..d84848545bd 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -53,6 +53,8 @@ actually operates on the path; it is only intended for display. ## Example ```rust +use std::io::fs::PathExtensions; + let mut path = Path::new("/tmp/path"); println!("path: {}", path.display()); path.set_filename("foo"); -- cgit 1.4.1-3-g733a5