Change 'from_bytes_until_nul' to const stable

This commit is contained in:
Trevor Gross 2023-01-30 18:11:03 -05:00
parent 83b05ef0ee
commit 877e9f5d3a
2 changed files with 14 additions and 4 deletions

View File

@ -320,8 +320,9 @@ fn strlen_rt(s: *const c_char) -> usize {
/// assert_eq!(c_str.to_str().unwrap(), "AAAAAAAA"); /// assert_eq!(c_str.to_str().unwrap(), "AAAAAAAA");
/// ``` /// ```
/// ///
#[rustc_allow_const_fn_unstable(const_slice_index)]
#[stable(feature = "cstr_from_bytes_until_nul", since = "CURRENT_RUSTC_VERSION")] #[stable(feature = "cstr_from_bytes_until_nul", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_cstr_from_bytes_until_nul", issue = "95027")] #[rustc_const_stable(feature = "cstr_from_bytes_until_nul", since = "CURRENT_RUSTC_VERSION")]
pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError> { pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError> {
let nul_pos = memchr::memchr(0, bytes); let nul_pos = memchr::memchr(0, bytes);
match nul_pos { match nul_pos {

View File

@ -16,25 +16,29 @@
/// bytes where the borrow propagated all the way to the most significant /// bytes where the borrow propagated all the way to the most significant
/// bit." /// bit."
#[inline] #[inline]
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
const fn contains_zero_byte(x: usize) -> bool { const fn contains_zero_byte(x: usize) -> bool {
x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
} }
#[cfg(target_pointer_width = "16")]
#[inline] #[inline]
#[cfg(target_pointer_width = "16")]
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
const fn repeat_byte(b: u8) -> usize { const fn repeat_byte(b: u8) -> usize {
(b as usize) << 8 | b as usize (b as usize) << 8 | b as usize
} }
#[cfg(not(target_pointer_width = "16"))]
#[inline] #[inline]
#[cfg(not(target_pointer_width = "16"))]
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
const fn repeat_byte(b: u8) -> usize { const fn repeat_byte(b: u8) -> usize {
(b as usize) * (usize::MAX / 255) (b as usize) * (usize::MAX / 255)
} }
/// Returns the first index matching the byte `x` in `text`. /// Returns the first index matching the byte `x` in `text`.
#[must_use]
#[inline] #[inline]
#[must_use]
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
pub const fn memchr(x: u8, text: &[u8]) -> Option<usize> { pub const fn memchr(x: u8, text: &[u8]) -> Option<usize> {
// Fast path for small slices. // Fast path for small slices.
if text.len() < 2 * USIZE_BYTES { if text.len() < 2 * USIZE_BYTES {
@ -45,6 +49,7 @@ pub const fn memchr(x: u8, text: &[u8]) -> Option<usize> {
} }
#[inline] #[inline]
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
const fn memchr_naive(x: u8, text: &[u8]) -> Option<usize> { const fn memchr_naive(x: u8, text: &[u8]) -> Option<usize> {
let mut i = 0; let mut i = 0;
@ -60,6 +65,10 @@ const fn memchr_naive(x: u8, text: &[u8]) -> Option<usize> {
None None
} }
#[rustc_allow_const_fn_unstable(const_cmp)]
#[rustc_allow_const_fn_unstable(const_slice_index)]
#[rustc_allow_const_fn_unstable(const_align_offset)]
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> { const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> {
// Scan for a single byte value by reading two `usize` words at a time. // Scan for a single byte value by reading two `usize` words at a time.
// //