#![allow(unsafe_code)]
pub struct StrBuf<const N: usize>([u8; N]);
impl<const N: usize> StrBuf<N> {
pub const unsafe fn new_unchecked(buf: [u8; N]) -> Self {
#[cfg(debug_assertions)]
{
assert!(core::str::from_utf8(&buf).is_ok())
}
Self(buf)
}
pub const fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.0) }
}
pub const fn as_bytes(&self) -> &[u8] {
&self.0
}
pub const fn from_str(s: &str) -> Self {
let buf = crate::bytes::clone::<N>(s.as_bytes());
unsafe { Self::new_unchecked(buf) }
}
}
#[macro_export]
macro_rules! from_utf8 {
($s: expr) => {{
use ::core::primitive::str;
const OUTPUT: &str = match ::core::str::from_utf8($s) {
Ok(s) => s,
Err(_) => panic!("invalid utf-8 bytes"),
};
OUTPUT
}};
}