const_str/__ctfe/
to_char_array.rs

1pub struct ToCharArray<T>(pub T);
2
3impl ToCharArray<&str> {
4    pub const fn output_len(&self) -> usize {
5        crate::utf8::str_count_chars(self.0)
6    }
7
8    pub const fn const_eval<const N: usize>(&self) -> [char; N] {
9        crate::utf8::str_chars(self.0)
10    }
11}
12
13/// Converts a string slice into an array of its characters.
14///
15/// This macro is [const-context only](./index.html#const-context-only).
16///
17/// # Examples
18/// ```
19/// const CHARS: [char; 5] = const_str::to_char_array!("Hello");
20/// assert_eq!(CHARS, ['H', 'e', 'l', 'l', 'o']);
21/// ```
22///
23#[macro_export]
24macro_rules! to_char_array {
25    ($s: expr) => {{
26        const OUTPUT_LEN: usize = $crate::__ctfe::ToCharArray($s).output_len();
27        const OUTPUT_BUF: [char; OUTPUT_LEN] =
28            $crate::__ctfe::ToCharArray($s).const_eval::<OUTPUT_LEN>();
29        OUTPUT_BUF
30    }};
31}