use core::{fmt::Debug, ops::Deref};
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct SizedString<const N: usize>([u8; N]);
impl<const N: usize> SizedString<N> {
#[must_use]
pub const fn new(bytes: [u8; N]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.0) }
}
}
impl<const N: usize> Deref for SizedString<N> {
type Target = str;
fn deref(&self) -> &Self::Target {
unsafe { core::str::from_utf8_unchecked(&self.0) }
}
}
impl<const N: usize> AsRef<str> for SizedString<N> {
fn as_ref(&self) -> &str {
self
}
}
impl<const N: usize, T> AsRef<T> for SizedString<N>
where
str: core::convert::AsRef<T>,
{
fn as_ref(&self) -> &T {
let s: &str = self.as_ref();
s.as_ref()
}
}
impl<const N: usize> Debug for SizedString<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("SizedString").field(&self.as_str()).finish()
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_sized_string() {
let s = quork_proc::sized_string!("hello world");
let s_str: &str = s.as_ref();
assert_eq!(s.len(), 11);
assert_eq!(s_str, "hello world");
}
}