Skip to main content

hadris_common/str/
utf16.rs

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