11//! impl char {}
22
33use super :: * ;
4- use crate :: intrinsics :: const_eval_select ;
4+ use crate :: macros :: const_panic ;
55use crate :: slice;
66use crate :: str:: from_utf8_unchecked_mut;
77use crate :: unicode:: printable:: is_printable;
@@ -1773,17 +1773,7 @@ const fn len_utf16(code: u32) -> usize {
17731773#[ cfg_attr( bootstrap, rustc_const_stable( feature = "const_char_encode_utf8" , since = "1.83.0" ) ) ]
17741774#[ doc( hidden) ]
17751775#[ inline]
1776- #[ rustc_allow_const_fn_unstable( const_eval_select) ]
17771776pub const fn encode_utf8_raw ( code : u32 , dst : & mut [ u8 ] ) -> & mut [ u8 ] {
1778- const fn panic_at_const ( _code : u32 , _len : usize , _dst_len : usize ) {
1779- // Note that we cannot format in constant expressions.
1780- panic ! ( "encode_utf8: buffer does not have enough bytes to encode code point" ) ;
1781- }
1782- fn panic_at_rt ( code : u32 , len : usize , dst_len : usize ) {
1783- panic ! (
1784- "encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1785- ) ;
1786- }
17871777 let len = len_utf8 ( code) ;
17881778 match ( len, & mut * dst) {
17891779 ( 1 , [ a, ..] ) => {
@@ -1804,8 +1794,15 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
18041794 * c = ( code >> 6 & 0x3F ) as u8 | TAG_CONT ;
18051795 * d = ( code & 0x3F ) as u8 | TAG_CONT ;
18061796 }
1807- // FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
1808- _ => const_eval_select ( ( code, len, dst. len ( ) ) , panic_at_const, panic_at_rt) ,
1797+ _ => {
1798+ const_panic ! (
1799+ "encode_utf8: buffer does not have enough bytes to encode code point" ,
1800+ "encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1801+ code: u32 = code,
1802+ len: usize = len,
1803+ dst_len: usize = dst. len( ) ,
1804+ )
1805+ }
18091806 } ;
18101807 // SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
18111808 unsafe { slice:: from_raw_parts_mut ( dst. as_mut_ptr ( ) , len) }
@@ -1826,15 +1823,6 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
18261823#[ doc( hidden) ]
18271824#[ inline]
18281825pub const fn encode_utf16_raw ( mut code : u32 , dst : & mut [ u16 ] ) -> & mut [ u16 ] {
1829- const fn panic_at_const ( _code : u32 , _len : usize , _dst_len : usize ) {
1830- // Note that we cannot format in constant expressions.
1831- panic ! ( "encode_utf16: buffer does not have enough bytes to encode code point" ) ;
1832- }
1833- fn panic_at_rt ( code : u32 , len : usize , dst_len : usize ) {
1834- panic ! (
1835- "encode_utf16: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1836- ) ;
1837- }
18381826 let len = len_utf16 ( code) ;
18391827 match ( len, & mut * dst) {
18401828 ( 1 , [ a, ..] ) => {
@@ -1845,8 +1833,15 @@ pub const fn encode_utf16_raw(mut code: u32, dst: &mut [u16]) -> &mut [u16] {
18451833 * a = ( code >> 10 ) as u16 | 0xD800 ;
18461834 * b = ( code & 0x3FF ) as u16 | 0xDC00 ;
18471835 }
1848- // FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
1849- _ => const_eval_select ( ( code, len, dst. len ( ) ) , panic_at_const, panic_at_rt) ,
1836+ _ => {
1837+ const_panic ! (
1838+ "encode_utf16: buffer does not have enough bytes to encode code point" ,
1839+ "encode_utf16: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1840+ code: u32 = code,
1841+ len: usize = len,
1842+ dst_len: usize = dst. len( ) ,
1843+ )
1844+ }
18501845 } ;
18511846 // SAFETY: `<&mut [u16]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
18521847 unsafe { slice:: from_raw_parts_mut ( dst. as_mut_ptr ( ) , len) }
0 commit comments