diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-23 15:09:05 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-23 15:09:05 -0700 |
| commit | fd13400627108fbf3998545f782ed44422e344bf (patch) | |
| tree | 209af0ca5850ae4c2add0da4b278fa908032be63 /src/libstd | |
| parent | c77af69a3793bc0c3c49b05ceffb15dccf5ed4d0 (diff) | |
| parent | 8389253df0431e58bfe0a8e0e3949d58ebe7400f (diff) | |
| download | rust-fd13400627108fbf3998545f782ed44422e344bf.tar.gz rust-fd13400627108fbf3998545f782ed44422e344bf.zip | |
rollup merge of #23538: aturon/conversion
Conflicts: src/librustc_back/rpath.rs
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/env.rs | 6 | ||||
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 11 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 77 | ||||
| -rw-r--r-- | src/libstd/fs/mod.rs | 70 | ||||
| -rw-r--r-- | src/libstd/fs/tempdir.rs | 7 | ||||
| -rw-r--r-- | src/libstd/lib.rs | 3 | ||||
| -rw-r--r-- | src/libstd/net/ip.rs | 1 | ||||
| -rw-r--r-- | src/libstd/os.rs | 5 | ||||
| -rw-r--r-- | src/libstd/path.rs | 200 | ||||
| -rw-r--r-- | src/libstd/prelude/v1.rs | 4 | ||||
| -rw-r--r-- | src/libstd/process.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/unix/fs2.rs | 3 | ||||
| -rw-r--r-- | src/libstd/sys/unix/os.rs | 8 |
13 files changed, 314 insertions, 87 deletions
diff --git a/src/libstd/env.rs b/src/libstd/env.rs index 24882c7f7ab..9d6933ce9c8 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -23,7 +23,7 @@ use error::Error; use ffi::{OsString, AsOsStr}; use fmt; use io; -use path::{AsPath, PathBuf}; +use path::{self, Path, PathBuf}; use sync::atomic::{AtomicIsize, ATOMIC_ISIZE_INIT, Ordering}; use sync::{StaticMutex, MUTEX_INIT}; use sys::os as os_imp; @@ -67,8 +67,8 @@ pub fn current_dir() -> io::Result<PathBuf> { /// println!("Successfully changed working directory to {}!", root.display()); /// ``` #[stable(feature = "env", since = "1.0.0")] -pub fn set_current_dir<P: AsPath + ?Sized>(p: &P) -> io::Result<()> { - os_imp::chdir(p.as_path()) +pub fn set_current_dir<P: AsRef<Path> + ?Sized>(p: &P) -> io::Result<()> { + os_imp::chdir(p.as_ref()) } static ENV_LOCK: StaticMutex = MUTEX_INIT; diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index fc4f03ff3a5..1760445a0fc 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -10,6 +10,7 @@ #![unstable(feature = "std_misc")] +use convert::Into; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use error::{Error, FromError}; use fmt; @@ -130,6 +131,8 @@ pub struct NulError(usize, Vec<u8>); /// A conversion trait used by the constructor of `CString` for types that can /// be converted to a vector of bytes. +#[deprecated(since = "1.0.0", reason = "use std::convert::Into<Vec<u8>> instead")] +#[unstable(feature = "std_misc")] pub trait IntoBytes { /// Consumes this container, returning a vector of bytes. fn into_bytes(self) -> Vec<u8>; @@ -163,8 +166,8 @@ impl CString { /// internal 0 byte. The error returned will contain the bytes as well as /// the position of the nul byte. #[stable(feature = "rust1", since = "1.0.0")] - pub fn new<T: IntoBytes>(t: T) -> Result<CString, NulError> { - let bytes = t.into_bytes(); + pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> { + let bytes = t.into(); match bytes.iter().position(|x| *x == 0) { Some(i) => Err(NulError(i, bytes)), None => Ok(unsafe { CString::from_vec_unchecked(bytes) }), @@ -433,15 +436,19 @@ pub unsafe fn c_str_to_bytes_with_nul<'a>(raw: &'a *const libc::c_char) slice::from_raw_parts(*(raw as *const _ as *const *const u8), len as usize) } +#[allow(deprecated)] impl<'a> IntoBytes for &'a str { fn into_bytes(self) -> Vec<u8> { self.as_bytes().to_vec() } } +#[allow(deprecated)] impl<'a> IntoBytes for &'a [u8] { fn into_bytes(self) -> Vec<u8> { self.to_vec() } } +#[allow(deprecated)] impl IntoBytes for String { fn into_bytes(self) -> Vec<u8> { self.into_bytes() } } +#[allow(deprecated)] impl IntoBytes for Vec<u8> { fn into_bytes(self) -> Vec<u8> { self } } diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index feacbf1e98b..4d411046632 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -63,16 +63,18 @@ pub struct OsStr { impl OsString { /// Constructs an `OsString` at no cost by consuming a `String`. #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "use `from` instead")] pub fn from_string(s: String) -> OsString { - OsString { inner: Buf::from_string(s) } + OsString::from(s) } /// Constructs an `OsString` by copying from a `&str` slice. /// /// Equivalent to: `OsString::from_string(String::from_str(s))`. #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "use `from` instead")] pub fn from_str(s: &str) -> OsString { - OsString { inner: Buf::from_str(s) } + OsString::from(s) } /// Constructs a new empty `OsString`. @@ -98,8 +100,36 @@ impl OsString { /// Extend the string with the given `&OsStr` slice. #[stable(feature = "rust1", since = "1.0.0")] - pub fn push<T: AsOsStr + ?Sized>(&mut self, s: &T) { - self.inner.push_slice(&s.as_os_str().inner) + pub fn push<T: AsRef<OsStr>>(&mut self, s: T) { + self.inner.push_slice(&s.as_ref().inner) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl From<String> for OsString { + fn from(s: String) -> OsString { + OsString { inner: Buf::from_string(s) } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a String> for OsString { + fn from(s: &'a String) -> OsString { + OsString { inner: Buf::from_str(s) } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a str> for OsString { + fn from(s: &'a str) -> OsString { + OsString { inner: Buf::from_str(s) } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a OsStr> for OsString { + fn from(s: &'a OsStr) -> OsString { + OsString { inner: s.inner.to_owned() } } } @@ -316,37 +346,76 @@ impl ToOwned for OsStr { } #[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl<'a, T: AsOsStr + ?Sized> AsOsStr for &'a T { fn as_os_str(&self) -> &OsStr { (*self).as_os_str() } } +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for OsStr { fn as_os_str(&self) -> &OsStr { self } } +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for OsString { fn as_os_str(&self) -> &OsStr { &self[..] } } +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for str { fn as_os_str(&self) -> &OsStr { OsStr::from_str(self) } } +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for String { fn as_os_str(&self) -> &OsStr { OsStr::from_str(&self[..]) } } +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<OsStr> for OsStr { + fn as_ref(&self) -> &OsStr { + self + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<OsStr> for OsString { + fn as_ref(&self) -> &OsStr { + self + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<OsStr> for str { + fn as_ref(&self) -> &OsStr { + OsStr::from_str(self) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<OsStr> for String { + fn as_ref(&self) -> &OsStr { + OsStr::from_str(&self[..]) + } +} + #[allow(deprecated)] +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for Path { #[cfg(unix)] fn as_os_str(&self) -> &OsStr { diff --git a/src/libstd/fs/mod.rs b/src/libstd/fs/mod.rs index 7df6d6887a2..ab65004f5d1 100644 --- a/src/libstd/fs/mod.rs +++ b/src/libstd/fs/mod.rs @@ -20,7 +20,7 @@ use core::prelude::*; use io::{self, Error, ErrorKind, SeekFrom, Seek, Read, Write}; -use path::{AsPath, Path, PathBuf}; +use path::{Path, PathBuf}; use sys::fs2 as fs_imp; use sys_common::{AsInnerMut, FromInner, AsInner}; use vec::Vec; @@ -129,7 +129,7 @@ impl File { /// This function will return an error if `path` does not already exist. /// Other errors may also be returned according to `OpenOptions::open`. #[stable(feature = "rust1", since = "1.0.0")] - pub fn open<P: AsPath>(path: P) -> io::Result<File> { + pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> { OpenOptions::new().read(true).open(path) } @@ -140,7 +140,7 @@ impl File { /// /// See the `OpenOptions::open` function for more details. #[stable(feature = "rust1", since = "1.0.0")] - pub fn create<P: AsPath>(path: P) -> io::Result<File> { + pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> { OpenOptions::new().write(true).create(true).truncate(true).open(path) } @@ -302,8 +302,8 @@ impl OpenOptions { /// permissions for /// * Filesystem-level errors (full disk, etc) #[stable(feature = "rust1", since = "1.0.0")] - pub fn open<P: AsPath>(&self, path: P) -> io::Result<File> { - let path = path.as_path(); + pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> { + let path = path.as_ref(); let inner = try!(fs_imp::File::open(path, &self.0)); Ok(File { path: path.to_path_buf(), inner: inner }) } @@ -415,8 +415,8 @@ impl DirEntry { /// user lacks permissions to remove the file, or if some other filesystem-level /// error occurs. #[stable(feature = "rust1", since = "1.0.0")] -pub fn remove_file<P: AsPath>(path: P) -> io::Result<()> { - fs_imp::unlink(path.as_path()) +pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> { + fs_imp::unlink(path.as_ref()) } /// Given a path, query the file system to get information about a file, @@ -443,8 +443,8 @@ pub fn remove_file<P: AsPath>(path: P) -> io::Result<()> { /// permissions to perform a `metadata` call on the given `path` or if there /// is no entry in the filesystem at the provided path. #[stable(feature = "rust1", since = "1.0.0")] -pub fn metadata<P: AsPath>(path: P) -> io::Result<Metadata> { - fs_imp::stat(path.as_path()).map(Metadata) +pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> { + fs_imp::stat(path.as_ref()).map(Metadata) } /// Rename a file or directory to a new name. @@ -464,8 +464,8 @@ pub fn metadata<P: AsPath>(path: P) -> io::Result<Metadata> { /// reside on separate filesystems, or if some other intermittent I/O error /// occurs. #[stable(feature = "rust1", since = "1.0.0")] -pub fn rename<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<()> { - fs_imp::rename(from.as_path(), to.as_path()) +pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> { + fs_imp::rename(from.as_ref(), to.as_ref()) } /// Copies the contents of one file to another. This function will also @@ -494,9 +494,9 @@ pub fn rename<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<()> { /// * The current process does not have the permission rights to access /// `from` or write `to` #[stable(feature = "rust1", since = "1.0.0")] -pub fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<u64> { - let from = from.as_path(); - let to = to.as_path(); +pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> { + let from = from.as_ref(); + let to = to.as_ref(); if !from.is_file() { return Err(Error::new(ErrorKind::InvalidInput, "the source path is not an existing file", @@ -517,16 +517,16 @@ pub fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<u64> { /// The `dst` path will be a link pointing to the `src` path. Note that systems /// often require these two paths to both be located on the same filesystem. #[stable(feature = "rust1", since = "1.0.0")] -pub fn hard_link<P: AsPath, Q: AsPath>(src: P, dst: Q) -> io::Result<()> { - fs_imp::link(src.as_path(), dst.as_path()) +pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> { + fs_imp::link(src.as_ref(), dst.as_ref()) } /// Creates a new soft link on the filesystem. /// /// The `dst` path will be a soft link pointing to the `src` path. #[stable(feature = "rust1", since = "1.0.0")] -pub fn soft_link<P: AsPath, Q: AsPath>(src: P, dst: Q) -> io::Result<()> { - fs_imp::symlink(src.as_path(), dst.as_path()) +pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> { + fs_imp::symlink(src.as_ref(), dst.as_ref()) } /// Reads a soft link, returning the file that the link points to. @@ -537,8 +537,8 @@ pub fn soft_link<P: AsPath, Q: AsPath>(src: P, dst: Q) -> io::Result<()> { /// reading a file that does not exist or reading a file that is not a soft /// link. #[stable(feature = "rust1", since = "1.0.0")] -pub fn read_link<P: AsPath>(path: P) -> io::Result<PathBuf> { - fs_imp::readlink(path.as_path()) +pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> { + fs_imp::readlink(path.as_ref()) } /// Create a new, empty directory at the provided path @@ -556,8 +556,8 @@ pub fn read_link<P: AsPath>(path: P) -> io::Result<PathBuf> { /// This function will return an error if the user lacks permissions to make a /// new directory at the provided `path`, or if the directory already exists. #[stable(feature = "rust1", since = "1.0.0")] -pub fn create_dir<P: AsPath>(path: P) -> io::Result<()> { - fs_imp::mkdir(path.as_path()) +pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { + fs_imp::mkdir(path.as_ref()) } /// Recursively create a directory and all of its parent components if they @@ -570,8 +570,8 @@ pub fn create_dir<P: AsPath>(path: P) -> io::Result<()> { /// error conditions for when a directory is being created (after it is /// determined to not exist) are outlined by `fs::create_dir`. #[stable(feature = "rust1", since = "1.0.0")] -pub fn create_dir_all<P: AsPath>(path: P) -> io::Result<()> { - let path = path.as_path(); +pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> { + let path = path.as_ref(); if path.is_dir() { return Ok(()) } if let Some(p) = path.parent() { try!(create_dir_all(p)) } create_dir(path) @@ -592,8 +592,8 @@ pub fn create_dir_all<P: AsPath>(path: P) -> io::Result<()> { /// This function will return an error if the user lacks permissions to remove /// the directory at the provided `path`, or if the directory isn't empty. #[stable(feature = "rust1", since = "1.0.0")] -pub fn remove_dir<P: AsPath>(path: P) -> io::Result<()> { - fs_imp::rmdir(path.as_path()) +pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { + fs_imp::rmdir(path.as_ref()) } /// Removes a directory at this path, after removing all its contents. Use @@ -606,8 +606,8 @@ pub fn remove_dir<P: AsPath>(path: P) -> io::Result<()> { /// /// See `file::remove_file` and `fs::remove_dir` #[stable(feature = "rust1", since = "1.0.0")] -pub fn remove_dir_all<P: AsPath>(path: P) -> io::Result<()> { - let path = path.as_path(); +pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> { + let path = path.as_ref(); for child in try!(read_dir(path)) { let child = try!(child).path(); let stat = try!(lstat(&*child)); @@ -659,8 +659,8 @@ pub fn remove_dir_all<P: AsPath>(path: P) -> io::Result<()> { /// the process lacks permissions to view the contents or if the `path` points /// at a non-directory file #[stable(feature = "rust1", since = "1.0.0")] -pub fn read_dir<P: AsPath>(path: P) -> io::Result<ReadDir> { - fs_imp::readdir(path.as_path()).map(ReadDir) +pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> { + fs_imp::readdir(path.as_ref()).map(ReadDir) } /// Returns an iterator that will recursively walk the directory structure @@ -675,7 +675,7 @@ pub fn read_dir<P: AsPath>(path: P) -> io::Result<ReadDir> { reason = "the precise semantics and defaults for a recursive walk \ may change and this may end up accounting for files such \ as symlinks differently")] -pub fn walk_dir<P: AsPath>(path: P) -> io::Result<WalkDir> { +pub fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<WalkDir> { let start = try!(read_dir(path)); Ok(WalkDir { cur: Some(start), stack: Vec::new() }) } @@ -761,9 +761,9 @@ impl PathExt for Path { reason = "the argument type of u64 is not quite appropriate for \ this function and may change if the standard library \ gains a type to represent a moment in time")] -pub fn set_file_times<P: AsPath>(path: P, accessed: u64, +pub fn set_file_times<P: AsRef<Path>>(path: P, accessed: u64, modified: u64) -> io::Result<()> { - fs_imp::utimes(path.as_path(), accessed, modified) + fs_imp::utimes(path.as_ref(), accessed, modified) } /// Changes the permissions found on a file or a directory. @@ -790,8 +790,8 @@ pub fn set_file_times<P: AsPath>(path: P, accessed: u64, reason = "a more granual ability to set specific permissions may \ be exposed on the Permissions structure itself and this \ method may not always exist")] -pub fn set_permissions<P: AsPath>(path: P, perm: Permissions) -> io::Result<()> { - fs_imp::set_perm(path.as_path(), perm.0) +pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> { + fs_imp::set_perm(path.as_ref(), perm.0) } #[cfg(test)] diff --git a/src/libstd/fs/tempdir.rs b/src/libstd/fs/tempdir.rs index 8f32d7a5864..a9717e36323 100644 --- a/src/libstd/fs/tempdir.rs +++ b/src/libstd/fs/tempdir.rs @@ -18,7 +18,7 @@ use prelude::v1::*; use env; use io::{self, Error, ErrorKind}; use fs; -use path::{self, PathBuf, AsPath}; +use path::{self, PathBuf}; use rand::{thread_rng, Rng}; /// A wrapper for a path to temporary directory implementing automatic @@ -43,10 +43,9 @@ impl TempDir { /// /// If no directory can be created, `Err` is returned. #[allow(deprecated)] // rand usage - pub fn new_in<P: AsPath + ?Sized>(tmpdir: &P, prefix: &str) - -> io::Result<TempDir> { + pub fn new_in<P: AsRef<path::Path>>(tmpdir: P, prefix: &str) -> io::Result<TempDir> { let storage; - let mut tmpdir = tmpdir.as_path(); + let mut tmpdir = tmpdir.as_ref(); if !tmpdir.is_absolute() { let cur_dir = try!(env::current_dir()); storage = cur_dir.join(tmpdir); diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index b055796ba54..1488c7969f6 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -126,8 +126,10 @@ #![feature(hash)] #![feature(int_uint)] #![feature(unique)] +#![feature(convert)] #![feature(allow_internal_unstable)] #![feature(str_char)] +#![feature(into_cow)] #![cfg_attr(test, feature(test, rustc_private))] // Don't link to std. We are std. @@ -169,6 +171,7 @@ pub use core::any; pub use core::cell; pub use core::clone; #[cfg(not(test))] pub use core::cmp; +pub use core::convert; pub use core::default; #[allow(deprecated)] pub use core::finally; diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 73c2464a6b2..d737ad17ff8 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -374,7 +374,6 @@ impl fmt::Display for Ipv6Addr { .iter() .map(|&seg| format!("{:x}", seg)) .collect::<Vec<String>>() - .as_slice() .connect(":") } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 3870b8614ff..72f9338b456 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -38,6 +38,7 @@ use self::MapError::*; use boxed::Box; use clone::Clone; +use convert::From; use env; use error::{FromError, Error}; use ffi::{OsString, OsStr}; @@ -79,12 +80,12 @@ fn err2old(new: ::io::Error) -> IoError { #[cfg(windows)] fn path2new(path: &Path) -> PathBuf { - PathBuf::new(path.as_str().unwrap()) + PathBuf::from(path.as_str().unwrap()) } #[cfg(unix)] fn path2new(path: &Path) -> PathBuf { use os::unix::prelude::*; - PathBuf::new(<OsStr as OsStrExt>::from_bytes(path.as_vec())) + PathBuf::from(<OsStr as OsStrExt>::from_bytes(path.as_vec())) } #[cfg(unix)] diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 05c7761be7b..8ee33e94fe7 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -106,6 +106,7 @@ use cmp; use iter::{self, IntoIterator}; use mem; use ops::{self, Deref}; +use string::String; use vec::Vec; use fmt; @@ -527,6 +528,13 @@ impl<'a> Component<'a> { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> AsRef<OsStr> for Component<'a> { + fn as_ref(&self) -> &OsStr { + self.as_os_str() + } +} + /// The core iterator giving the components of a path. /// /// See the module documentation for an in-depth explanation of components and @@ -601,6 +609,7 @@ impl<'a> Components<'a> { } /// Extract a slice corresponding to the portion of the path remaining for iteration. + #[stable(feature = "rust1", since = "1.0.0")] pub fn as_path(&self) -> &'a Path { let mut comps = self.clone(); if comps.front == State::Body { comps.trim_left(); } @@ -695,6 +704,20 @@ impl<'a> Components<'a> { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> AsRef<Path> for Components<'a> { + fn as_ref(&self) -> &Path { + self.as_path() + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> AsRef<OsStr> for Components<'a> { + fn as_ref(&self) -> &OsStr { + self.as_path().as_os_str() + } +} + impl<'a> Iter<'a> { /// Extract a slice corresponding to the portion of the path remaining for iteration. #[stable(feature = "rust1", since = "1.0.0")] @@ -704,6 +727,20 @@ impl<'a> Iter<'a> { } #[stable(feature = "rust1", since = "1.0.0")] +impl<'a> AsRef<Path> for Iter<'a> { + fn as_ref(&self) -> &Path { + self.as_path() + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> AsRef<OsStr> for Iter<'a> { + fn as_ref(&self) -> &OsStr { + self.as_path().as_os_str() + } +} + +#[stable(feature = "rust1", since = "1.0.0")] impl<'a> Iterator for Iter<'a> { type Item = &'a OsStr; @@ -873,11 +910,10 @@ impl PathBuf { unsafe { mem::transmute(self) } } - /// Allocate a `PathBuf` with initial contents given by the - /// argument. + /// Allocate an empty `PathBuf`. #[stable(feature = "rust1", since = "1.0.0")] - pub fn new<S: AsOsStr>(s: S) -> PathBuf { - PathBuf { inner: s.as_os_str().to_os_string() } + pub fn new() -> PathBuf { + PathBuf { inner: OsString::new() } } /// Extend `self` with `path`. @@ -890,8 +926,8 @@ impl PathBuf { /// replaces everything except for the prefix (if any) of `self`. /// * if `path` has a prefix but no root, it replaces `self. #[stable(feature = "rust1", since = "1.0.0")] - pub fn push<P: AsPath>(&mut self, path: P) { - let path = path.as_path(); + pub fn push<P: AsRef<Path>>(&mut self, path: P) { + let path = path.as_ref(); // 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); @@ -958,12 +994,12 @@ impl PathBuf { /// assert!(buf == PathBuf::new("/baz.txt")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn set_file_name<S: AsOsStr>(&mut self, file_name: S) { + pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) { if self.file_name().is_some() { let popped = self.pop(); debug_assert!(popped); } - self.push(file_name.as_os_str()); + self.push(file_name.as_ref()); } /// Updates `self.extension()` to `extension`. @@ -973,15 +1009,15 @@ impl PathBuf { /// Otherwise, returns `true`; if `self.extension()` is `None`, the extension /// is added; otherwise it is replaced. #[stable(feature = "rust1", since = "1.0.0")] - pub fn set_extension<S: AsOsStr>(&mut self, extension: S) -> bool { + pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool { if self.file_name().is_none() { return false; } let mut stem = match self.file_stem() { Some(stem) => stem.to_os_string(), - None => OsString::from_str(""), + None => OsString::new(), }; - let extension = extension.as_os_str(); + let extension = extension.as_ref(); if os_str_as_u8_slice(extension).len() > 0 { stem.push("."); stem.push(extension); @@ -999,16 +1035,65 @@ impl PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -impl<P: AsPath> iter::FromIterator<P> for PathBuf { +impl<'a> From<&'a Path> for PathBuf { + fn from(s: &'a Path) -> PathBuf { + s.to_path_buf() + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a str> for PathBuf { + fn from(s: &'a str) -> PathBuf { + PathBuf::from(OsString::from(s)) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a String> for PathBuf { + fn from(s: &'a String) -> PathBuf { + PathBuf::from(OsString::from(s)) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl From<String> for PathBuf { + fn from(s: String) -> PathBuf { + PathBuf::from(OsString::from(s)) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a OsStr> for PathBuf { + fn from(s: &'a OsStr) -> PathBuf { + PathBuf::from(OsString::from(s)) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a OsString> for PathBuf { + fn from(s: &'a OsString) -> PathBuf { + PathBuf::from(s.to_os_string()) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl From<OsString> for PathBuf { + fn from(s: OsString) -> PathBuf { + PathBuf { inner: s } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf { fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf { - let mut buf = PathBuf::new(""); + let mut buf = PathBuf::new(); buf.extend(iter); buf } } #[stable(feature = "rust1", since = "1.0.0")] -impl<P: AsPath> iter::Extend<P> for PathBuf { +impl<P: AsRef<Path>> iter::Extend<P> for PathBuf { fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) { for p in iter { self.push(p) @@ -1084,12 +1169,27 @@ impl cmp::Ord for PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<OsStr> for PathBuf { + fn as_ref(&self) -> &OsStr { + &self.inner[..] + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for PathBuf { fn as_os_str(&self) -> &OsStr { &self.inner[..] } } +#[stable(feature = "rust1", since = "1.0.0")] +impl Into<OsString> for PathBuf { + fn into(self) -> OsString { + self.inner + } +} + /// A slice of a path (akin to `str`). /// /// This type supports a number of operations for inspecting a path, including @@ -1133,8 +1233,14 @@ impl Path { /// /// This is a cost-free conversion. #[stable(feature = "rust1", since = "1.0.0")] - pub fn new<S: ?Sized + AsOsStr>(s: &S) -> &Path { - unsafe { mem::transmute(s.as_os_str()) } + pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path { + unsafe { mem::transmute(s.as_ref()) } + } + + /// Yield the underlying `OsStr` slice. + #[stable(feature = "rust1", since = "1.0.0")] + pub fn as_os_str(&self) -> &OsStr { + &self.inner } /// Yield a `&str` slice if the `Path` is valid unicode. @@ -1156,7 +1262,7 @@ impl Path { /// Convert a `Path` to an owned `PathBuf`. #[stable(feature = "rust1", since = "1.0.0")] pub fn to_path_buf(&self) -> PathBuf { - PathBuf::new(self) + PathBuf::from(self.inner.to_os_string()) } /// A path is *absolute* if it is independent of the current directory. @@ -1247,22 +1353,21 @@ impl Path { /// If `base` is not a prefix of `self` (i.e. `starts_with` /// returns false), then `relative_from` returns `None`. #[unstable(feature = "path_relative_from", reason = "see #23284")] - pub fn relative_from<'a, P: ?Sized>(&'a self, base: &'a P) -> Option<&Path> where - P: AsPath + pub fn relative_from<'a, P: ?Sized + AsRef<Path>>(&'a self, base: &'a P) -> Option<&Path> { - iter_after(self.components(), base.as_path().components()).map(|c| c.as_path()) + iter_after(self.components(), base.as_ref().components()).map(|c| c.as_path()) } /// Determines whether `base` is a prefix of `self`. #[stable(feature = "rust1", since = "1.0.0")] - pub fn starts_with<P: AsPath>(&self, base: P) -> bool { - iter_after(self.components(), base.as_path().components()).is_some() + pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool { + iter_after(self.components(), base.as_ref().components()).is_some() } /// Determines whether `child` is a suffix of `self`. #[stable(feature = "rust1", since = "1.0.0")] - pub fn ends_with<P: AsPath>(&self, child: P) -> bool { - iter_after(self.components().rev(), child.as_path().components().rev()).is_some() + pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool { + iter_after(self.components().rev(), child.as_ref().components().rev()).is_some() } /// Extract the stem (non-extension) portion of `self.file()`. @@ -1295,7 +1400,7 @@ impl Path { /// /// See `PathBuf::push` for more details on what it means to adjoin a path. #[stable(feature = "rust1", since = "1.0.0")] - pub fn join<P: AsPath>(&self, path: P) -> PathBuf { + pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf { let mut buf = self.to_path_buf(); buf.push(path); buf @@ -1305,7 +1410,7 @@ impl Path { /// /// See `PathBuf::set_file_name` for more details. #[stable(feature = "rust1", since = "1.0.0")] - pub fn with_file_name<S: AsOsStr>(&self, file_name: S) -> PathBuf { + pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf { let mut buf = self.to_path_buf(); buf.set_file_name(file_name); buf @@ -1315,7 +1420,7 @@ impl Path { /// /// See `PathBuf::set_extension` for more details. #[stable(feature = "rust1", since = "1.0.0")] - pub fn with_extension<S: AsOsStr>(&self, extension: S) -> PathBuf { + pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf { let mut buf = self.to_path_buf(); buf.set_extension(extension); buf @@ -1349,6 +1454,14 @@ impl Path { } #[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<OsStr> for Path { + fn as_ref(&self) -> &OsStr { + &self.inner + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", reason = "trait is deprecated")] impl AsOsStr for Path { fn as_os_str(&self) -> &OsStr { &self.inner @@ -1408,6 +1521,7 @@ impl cmp::Ord for Path { /// Freely convertible to a `Path`. #[unstable(feature = "std_misc")] +#[deprecated(since = "1.0.0", reason = "use std::convert::AsRef<Path> instead")] pub trait AsPath { /// Convert to a `Path`. #[unstable(feature = "std_misc")] @@ -1415,10 +1529,42 @@ pub trait AsPath { } #[unstable(feature = "std_misc")] +#[deprecated(since = "1.0.0", reason = "use std::convert::AsRef<Path> instead")] +#[allow(deprecated)] impl<T: AsOsStr + ?Sized> AsPath for T { fn as_path(&self) -> &Path { Path::new(self.as_os_str()) } } +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<Path> for Path { + fn as_ref(&self) -> &Path { self } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<Path> for OsStr { + fn as_ref(&self) -> &Path { Path::new(self) } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<Path> for OsString { + fn as_ref(&self) -> &Path { Path::new(self) } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<Path> for str { + fn as_ref(&self) -> &Path { Path::new(self) } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<Path> for String { + fn as_ref(&self) -> &Path { Path::new(self) } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl AsRef<Path> for PathBuf { + fn as_ref(&self) -> &Path { self } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs index a0b4c80e9f3..6e12ac1a226 100644 --- a/src/libstd/prelude/v1.rs +++ b/src/libstd/prelude/v1.rs @@ -29,6 +29,8 @@ #[doc(no_inline)] pub use clone::Clone; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord}; +#[unstable(feature = "convert")] +#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From}; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use iter::DoubleEndedIterator; #[stable(feature = "rust1", since = "1.0.0")] @@ -40,8 +42,10 @@ #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use result::Result::{self, Ok, Err}; #[stable(feature = "rust1", since = "1.0.0")] +#[allow(deprecated)] #[doc(no_inline)] pub use slice::{SliceConcatExt, AsSlice}; #[stable(feature = "rust1", since = "1.0.0")] +#[allow(deprecated)] #[doc(no_inline)] pub use str::Str; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use string::{String, ToString}; diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 6b09636c1df..d11c3d22144 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -19,8 +19,8 @@ use io::prelude::*; use ffi::AsOsStr; use fmt; use io::{self, Error, ErrorKind}; -use path::AsPath; use libc; +use path; use sync::mpsc::{channel, Receiver}; use sys::pipe2::{self, AnonPipe}; use sys::process2::Process as ProcessImp; @@ -198,8 +198,8 @@ impl Command { /// Set the working directory for the child process. #[stable(feature = "process", since = "1.0.0")] - pub fn current_dir<P: AsPath>(&mut self, dir: P) -> &mut Command { - self.inner.cwd(dir.as_path().as_os_str()); + pub fn current_dir<P: AsRef<path::Path>>(&mut self, dir: P) -> &mut Command { + self.inner.cwd(dir.as_ref().as_os_str()); self } diff --git a/src/libstd/sys/unix/fs2.rs b/src/libstd/sys/unix/fs2.rs index ea74aab3331..202e5ddaec4 100644 --- a/src/libstd/sys/unix/fs2.rs +++ b/src/libstd/sys/unix/fs2.rs @@ -338,8 +338,7 @@ pub fn readlink(p: &Path) -> io::Result<PathBuf> { })); buf.set_len(n as usize); } - let s: OsString = OsStringExt::from_vec(buf); - Ok(PathBuf::new(&s)) + Ok(PathBuf::from(OsString::from_vec(buf))) } pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> { diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index a5a2f71acb7..6c191689255 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -36,7 +36,7 @@ const BUF_BYTES: usize = 2048; const TMPBUF_SZ: usize = 128; fn bytes2path(b: &[u8]) -> PathBuf { - PathBuf::new(<OsStr as OsStrExt>::from_bytes(b)) + PathBuf::from(<OsStr as OsStrExt>::from_bytes(b)) } fn os2path(os: OsString) -> PathBuf { @@ -253,7 +253,7 @@ pub fn current_exe() -> io::Result<PathBuf> { let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz); if err != 0 { return Err(io::Error::last_os_error()); } v.set_len(sz as uint - 1); // chop off trailing NUL - Ok(PathBuf::new(OsString::from_vec(v))) + Ok(PathBuf::from(OsString::from_vec(v))) } } @@ -466,9 +466,9 @@ pub fn page_size() -> usize { pub fn temp_dir() -> PathBuf { getenv("TMPDIR".as_os_str()).map(os2path).unwrap_or_else(|| { if cfg!(target_os = "android") { - PathBuf::new("/data/local/tmp") + PathBuf::from("/data/local/tmp") } else { - PathBuf::new("/tmp") + PathBuf::from("/tmp") } }) } |
