1#![allow(unsafe_code)]
2
3pub struct StrBuf<const N: usize>([u8; N]);
4
5impl<const N: usize> StrBuf<N> {
6 pub const unsafe fn new_unchecked(buf: [u8; N]) -> Self {
9 #[cfg(debug_assertions)]
10 {
11 assert!(core::str::from_utf8(&buf).is_ok())
12 }
13 Self(buf)
14 }
15
16 pub const fn as_str(&self) -> &str {
18 unsafe { core::str::from_utf8_unchecked(&self.0) }
19 }
20
21 pub const fn as_bytes(&self) -> &[u8] {
22 &self.0
23 }
24
25 pub const fn from_str(s: &str) -> Self {
26 let buf = crate::bytes::clone::<N>(s.as_bytes());
27 unsafe { Self::new_unchecked(buf) }
28 }
29}
30
31#[macro_export]
44macro_rules! from_utf8 {
45 ($s: expr) => {{
46 use ::core::primitive::str;
47 const OUTPUT: &str = match ::core::str::from_utf8($s) {
49 Ok(s) => s,
50 Err(_) => panic!("invalid utf-8 bytes"),
51 };
52 OUTPUT
53 }};
54}