ffi::c_str smaller as_bytes
This commit is contained in:
parent
84542d22a7
commit
a0ff4612f2
1 changed files with 4 additions and 2 deletions
|
@ -613,7 +613,8 @@ impl CString {
|
||||||
#[inline]
|
#[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]
|
// SAFETY: CString has a length at least 1
|
||||||
|
unsafe { self.inner.get_unchecked(..self.inner.len() - 1) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Equivalent to [`CString::as_bytes()`] except that the
|
/// Equivalent to [`CString::as_bytes()`] except that the
|
||||||
|
@ -1322,7 +1323,8 @@ impl CStr {
|
||||||
#[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();
|
||||||
&bytes[..bytes.len() - 1]
|
// SAFETY: to_bytes_with_nul returns slice with length at least 1
|
||||||
|
unsafe { bytes.get_unchecked(..bytes.len() - 1) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts this C string to a byte slice containing the trailing 0 byte.
|
/// Converts this C string to a byte slice containing the trailing 0 byte.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue