Skip to content

Reduce code bloat from conversion traits in function parameters #28256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/librustc_back/tempdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ impl TempDir {
#[allow(deprecated)] // rand usage
pub fn new_in<P: AsRef<Path>>(tmpdir: P, prefix: &str)
-> io::Result<TempDir> {
Self::_new_in(tmpdir.as_ref(), prefix)
}

fn _new_in(tmpdir: &Path, prefix: &str) -> io::Result<TempDir> {
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);
Expand Down
22 changes: 19 additions & 3 deletions src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl Iterator for VarsOs {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
_var(key.as_ref())
}

fn _var(key: &OsStr) -> Result<String, VarError> {
match var_os(key) {
Some(s) => s.into_string().map_err(VarError::NotUnicode),
None => Err(VarError::NotPresent)
Expand All @@ -196,8 +200,12 @@ pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
_var_os(key.as_ref())
}

fn _var_os(key: &OsStr) -> Option<OsString> {
let _g = ENV_LOCK.lock();
os_imp::getenv(key.as_ref())
os_imp::getenv(key)
}

/// Possible errors from the `env::var` method.
Expand Down Expand Up @@ -263,8 +271,12 @@ impl Error for VarError {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(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.
Expand Down Expand Up @@ -294,8 +306,12 @@ pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(k: K, v: V) {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn remove_var<K: AsRef<OsStr>>(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
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ impl CString {
/// the position of the nul byte.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
let bytes = t.into();
Self::_new(t.into())
}

fn _new(bytes: Vec<u8>) -> Result<CString, NulError> {
match bytes.iter().position(|x| *x == 0) {
Some(i) => Err(NulError(i, bytes)),
None => Ok(unsafe { CString::from_vec_unchecked(bytes) }),
Expand Down
20 changes: 10 additions & 10 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B>(bytes: B) -> Option<OsString> where B: Into<Vec<u8>> {
#[cfg(unix)]
fn from_bytes_inner(vec: Vec<u8>) -> Option<OsString> {
use os::unix::ffi::OsStringExt;
Some(OsString::from_vec(vec))
}
Self::_from_bytes(bytes.into())
}

#[cfg(windows)]
fn from_bytes_inner(vec: Vec<u8>) -> Option<OsString> {
String::from_utf8(vec).ok().map(OsString::from)
}
#[cfg(unix)]
fn _from_bytes(vec: Vec<u8>) -> Option<OsString> {
use os::unix::ffi::OsStringExt;
Some(OsString::from_vec(vec))
}

from_bytes_inner(bytes.into())
#[cfg(windows)]
fn _from_bytes(vec: Vec<u8>) -> Option<OsString> {
String::from_utf8(vec).ok().map(OsString::from)
}

/// Converts to an `OsStr` slice.
Expand Down
23 changes: 18 additions & 5 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl File {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
OpenOptions::new().read(true).open(path)
OpenOptions::new().read(true).open(path.as_ref())
}

/// Opens a file in write-only mode.
Expand All @@ -206,7 +206,7 @@ impl File {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
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.
Expand Down Expand Up @@ -494,7 +494,10 @@ impl OpenOptions {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
let path = path.as_ref();
self._open(path.as_ref())
}

fn _open(&self, path: &Path) -> io::Result<File> {
let inner = try!(fs_imp::File::open(path, &self.0));
Ok(File { inner: inner })
}
Expand Down Expand Up @@ -1048,7 +1051,10 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_dir_all<P: AsRef<Path>>(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));
Expand Down Expand Up @@ -1113,6 +1119,10 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
as symlinks differently",
issue = "27707")]
pub fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<WalkDir> {
_walk_dir(path.as_ref())
}

fn _walk_dir(path: &Path) -> io::Result<WalkDir> {
let start = try!(read_dir(path));
Ok(WalkDir { cur: Some(start), stack: Vec::new() })
}
Expand Down Expand Up @@ -1272,7 +1282,10 @@ impl DirBuilder {
/// Create the specified directory with the options configured in this
/// builder.
pub fn create<P: AsRef<Path>>(&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 {
Expand Down
6 changes: 5 additions & 1 deletion src/libstd/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,14 @@ impl Error {
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where E: Into<Box<error::Error+Send+Sync>>
{
Self::_new(kind, error.into())
}

fn _new(kind: ErrorKind, error: Box<error::Error+Send+Sync>) -> Error {
Error {
repr: Repr::Custom(Box::new(Custom {
kind: kind,
error: error.into(),
error: error,
}))
}
}
Expand Down
47 changes: 40 additions & 7 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(&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);

Expand Down Expand Up @@ -1033,11 +1035,15 @@ impl PathBuf {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn set_file_name<S: AsRef<OsStr>>(&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`.
Expand All @@ -1048,14 +1054,17 @@ impl PathBuf {
/// is added; otherwise it is replaced.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn set_extension<S: AsRef<OsStr>>(&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() {
Some(stem) => stem.to_os_string(),
None => OsString::new(),
};

let extension = extension.as_ref();
if !os_str_as_u8_slice(extension).is_empty() {
stem.push(".");
stem.push(extension);
Expand Down Expand Up @@ -1106,7 +1115,7 @@ impl<P: AsRef<Path>> iter::FromIterator<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)
self.push(p.as_ref())
}
}
}
Expand Down Expand Up @@ -1452,7 +1461,11 @@ impl Path {
issue = "23284")]
pub fn relative_from<'a, P: ?Sized + AsRef<Path>>(&'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`.
Expand All @@ -1472,7 +1485,11 @@ impl Path {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn starts_with<P: AsRef<Path>>(&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`.
Expand All @@ -1490,7 +1507,11 @@ impl Path {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ends_with<P: AsRef<Path>>(&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()`.
Expand Down Expand Up @@ -1552,6 +1573,10 @@ impl Path {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn join<P: AsRef<Path>>(&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
Expand All @@ -1571,6 +1596,10 @@ impl Path {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_file_name<S: AsRef<OsStr>>(&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
Expand All @@ -1590,6 +1619,10 @@ impl Path {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_extension<S: AsRef<OsStr>>(&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
Expand Down