Skip to content

Commit 1867de5

Browse files
committed
Add in as_c_str and AsRef impl (contested in original ACP)
1 parent 2835a66 commit 1867de5

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

library/core/src/ffi/c_str.rs

+17
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,23 @@ impl<'a> CStrBytes<'a> {
731731
// the nul terminator.
732732
unsafe { *self.ptr.as_ref() == 0 }
733733
}
734+
735+
/// Returns the remainder of the string as a slice.
736+
#[unstable(feature = "cstr_bytes", issue = "none")]
737+
#[inline]
738+
pub fn as_c_str(&self) -> &'a CStr {
739+
// SAFETY: We start with a valid C string and never increment beyond the
740+
// nul terminator, retaining a valid C string, even if it's empty.
741+
unsafe { CStr::from_ptr(self.ptr.as_ptr() as *mut u8 as *const c_char) }
742+
}
743+
}
744+
745+
#[unstable(feature = "cstr_bytes", issue = "none")]
746+
impl AsRef<CStr> for CStrBytes<'_> {
747+
#[inline]
748+
fn as_ref(&self) -> &CStr {
749+
self.as_c_str()
750+
}
734751
}
735752

736753
#[unstable(feature = "cstr_bytes", issue = "none")]

0 commit comments

Comments
 (0)