@@ -121,10 +121,10 @@ enum FromBytesWithNulErrorKind {
121
121
}
122
122
123
123
impl FromBytesWithNulError {
124
- fn interior_nul ( pos : usize ) -> FromBytesWithNulError {
124
+ const fn interior_nul ( pos : usize ) -> FromBytesWithNulError {
125
125
FromBytesWithNulError { kind : FromBytesWithNulErrorKind :: InteriorNul ( pos) }
126
126
}
127
- fn not_nul_terminated ( ) -> FromBytesWithNulError {
127
+ const fn not_nul_terminated ( ) -> FromBytesWithNulError {
128
128
FromBytesWithNulError { kind : FromBytesWithNulErrorKind :: NotNulTerminated }
129
129
}
130
130
@@ -299,7 +299,8 @@ impl CStr {
299
299
/// ```
300
300
///
301
301
#[ unstable( feature = "cstr_from_bytes_until_nul" , issue = "95027" ) ]
302
- pub fn from_bytes_until_nul ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesUntilNulError > {
302
+ #[ rustc_const_unstable( feature = "cstr_from_bytes_until_nul" , issue = "95027" ) ]
303
+ pub const fn from_bytes_until_nul ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesUntilNulError > {
303
304
let nul_pos = memchr:: memchr ( 0 , bytes) ;
304
305
match nul_pos {
305
306
Some ( nul_pos) => {
@@ -348,7 +349,8 @@ impl CStr {
348
349
/// assert!(cstr.is_err());
349
350
/// ```
350
351
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
351
- pub fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
352
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
353
+ pub const fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
352
354
let nul_pos = memchr:: memchr ( 0 , bytes) ;
353
355
match nul_pos {
354
356
Some ( nul_pos) if nul_pos + 1 == bytes. len ( ) => {
@@ -497,7 +499,8 @@ impl CStr {
497
499
#[ must_use = "this returns the result of the operation, \
498
500
without modifying the original"]
499
501
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
500
- pub fn to_bytes ( & self ) -> & [ u8 ] {
502
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
503
+ pub const fn to_bytes ( & self ) -> & [ u8 ] {
501
504
let bytes = self . to_bytes_with_nul ( ) ;
502
505
// SAFETY: to_bytes_with_nul returns slice with length at least 1
503
506
unsafe { bytes. get_unchecked ( ..bytes. len ( ) - 1 ) }
@@ -524,7 +527,8 @@ impl CStr {
524
527
#[ must_use = "this returns the result of the operation, \
525
528
without modifying the original"]
526
529
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
527
- pub fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
530
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
531
+ pub const fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
528
532
// SAFETY: Transmuting a slice of `c_char`s to a slice of `u8`s
529
533
// is safe on all supported targets.
530
534
unsafe { & * ( & self . inner as * const [ c_char ] as * const [ u8 ] ) }
@@ -547,7 +551,8 @@ impl CStr {
547
551
/// assert_eq!(cstr.to_str(), Ok("foo"));
548
552
/// ```
549
553
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
550
- pub fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
554
+ #[ rustc_const_unstable( feature = "const_cstr_methods" , issue = "101719" ) ]
555
+ pub const fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
551
556
// N.B., when `CStr` is changed to perform the length check in `.to_bytes()`
552
557
// instead of in `from_ptr()`, it may be worth considering if this should
553
558
// be rewritten to do the UTF-8 check inline with the length calculation
0 commit comments