Skip to content
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

Mark smaller CStr and CString functions as #[inline] #42716

Merged
merged 1 commit into from
Jun 20, 2017
Merged
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
15 changes: 15 additions & 0 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ impl CString {
/// let _ = CString::from_raw(ptr);
/// }
/// ```
#[inline]
#[stable(feature = "cstr_memory", since = "1.4.0")]
pub fn into_raw(self) -> *mut c_char {
Box::into_raw(self.into_inner()) as *mut c_char
Expand Down Expand Up @@ -382,6 +383,7 @@ impl CString {
/// let bytes = c_string.as_bytes();
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_bytes(&self) -> &[u8] {
&self.inner[..self.inner.len() - 1]
Expand All @@ -401,6 +403,7 @@ impl CString {
/// let bytes = c_string.as_bytes_with_nul();
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_bytes_with_nul(&self) -> &[u8] {
&self.inner
Expand All @@ -409,6 +412,7 @@ impl CString {
/// Extracts a [`CStr`] slice containing the entire string.
///
/// [`CStr`]: struct.CStr.html
#[inline]
#[unstable(feature = "as_c_str", issue = "40380")]
pub fn as_c_str(&self) -> &CStr {
&*self
Expand Down Expand Up @@ -449,6 +453,7 @@ impl Drop for CString {
impl ops::Deref for CString {
type Target = CStr;

#[inline]
fn deref(&self) -> &CStr {
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
}
Expand All @@ -463,6 +468,7 @@ impl fmt::Debug for CString {

#[stable(feature = "cstring_into", since = "1.7.0")]
impl From<CString> for Vec<u8> {
#[inline]
fn from(s: CString) -> Vec<u8> {
s.into_bytes()
}
Expand Down Expand Up @@ -498,6 +504,7 @@ impl Default for CString {

#[stable(feature = "cstr_borrow", since = "1.3.0")]
impl Borrow<CStr> for CString {
#[inline]
fn borrow(&self) -> &CStr { self }
}

Expand All @@ -511,13 +518,15 @@ impl<'a> From<&'a CStr> for Box<CStr> {

#[stable(feature = "c_string_from_box", since = "1.18.0")]
impl From<Box<CStr>> for CString {
#[inline]
fn from(s: Box<CStr>) -> CString {
s.into_c_string()
}
}

#[stable(feature = "box_from_c_string", since = "1.18.0")]
impl Into<Box<CStr>> for CString {
#[inline]
fn into(self) -> Box<CStr> {
self.into_boxed_c_str()
}
Expand Down Expand Up @@ -730,6 +739,7 @@ impl CStr {
/// assert_eq!(cstr, &*cstring);
/// }
/// ```
#[inline]
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
mem::transmute(bytes)
Expand Down Expand Up @@ -772,6 +782,7 @@ impl CStr {
/// *ptr;
/// }
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_ptr(&self) -> *const c_char {
self.inner.as_ptr()
Expand All @@ -789,6 +800,7 @@ impl CStr {
/// > **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
/// > length calculation whenever this method is called.
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn to_bytes(&self) -> &[u8] {
let bytes = self.to_bytes_with_nul();
Expand All @@ -805,6 +817,7 @@ impl CStr {
/// > length calculation whenever this method is called.
///
/// [`to_bytes`]: #method.to_bytes
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn to_bytes_with_nul(&self) -> &[u8] {
unsafe { mem::transmute(&self.inner) }
Expand Down Expand Up @@ -908,13 +921,15 @@ impl ops::Index<ops::RangeFull> for CString {

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl AsRef<CStr> for CStr {
#[inline]
fn as_ref(&self) -> &CStr {
self
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl AsRef<CStr> for CString {
#[inline]
fn as_ref(&self) -> &CStr {
self
}
Expand Down