Skip to content

Commit a0ff461

Browse files
committed
ffi::c_str smaller as_bytes
1 parent 84542d2 commit a0ff461

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

library/std/src/ffi/c_str.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ impl CString {
613613
#[inline]
614614
#[stable(feature = "rust1", since = "1.0.0")]
615615
pub fn as_bytes(&self) -> &[u8] {
616-
&self.inner[..self.inner.len() - 1]
616+
// SAFETY: CString has a length at least 1
617+
unsafe { self.inner.get_unchecked(..self.inner.len() - 1) }
617618
}
618619

619620
/// Equivalent to [`CString::as_bytes()`] except that the
@@ -1322,7 +1323,8 @@ impl CStr {
13221323
#[stable(feature = "rust1", since = "1.0.0")]
13231324
pub fn to_bytes(&self) -> &[u8] {
13241325
let bytes = self.to_bytes_with_nul();
1325-
&bytes[..bytes.len() - 1]
1326+
// SAFETY: to_bytes_with_nul returns slice with length at least 1
1327+
unsafe { bytes.get_unchecked(..bytes.len() - 1) }
13261328
}
13271329

13281330
/// Converts this C string to a byte slice containing the trailing 0 byte.

0 commit comments

Comments
 (0)