Auto merge of #42716 - alexbool:cstr-inlines, r=BurntSushi
Mark smaller CStr and CString functions as #[inline]
This commit is contained in:
commit
29bce6e220
1 changed files with 15 additions and 0 deletions
|
@ -308,6 +308,7 @@ impl CString {
|
||||||
/// let _ = CString::from_raw(ptr);
|
/// let _ = CString::from_raw(ptr);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
||||||
pub fn into_raw(self) -> *mut c_char {
|
pub fn into_raw(self) -> *mut c_char {
|
||||||
Box::into_raw(self.into_inner()) as *mut c_char
|
Box::into_raw(self.into_inner()) as *mut c_char
|
||||||
|
@ -382,6 +383,7 @@ impl CString {
|
||||||
/// let bytes = c_string.as_bytes();
|
/// let bytes = c_string.as_bytes();
|
||||||
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
|
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn as_bytes(&self) -> &[u8] {
|
pub fn as_bytes(&self) -> &[u8] {
|
||||||
&self.inner[..self.inner.len() - 1]
|
&self.inner[..self.inner.len() - 1]
|
||||||
|
@ -401,6 +403,7 @@ impl CString {
|
||||||
/// let bytes = c_string.as_bytes_with_nul();
|
/// let bytes = c_string.as_bytes_with_nul();
|
||||||
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
|
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn as_bytes_with_nul(&self) -> &[u8] {
|
pub fn as_bytes_with_nul(&self) -> &[u8] {
|
||||||
&self.inner
|
&self.inner
|
||||||
|
@ -409,6 +412,7 @@ impl CString {
|
||||||
/// Extracts a [`CStr`] slice containing the entire string.
|
/// Extracts a [`CStr`] slice containing the entire string.
|
||||||
///
|
///
|
||||||
/// [`CStr`]: struct.CStr.html
|
/// [`CStr`]: struct.CStr.html
|
||||||
|
#[inline]
|
||||||
#[unstable(feature = "as_c_str", issue = "40380")]
|
#[unstable(feature = "as_c_str", issue = "40380")]
|
||||||
pub fn as_c_str(&self) -> &CStr {
|
pub fn as_c_str(&self) -> &CStr {
|
||||||
&*self
|
&*self
|
||||||
|
@ -449,6 +453,7 @@ impl Drop for CString {
|
||||||
impl ops::Deref for CString {
|
impl ops::Deref for CString {
|
||||||
type Target = CStr;
|
type Target = CStr;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn deref(&self) -> &CStr {
|
fn deref(&self) -> &CStr {
|
||||||
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
|
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
|
||||||
}
|
}
|
||||||
|
@ -463,6 +468,7 @@ impl fmt::Debug for CString {
|
||||||
|
|
||||||
#[stable(feature = "cstring_into", since = "1.7.0")]
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
||||||
impl From<CString> for Vec<u8> {
|
impl From<CString> for Vec<u8> {
|
||||||
|
#[inline]
|
||||||
fn from(s: CString) -> Vec<u8> {
|
fn from(s: CString) -> Vec<u8> {
|
||||||
s.into_bytes()
|
s.into_bytes()
|
||||||
}
|
}
|
||||||
|
@ -498,6 +504,7 @@ impl Default for CString {
|
||||||
|
|
||||||
#[stable(feature = "cstr_borrow", since = "1.3.0")]
|
#[stable(feature = "cstr_borrow", since = "1.3.0")]
|
||||||
impl Borrow<CStr> for CString {
|
impl Borrow<CStr> for CString {
|
||||||
|
#[inline]
|
||||||
fn borrow(&self) -> &CStr { self }
|
fn borrow(&self) -> &CStr { self }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,6 +518,7 @@ impl<'a> From<&'a CStr> for Box<CStr> {
|
||||||
|
|
||||||
#[stable(feature = "c_string_from_box", since = "1.18.0")]
|
#[stable(feature = "c_string_from_box", since = "1.18.0")]
|
||||||
impl From<Box<CStr>> for CString {
|
impl From<Box<CStr>> for CString {
|
||||||
|
#[inline]
|
||||||
fn from(s: Box<CStr>) -> CString {
|
fn from(s: Box<CStr>) -> CString {
|
||||||
s.into_c_string()
|
s.into_c_string()
|
||||||
}
|
}
|
||||||
|
@ -518,6 +526,7 @@ impl From<Box<CStr>> for CString {
|
||||||
|
|
||||||
#[stable(feature = "box_from_c_string", since = "1.18.0")]
|
#[stable(feature = "box_from_c_string", since = "1.18.0")]
|
||||||
impl Into<Box<CStr>> for CString {
|
impl Into<Box<CStr>> for CString {
|
||||||
|
#[inline]
|
||||||
fn into(self) -> Box<CStr> {
|
fn into(self) -> Box<CStr> {
|
||||||
self.into_boxed_c_str()
|
self.into_boxed_c_str()
|
||||||
}
|
}
|
||||||
|
@ -730,6 +739,7 @@ impl CStr {
|
||||||
/// assert_eq!(cstr, &*cstring);
|
/// assert_eq!(cstr, &*cstring);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
|
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
|
||||||
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
|
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
|
||||||
mem::transmute(bytes)
|
mem::transmute(bytes)
|
||||||
|
@ -772,6 +782,7 @@ impl CStr {
|
||||||
/// *ptr;
|
/// *ptr;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn as_ptr(&self) -> *const c_char {
|
pub fn as_ptr(&self) -> *const c_char {
|
||||||
self.inner.as_ptr()
|
self.inner.as_ptr()
|
||||||
|
@ -789,6 +800,7 @@ impl CStr {
|
||||||
/// > **Note**: This method is currently implemented as a 0-cost cast, but
|
/// > **Note**: This method is currently implemented as a 0-cost cast, but
|
||||||
/// > it is planned to alter its definition in the future to perform the
|
/// > it is planned to alter its definition in the future to perform the
|
||||||
/// > length calculation whenever this method is called.
|
/// > length calculation whenever this method is called.
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn to_bytes(&self) -> &[u8] {
|
pub fn to_bytes(&self) -> &[u8] {
|
||||||
let bytes = self.to_bytes_with_nul();
|
let bytes = self.to_bytes_with_nul();
|
||||||
|
@ -805,6 +817,7 @@ impl CStr {
|
||||||
/// > length calculation whenever this method is called.
|
/// > length calculation whenever this method is called.
|
||||||
///
|
///
|
||||||
/// [`to_bytes`]: #method.to_bytes
|
/// [`to_bytes`]: #method.to_bytes
|
||||||
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn to_bytes_with_nul(&self) -> &[u8] {
|
pub fn to_bytes_with_nul(&self) -> &[u8] {
|
||||||
unsafe { mem::transmute(&self.inner) }
|
unsafe { mem::transmute(&self.inner) }
|
||||||
|
@ -908,6 +921,7 @@ impl ops::Index<ops::RangeFull> for CString {
|
||||||
|
|
||||||
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
||||||
impl AsRef<CStr> for CStr {
|
impl AsRef<CStr> for CStr {
|
||||||
|
#[inline]
|
||||||
fn as_ref(&self) -> &CStr {
|
fn as_ref(&self) -> &CStr {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -915,6 +929,7 @@ impl AsRef<CStr> for CStr {
|
||||||
|
|
||||||
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
||||||
impl AsRef<CStr> for CString {
|
impl AsRef<CStr> for CString {
|
||||||
|
#[inline]
|
||||||
fn as_ref(&self) -> &CStr {
|
fn as_ref(&self) -> &CStr {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue