Skip to content

Commit

Permalink
Add in as_c_str and AsRef impl (contested in original ACP)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey committed May 30, 2023
1 parent 2835a66 commit 1867de5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,23 @@ impl<'a> CStrBytes<'a> {
// the nul terminator.
unsafe { *self.ptr.as_ref() == 0 }
}

/// Returns the remainder of the string as a slice.
#[unstable(feature = "cstr_bytes", issue = "none")]
#[inline]
pub fn as_c_str(&self) -> &'a CStr {
// SAFETY: We start with a valid C string and never increment beyond the
// nul terminator, retaining a valid C string, even if it's empty.
unsafe { CStr::from_ptr(self.ptr.as_ptr() as *mut u8 as *const c_char) }
}
}

#[unstable(feature = "cstr_bytes", issue = "none")]
impl AsRef<CStr> for CStrBytes<'_> {
#[inline]
fn as_ref(&self) -> &CStr {
self.as_c_str()
}
}

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

0 comments on commit 1867de5

Please sign in to comment.