Skip to content

Commit

Permalink
Rollup merge of #81281 - a1phyr:inline_path, r=dtolnay
Browse files Browse the repository at this point in the history
Inline methods of Path and OsString

These methods are not generic, and therefore aren't candidates for cross-crate inlining without an `#[inline]` attribute.
  • Loading branch information
jonas-schievink committed Jan 23, 2021
2 parents 81647c6 + 9880560 commit 44c668c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
34 changes: 34 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl OsString {
/// let os_string = OsString::new();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn new() -> OsString {
OsString { inner: Buf::from_string(String::new()) }
}
Expand All @@ -127,6 +128,7 @@ impl OsString {
/// assert_eq!(os_string.as_os_str(), os_str);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn as_os_str(&self) -> &OsStr {
self
}
Expand All @@ -145,6 +147,7 @@ impl OsString {
/// assert_eq!(string, Ok(String::from("foo")));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn into_string(self) -> Result<String, OsString> {
self.inner.into_string().map_err(|buf| OsString { inner: buf })
}
Expand All @@ -163,6 +166,7 @@ impl OsString {
/// assert_eq!(&os_string, "foobar");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
}
Expand All @@ -189,6 +193,7 @@ impl OsString {
/// assert_eq!(capacity, os_string.capacity());
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn with_capacity(capacity: usize) -> OsString {
OsString { inner: Buf::with_capacity(capacity) }
}
Expand All @@ -207,6 +212,7 @@ impl OsString {
/// assert_eq!(&os_string, "");
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn clear(&mut self) {
self.inner.clear()
}
Expand All @@ -224,6 +230,7 @@ impl OsString {
/// assert!(os_string.capacity() >= 10);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
Expand All @@ -243,6 +250,7 @@ impl OsString {
/// assert!(s.capacity() >= 10);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
Expand All @@ -265,6 +273,7 @@ impl OsString {
/// assert!(s.capacity() >= 10);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
Expand All @@ -285,6 +294,7 @@ impl OsString {
/// assert_eq!(3, s.capacity());
/// ```
#[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
#[inline]
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit()
}
Expand Down Expand Up @@ -342,6 +352,7 @@ impl From<String> for OsString {
/// Converts a [`String`] into a [`OsString`].
///
/// The conversion copies the data, and includes an allocation on the heap.
#[inline]
fn from(s: String) -> OsString {
OsString { inner: Buf::from_string(s) }
}
Expand Down Expand Up @@ -408,34 +419,39 @@ impl fmt::Debug for OsString {

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for OsString {
#[inline]
fn eq(&self, other: &OsString) -> bool {
&**self == &**other
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<str> for OsString {
#[inline]
fn eq(&self, other: &str) -> bool {
&**self == other
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<OsString> for str {
#[inline]
fn eq(&self, other: &OsString) -> bool {
&**other == self
}
}

#[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
impl PartialEq<&str> for OsString {
#[inline]
fn eq(&self, other: &&str) -> bool {
**self == **other
}
}

#[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
impl<'a> PartialEq<OsString> for &'a str {
#[inline]
fn eq(&self, other: &OsString) -> bool {
**other == **self
}
Expand Down Expand Up @@ -539,6 +555,7 @@ impl OsStr {
/// assert_eq!(os_str.to_str(), Some("foo"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_str(&self) -> Option<&str> {
self.inner.to_str()
}
Expand Down Expand Up @@ -589,6 +606,7 @@ impl OsStr {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_string_lossy(&self) -> Cow<'_, str> {
self.inner.to_string_lossy()
}
Expand All @@ -605,6 +623,7 @@ impl OsStr {
/// assert_eq!(os_string, OsString::from("foo"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_os_string(&self) -> OsString {
OsString { inner: self.inner.to_owned() }
}
Expand Down Expand Up @@ -655,6 +674,7 @@ impl OsStr {
/// ```
#[doc(alias = "length")]
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn len(&self) -> usize {
self.inner.inner.len()
}
Expand Down Expand Up @@ -696,6 +716,7 @@ impl OsStr {
/// assert_eq!("grÜße, jÜrgen ❤", s);
/// ```
#[unstable(feature = "osstring_ascii", issue = "70516")]
#[inline]
pub fn make_ascii_lowercase(&mut self) {
self.inner.make_ascii_lowercase()
}
Expand All @@ -721,6 +742,7 @@ impl OsStr {
/// assert_eq!("GRüßE, JüRGEN ❤", s);
/// ```
#[unstable(feature = "osstring_ascii", issue = "70516")]
#[inline]
pub fn make_ascii_uppercase(&mut self) {
self.inner.make_ascii_uppercase()
}
Expand Down Expand Up @@ -784,6 +806,7 @@ impl OsStr {
/// assert!(!non_ascii.is_ascii());
/// ```
#[unstable(feature = "osstring_ascii", issue = "70516")]
#[inline]
pub fn is_ascii(&self) -> bool {
self.inner.is_ascii()
}
Expand Down Expand Up @@ -811,6 +834,7 @@ impl OsStr {

#[stable(feature = "box_from_os_str", since = "1.17.0")]
impl From<&OsStr> for Box<OsStr> {
#[inline]
fn from(s: &OsStr) -> Box<OsStr> {
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
Expand All @@ -832,6 +856,7 @@ impl From<Cow<'_, OsStr>> for Box<OsStr> {
impl From<Box<OsStr>> for OsString {
/// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
/// allocating.
#[inline]
fn from(boxed: Box<OsStr>) -> OsString {
boxed.into_os_string()
}
Expand All @@ -840,6 +865,7 @@ impl From<Box<OsStr>> for OsString {
#[stable(feature = "box_from_os_string", since = "1.20.0")]
impl From<OsString> for Box<OsStr> {
/// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
#[inline]
fn from(s: OsString) -> Box<OsStr> {
s.into_boxed_os_str()
}
Expand Down Expand Up @@ -925,6 +951,7 @@ impl<'a> From<Cow<'a, OsStr>> for OsString {

#[stable(feature = "box_default_extra", since = "1.17.0")]
impl Default for Box<OsStr> {
#[inline]
fn default() -> Box<OsStr> {
let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
Expand Down Expand Up @@ -1075,6 +1102,7 @@ impl OsStr {

#[stable(feature = "rust1", since = "1.0.0")]
impl Borrow<OsStr> for OsString {
#[inline]
fn borrow(&self) -> &OsStr {
&self[..]
}
Expand All @@ -1083,16 +1111,19 @@ impl Borrow<OsStr> for OsString {
#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for OsStr {
type Owned = OsString;
#[inline]
fn to_owned(&self) -> OsString {
self.to_os_string()
}
#[inline]
fn clone_into(&self, target: &mut OsString) {
self.inner.clone_into(&mut target.inner)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for OsStr {
#[inline]
fn as_ref(&self) -> &OsStr {
self
}
Expand Down Expand Up @@ -1123,12 +1154,14 @@ impl AsRef<OsStr> for String {
}

impl FromInner<Buf> for OsString {
#[inline]
fn from_inner(buf: Buf) -> OsString {
OsString { inner: buf }
}
}

impl IntoInner<Buf> for OsString {
#[inline]
fn into_inner(self) -> Buf {
self.inner
}
Expand All @@ -1145,6 +1178,7 @@ impl AsInner<Slice> for OsStr {
impl FromStr for OsString {
type Err = core::convert::Infallible;

#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(OsString::from(s))
}
Expand Down
Loading

0 comments on commit 44c668c

Please sign in to comment.