Skip to content

Commit

Permalink
Rollup merge of rust-lang#68102 - lzutao:inline, r=alexcrichton
Browse files Browse the repository at this point in the history
Inline some conversion methods around OsStr

Diff on the assembly of this snippet before and after this PR: https://www.diffchecker.com/NeGMjaJ2
```rust
use std::env;
use std::io;
use std::path::{Path, PathBuf};

pub fn cargo_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
    match env::var_os("CARGO_HOME").filter(|h| !h.is_empty()) {
        Some(home) => {
            let home = PathBuf::from(home);
            if home.is_absolute() {
                Ok(home)
            } else {
                Ok(cwd.join(&home))
            }
        }
        _ => env::home_dir()
            .map(|p| p.join(".cargo"))
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find cargo home dir")),
    }
}
```
  • Loading branch information
Centril authored Jan 11, 2020
2 parents 4b7838f + 5f3f1a3 commit 7817299
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl From<String> for Box<dyn Error + Send + Sync> {
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
#[inline]
fn from(err: String) -> Box<dyn Error + Send + Sync> {
struct StringError(String);

Expand Down Expand Up @@ -317,6 +318,7 @@ impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
#[inline]
fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ impl OsStr {
/// assert!(!os_str.is_empty());
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.inner.is_empty()
}
Expand Down Expand Up @@ -965,6 +966,7 @@ impl AsRef<OsStr> for OsStr {

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for OsString {
#[inline]
fn as_ref(&self) -> &OsStr {
self
}
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,7 @@ impl From<OsString> for PathBuf {
/// Converts a `OsString` into a `PathBuf`
///
/// This conversion does not allocate or copy memory.
#[inline]
fn from(s: OsString) -> PathBuf {
PathBuf { inner: s }
}
Expand Down Expand Up @@ -1535,7 +1536,7 @@ impl fmt::Debug for PathBuf {
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Deref for PathBuf {
type Target = Path;

#[inline]
fn deref(&self) -> &Path {
Path::new(&self.inner)
}
Expand Down Expand Up @@ -2655,6 +2656,7 @@ impl AsRef<Path> for OsString {

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<Path> for str {
#[inline]
fn as_ref(&self) -> &Path {
Path::new(self)
}
Expand All @@ -2669,6 +2671,7 @@ impl AsRef<Path> for String {

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<Path> for PathBuf {
#[inline]
fn as_ref(&self) -> &Path {
self
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys_common/os_str_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl Buf {
self.inner.shrink_to(min_capacity)
}

#[inline]
pub fn as_slice(&self) -> &Slice {
unsafe { mem::transmute(&*self.inner) }
}
Expand Down

0 comments on commit 7817299

Please sign in to comment.