@@ -120,10 +120,10 @@ enum FromBytesWithNulErrorKind {
120
120
}
121
121
122
122
impl FromBytesWithNulError {
123
- fn interior_nul ( pos : usize ) -> FromBytesWithNulError {
123
+ const fn interior_nul ( pos : usize ) -> FromBytesWithNulError {
124
124
FromBytesWithNulError { kind : FromBytesWithNulErrorKind :: InteriorNul ( pos) }
125
125
}
126
- fn not_nul_terminated ( ) -> FromBytesWithNulError {
126
+ const fn not_nul_terminated ( ) -> FromBytesWithNulError {
127
127
FromBytesWithNulError { kind : FromBytesWithNulErrorKind :: NotNulTerminated }
128
128
}
129
129
@@ -294,7 +294,8 @@ impl CStr {
294
294
/// ```
295
295
///
296
296
#[ unstable( feature = "cstr_from_bytes_until_nul" , issue = "95027" ) ]
297
- pub fn from_bytes_until_nul ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesUntilNulError > {
297
+ #[ rustc_const_unstable( feature = "cstr_from_bytes_until_nul" , issue = "95027" ) ]
298
+ pub const fn from_bytes_until_nul ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesUntilNulError > {
298
299
let nul_pos = memchr:: memchr ( 0 , bytes) ;
299
300
match nul_pos {
300
301
Some ( nul_pos) => {
@@ -343,7 +344,8 @@ impl CStr {
343
344
/// assert!(cstr.is_err());
344
345
/// ```
345
346
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
346
- pub fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
347
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
348
+ pub const fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
347
349
let nul_pos = memchr:: memchr ( 0 , bytes) ;
348
350
match nul_pos {
349
351
Some ( nul_pos) if nul_pos + 1 == bytes. len ( ) => {
@@ -493,7 +495,8 @@ impl CStr {
493
495
#[ must_use = "this returns the result of the operation, \
494
496
without modifying the original"]
495
497
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
496
- pub fn to_bytes ( & self ) -> & [ u8 ] {
498
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
499
+ pub const fn to_bytes ( & self ) -> & [ u8 ] {
497
500
let bytes = self . to_bytes_with_nul ( ) ;
498
501
// SAFETY: to_bytes_with_nul returns slice with length at least 1
499
502
unsafe { bytes. get_unchecked ( ..bytes. len ( ) - 1 ) }
@@ -520,7 +523,8 @@ impl CStr {
520
523
#[ must_use = "this returns the result of the operation, \
521
524
without modifying the original"]
522
525
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
523
- pub fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
526
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
527
+ pub const fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
524
528
// SAFETY: Transmuting a slice of `c_char`s to a slice of `u8`s
525
529
// is safe on all supported targets.
526
530
unsafe { & * ( & self . inner as * const [ c_char ] as * const [ u8 ] ) }
@@ -543,7 +547,8 @@ impl CStr {
543
547
/// assert_eq!(cstr.to_str(), Ok("foo"));
544
548
/// ```
545
549
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
546
- pub fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
550
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
551
+ pub const fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
547
552
// N.B., when `CStr` is changed to perform the length check in `.to_bytes()`
548
553
// instead of in `from_ptr()`, it may be worth considering if this should
549
554
// be rewritten to do the UTF-8 check inline with the length calculation
0 commit comments