hadris_common/str/
utf16.rs1use alloc::string::String;
2use alloc::vec::Vec;
3
4use crate::types::{
5 endian::{Endian, LittleEndian},
6 number::U16,
7};
8
9#[repr(C)]
10#[derive(Debug, Clone, Copy)]
11pub struct FixedUtf16Str<const N: usize> {
12 data: [U16<LittleEndian>; N],
13}
14
15impl<const N: usize> FixedUtf16Str<N> {
16 pub fn to_string(&self) -> Result<String, ()> {
17 let data = self.data.iter().map(|c| c.get() as u8).collect::<Vec<u8>>();
19 String::from_utf8(data).map_err(|_| ())
20 }
21}
22
23#[cfg(feature = "bytemuck")]
24unsafe impl<const N: usize> bytemuck::Pod for FixedUtf16Str<N> {}
25#[cfg(feature = "bytemuck")]
26unsafe impl<const N: usize> bytemuck::Zeroable for FixedUtf16Str<N> {}