add wide string format helper

This commit is contained in:
Numbers
2024-05-08 16:49:08 -04:00
parent 15caad3e4a
commit a1379c2696
2 changed files with 30 additions and 1 deletions

View File

@@ -40,4 +40,7 @@ mod time;
pub use time::dur;
mod hash;
pub use hash::*;
pub use hash::*;
mod strings;
pub use strings::*;

26
src/strings.rs Normal file
View File

@@ -0,0 +1,26 @@
use core::fmt::{Debug, Display, Error, Formatter, Write};
use core::ops::{ControlFlow, Deref};
#[repr(transparent)]
pub struct Wide<'a>(pub &'a [u16]);
impl<'a> Display for Wide<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let result = char::decode_utf16(self.0.iter().cloned())
.map(|a| a.unwrap_or('?'))
.try_for_each(|c| match f.write_char(c) {
Ok(_) => ControlFlow::Continue(()),
Err(e) => ControlFlow::Break(e),
});
match result {
ControlFlow::Continue(_) => Ok(()),
ControlFlow::Break(e) => Err(e)
}
}
}