rustpython_common/
windows.rs

1use std::{
2    ffi::{OsStr, OsString},
3    os::windows::ffi::{OsStrExt, OsStringExt},
4};
5
6pub trait ToWideString {
7    fn to_wide(&self) -> Vec<u16>;
8    fn to_wide_with_nul(&self) -> Vec<u16>;
9}
10impl<T> ToWideString for T
11where
12    T: AsRef<OsStr>,
13{
14    fn to_wide(&self) -> Vec<u16> {
15        self.as_ref().encode_wide().collect()
16    }
17    fn to_wide_with_nul(&self) -> Vec<u16> {
18        self.as_ref().encode_wide().chain(Some(0)).collect()
19    }
20}
21
22pub trait FromWideString
23where
24    Self: Sized,
25{
26    fn from_wides_until_nul(wide: &[u16]) -> Self;
27}
28impl FromWideString for OsString {
29    fn from_wides_until_nul(wide: &[u16]) -> OsString {
30        let len = wide.iter().take_while(|&&c| c != 0).count();
31        OsString::from_wide(&wide[..len])
32    }
33}