diff --git a/src/librustc_back/tempdir.rs b/src/librustc_back/tempdir.rs index 077a0feebd4..04739c7418c 100644 --- a/src/librustc_back/tempdir.rs +++ b/src/librustc_back/tempdir.rs @@ -38,8 +38,12 @@ impl TempDir { #[allow(deprecated)] // rand usage pub fn new_in>(tmpdir: P, prefix: &str) -> io::Result { + Self::_new_in(tmpdir.as_ref(), prefix) + } + + fn _new_in(tmpdir: &Path, prefix: &str) -> io::Result { let storage; - let mut tmpdir = tmpdir.as_ref(); + let mut tmpdir = tmpdir; if !tmpdir.is_absolute() { let cur_dir = try!(env::current_dir()); storage = cur_dir.join(tmpdir); diff --git a/src/libstd/env.rs b/src/libstd/env.rs index d80a222d8d2..3447d1683a8 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -174,6 +174,10 @@ impl Iterator for VarsOs { /// ``` #[stable(feature = "env", since = "1.0.0")] pub fn var>(key: K) -> Result { + _var(key.as_ref()) +} + +fn _var(key: &OsStr) -> Result { match var_os(key) { Some(s) => s.into_string().map_err(VarError::NotUnicode), None => Err(VarError::NotPresent) @@ -196,8 +200,12 @@ pub fn var>(key: K) -> Result { /// ``` #[stable(feature = "env", since = "1.0.0")] pub fn var_os>(key: K) -> Option { + _var_os(key.as_ref()) +} + +fn _var_os(key: &OsStr) -> Option { let _g = ENV_LOCK.lock(); - os_imp::getenv(key.as_ref()) + os_imp::getenv(key) } /// Possible errors from the `env::var` method. @@ -263,8 +271,12 @@ impl Error for VarError { /// ``` #[stable(feature = "env", since = "1.0.0")] pub fn set_var, V: AsRef>(k: K, v: V) { + _set_var(k.as_ref(), v.as_ref()) +} + +fn _set_var(k: &OsStr, v: &OsStr) { let _g = ENV_LOCK.lock(); - os_imp::setenv(k.as_ref(), v.as_ref()) + os_imp::setenv(k, v) } /// Removes an environment variable from the environment of the currently running process. @@ -294,8 +306,12 @@ pub fn set_var, V: AsRef>(k: K, v: V) { /// ``` #[stable(feature = "env", since = "1.0.0")] pub fn remove_var>(k: K) { + _remove_var(k.as_ref()) +} + +fn _remove_var(k: &OsStr) { let _g = ENV_LOCK.lock(); - os_imp::unsetenv(k.as_ref()) + os_imp::unsetenv(k) } /// An iterator over `Path` instances for parsing an environment variable diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 3a1853a7a6c..7c9c2066ae9 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -181,7 +181,10 @@ impl CString { /// the position of the nul byte. #[stable(feature = "rust1", since = "1.0.0")] pub fn new>>(t: T) -> Result { - let bytes = t.into(); + Self::_new(t.into()) + } + + fn _new(bytes: Vec) -> Result { match bytes.iter().position(|x| *x == 0) { Some(i) => Err(NulError(i, bytes)), None => Ok(unsafe { CString::from_vec_unchecked(bytes) }), diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 751c76b9960..7409d9b45d2 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -73,18 +73,18 @@ impl OsString { /// convert; non UTF-8 data will produce `None`. #[unstable(feature = "convert", reason = "recently added", issue = "27704")] pub fn from_bytes(bytes: B) -> Option where B: Into> { - #[cfg(unix)] - fn from_bytes_inner(vec: Vec) -> Option { - use os::unix::ffi::OsStringExt; - Some(OsString::from_vec(vec)) - } + Self::_from_bytes(bytes.into()) + } - #[cfg(windows)] - fn from_bytes_inner(vec: Vec) -> Option { - String::from_utf8(vec).ok().map(OsString::from) - } + #[cfg(unix)] + fn _from_bytes(vec: Vec) -> Option { + use os::unix::ffi::OsStringExt; + Some(OsString::from_vec(vec)) + } - from_bytes_inner(bytes.into()) + #[cfg(windows)] + fn _from_bytes(vec: Vec) -> Option { + String::from_utf8(vec).ok().map(OsString::from) } /// Converts to an `OsStr` slice. diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 0014391670a..a3d509ba0f1 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -184,7 +184,7 @@ impl File { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn open>(path: P) -> io::Result { - OpenOptions::new().read(true).open(path) + OpenOptions::new().read(true).open(path.as_ref()) } /// Opens a file in write-only mode. @@ -206,7 +206,7 @@ impl File { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn create>(path: P) -> io::Result { - OpenOptions::new().write(true).create(true).truncate(true).open(path) + OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref()) } /// Attempts to sync all OS-internal metadata to disk. @@ -494,7 +494,10 @@ impl OpenOptions { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn open>(&self, path: P) -> io::Result { - let path = path.as_ref(); + self._open(path.as_ref()) + } + + fn _open(&self, path: &Path) -> io::Result { let inner = try!(fs_imp::File::open(path, &self.0)); Ok(File { inner: inner }) } @@ -1048,7 +1051,10 @@ pub fn remove_dir>(path: P) -> io::Result<()> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn remove_dir_all>(path: P) -> io::Result<()> { - let path = path.as_ref(); + _remove_dir_all(path.as_ref()) +} + +fn _remove_dir_all(path: &Path) -> io::Result<()> { for child in try!(read_dir(path)) { let child = try!(child).path(); let stat = try!(symlink_metadata(&*child)); @@ -1113,6 +1119,10 @@ pub fn read_dir>(path: P) -> io::Result { as symlinks differently", issue = "27707")] pub fn walk_dir>(path: P) -> io::Result { + _walk_dir(path.as_ref()) +} + +fn _walk_dir(path: &Path) -> io::Result { let start = try!(read_dir(path)); Ok(WalkDir { cur: Some(start), stack: Vec::new() }) } @@ -1272,7 +1282,10 @@ impl DirBuilder { /// Create the specified directory with the options configured in this /// builder. pub fn create>(&self, path: P) -> io::Result<()> { - let path = path.as_ref(); + self._create(path.as_ref()) + } + + fn _create(&self, path: &Path) -> io::Result<()> { if self.recursive { self.create_dir_all(path) } else { diff --git a/src/libstd/io/error.rs b/src/libstd/io/error.rs index 576d9b92156..773f9ac6a12 100644 --- a/src/libstd/io/error.rs +++ b/src/libstd/io/error.rs @@ -191,10 +191,14 @@ impl Error { pub fn new(kind: ErrorKind, error: E) -> Error where E: Into> { + Self::_new(kind, error.into()) + } + + fn _new(kind: ErrorKind, error: Box) -> Error { Error { repr: Repr::Custom(Box::new(Custom { kind: kind, - error: error.into(), + error: error, })) } } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 66893ffd330..8eb5d1f2726 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -965,8 +965,10 @@ impl PathBuf { /// * if `path` has a prefix but no root, it replaces `self`. #[stable(feature = "rust1", since = "1.0.0")] pub fn push>(&mut self, path: P) { - let path = path.as_ref(); + self._push(path.as_ref()) + } + fn _push(&mut self, path: &Path) { // in general, a separator is needed if the rightmost byte is not a separator let mut need_sep = self.as_mut_vec().last().map(|c| !is_sep_byte(*c)).unwrap_or(false); @@ -1033,11 +1035,15 @@ impl PathBuf { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn set_file_name>(&mut self, file_name: S) { + self._set_file_name(file_name.as_ref()) + } + + fn _set_file_name(&mut self, file_name: &OsStr) { if self.file_name().is_some() { let popped = self.pop(); debug_assert!(popped); } - self.push(file_name.as_ref()); + self.push(file_name); } /// Updates `self.extension()` to `extension`. @@ -1048,6 +1054,10 @@ impl PathBuf { /// is added; otherwise it is replaced. #[stable(feature = "rust1", since = "1.0.0")] pub fn set_extension>(&mut self, extension: S) -> bool { + self._set_extension(extension.as_ref()) + } + + fn _set_extension(&mut self, extension: &OsStr) -> bool { if self.file_name().is_none() { return false; } let mut stem = match self.file_stem() { @@ -1055,7 +1065,6 @@ impl PathBuf { None => OsString::new(), }; - let extension = extension.as_ref(); if !os_str_as_u8_slice(extension).is_empty() { stem.push("."); stem.push(extension); @@ -1106,7 +1115,7 @@ impl> iter::FromIterator

for PathBuf { impl> iter::Extend

for PathBuf { fn extend>(&mut self, iter: I) { for p in iter { - self.push(p) + self.push(p.as_ref()) } } } @@ -1452,7 +1461,11 @@ impl Path { issue = "23284")] pub fn relative_from<'a, P: ?Sized + AsRef>(&'a self, base: &'a P) -> Option<&Path> { - iter_after(self.components(), base.as_ref().components()).map(|c| c.as_path()) + self._relative_from(base.as_ref()) + } + + fn _relative_from<'a>(&'a self, base: &'a Path) -> Option<&'a Path> { + iter_after(self.components(), base.components()).map(|c| c.as_path()) } /// Determines whether `base` is a prefix of `self`. @@ -1472,7 +1485,11 @@ impl Path { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn starts_with>(&self, base: P) -> bool { - iter_after(self.components(), base.as_ref().components()).is_some() + self._starts_with(base.as_ref()) + } + + fn _starts_with(&self, base: &Path) -> bool { + iter_after(self.components(), base.components()).is_some() } /// Determines whether `child` is a suffix of `self`. @@ -1490,7 +1507,11 @@ impl Path { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn ends_with>(&self, child: P) -> bool { - iter_after(self.components().rev(), child.as_ref().components().rev()).is_some() + self._ends_with(child.as_ref()) + } + + fn _ends_with(&self, child: &Path) -> bool { + iter_after(self.components().rev(), child.components().rev()).is_some() } /// Extracts the stem (non-extension) portion of `self.file_name()`. @@ -1552,6 +1573,10 @@ impl Path { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn join>(&self, path: P) -> PathBuf { + self._join(path.as_ref()) + } + + fn _join(&self, path: &Path) -> PathBuf { let mut buf = self.to_path_buf(); buf.push(path); buf @@ -1571,6 +1596,10 @@ impl Path { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn with_file_name>(&self, file_name: S) -> PathBuf { + self._with_file_name(file_name.as_ref()) + } + + fn _with_file_name(&self, file_name: &OsStr) -> PathBuf { let mut buf = self.to_path_buf(); buf.set_file_name(file_name); buf @@ -1590,6 +1619,10 @@ impl Path { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn with_extension>(&self, extension: S) -> PathBuf { + self._with_extension(extension.as_ref()) + } + + fn _with_extension(&self, extension: &OsStr) -> PathBuf { let mut buf = self.to_path_buf(); buf.set_extension(extension); buf