@@ -241,7 +241,7 @@ impl CStr {
241
241
/// ```
242
242
///
243
243
/// ```
244
- /// #![feature(const_cstr_methods )]
244
+ /// #![feature(const_cstr_from_ptr )]
245
245
///
246
246
/// use std::ffi::{c_char, CStr};
247
247
///
@@ -256,7 +256,7 @@ impl CStr {
256
256
#[ inline]
257
257
#[ must_use]
258
258
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
259
- #[ rustc_const_unstable( feature = "const_cstr_methods " , issue = "101719" ) ]
259
+ #[ rustc_const_unstable( feature = "const_cstr_from_ptr " , issue = "101719" ) ]
260
260
pub const unsafe fn from_ptr < ' a > ( ptr : * const c_char ) -> & ' a CStr {
261
261
// SAFETY: The caller has provided a pointer that points to a valid C
262
262
// string with a NUL terminator of size less than `isize::MAX`, whose
@@ -377,7 +377,7 @@ impl CStr {
377
377
/// assert!(cstr.is_err());
378
378
/// ```
379
379
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
380
- #[ rustc_const_unstable ( feature = "const_cstr_methods" , issue = "101719 " ) ]
380
+ #[ rustc_const_stable ( feature = "const_cstr_methods" , since = "CURRENT_RUSTC_VERSION " ) ]
381
381
pub const fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
382
382
let nul_pos = memchr:: memchr ( 0 , bytes) ;
383
383
match nul_pos {
@@ -561,10 +561,12 @@ impl CStr {
561
561
#[ must_use = "this returns the result of the operation, \
562
562
without modifying the original"]
563
563
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
564
- pub fn to_bytes ( & self ) -> & [ u8 ] {
564
+ #[ rustc_const_stable( feature = "const_cstr_methods" , since = "CURRENT_RUSTC_VERSION" ) ]
565
+ pub const fn to_bytes ( & self ) -> & [ u8 ] {
565
566
let bytes = self . to_bytes_with_nul ( ) ;
567
+ // FIXME(const-hack) replace with range index
566
568
// SAFETY: to_bytes_with_nul returns slice with length at least 1
567
- unsafe { bytes. get_unchecked ( .. bytes. len ( ) - 1 ) }
569
+ unsafe { slice :: from_raw_parts ( bytes. as_ptr ( ) , bytes. len ( ) - 1 ) }
568
570
}
569
571
570
572
/// Converts this C string to a byte slice containing the trailing 0 byte.
@@ -588,7 +590,7 @@ impl CStr {
588
590
#[ must_use = "this returns the result of the operation, \
589
591
without modifying the original"]
590
592
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
591
- #[ rustc_const_unstable ( feature = "const_cstr_methods" , issue = "101719 " ) ]
593
+ #[ rustc_const_stable ( feature = "const_cstr_methods" , since = "CURRENT_RUSTC_VERSION " ) ]
592
594
pub const fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
593
595
// SAFETY: Transmuting a slice of `c_char`s to a slice of `u8`s
594
596
// is safe on all supported targets.
@@ -612,7 +614,8 @@ impl CStr {
612
614
/// assert_eq!(cstr.to_str(), Ok("foo"));
613
615
/// ```
614
616
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
615
- pub fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
617
+ #[ rustc_const_stable( feature = "const_cstr_methods" , since = "CURRENT_RUSTC_VERSION" ) ]
618
+ pub const fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
616
619
// N.B., when `CStr` is changed to perform the length check in `.to_bytes()`
617
620
// instead of in `from_ptr()`, it may be worth considering if this should
618
621
// be rewritten to do the UTF-8 check inline with the length calculation
0 commit comments