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}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_strbuf_runtime() {
62 let buf: StrBuf<5> = StrBuf::from_str("hello");
64 assert_eq!(buf.as_str(), "hello");
65 assert_eq!(buf.as_bytes(), b"hello");
66
67 let buf_empty: StrBuf<0> = StrBuf::from_str("");
69 assert_eq!(buf_empty.as_str(), "");
70 assert_eq!(buf_empty.as_bytes(), b"");
71
72 let buf_unicode: StrBuf<9> = StrBuf::from_str("你好!");
74 assert_eq!(buf_unicode.as_str(), "你好!");
75
76 let raw_buf = [b'a', b'b', b'c'];
78 let buf_unsafe: StrBuf<3> = unsafe { StrBuf::new_unchecked(raw_buf) };
79 assert_eq!(buf_unsafe.as_str(), "abc");
80 }
81}