forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.rs
More file actions
56 lines (50 loc) · 1.3 KB
/
windows.rs
File metadata and controls
56 lines (50 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use rustpython_wtf8::Wtf8;
use std::{
ffi::{OsStr, OsString},
os::windows::ffi::{OsStrExt, OsStringExt},
};
/// _MAX_ENV from Windows CRT stdlib.h - maximum environment variable size
pub const _MAX_ENV: usize = 32767;
pub trait ToWideString {
fn to_wide(&self) -> Vec<u16>;
fn to_wide_with_nul(&self) -> Vec<u16>;
}
impl<T> ToWideString for T
where
T: AsRef<OsStr>,
{
fn to_wide(&self) -> Vec<u16> {
self.as_ref().encode_wide().collect()
}
fn to_wide_with_nul(&self) -> Vec<u16> {
self.as_ref().encode_wide().chain(Some(0)).collect()
}
}
impl ToWideString for OsStr {
fn to_wide(&self) -> Vec<u16> {
self.encode_wide().collect()
}
fn to_wide_with_nul(&self) -> Vec<u16> {
self.encode_wide().chain(Some(0)).collect()
}
}
impl ToWideString for Wtf8 {
fn to_wide(&self) -> Vec<u16> {
self.encode_wide().collect()
}
fn to_wide_with_nul(&self) -> Vec<u16> {
self.encode_wide().chain(Some(0)).collect()
}
}
pub trait FromWideString
where
Self: Sized,
{
fn from_wides_until_nul(wide: &[u16]) -> Self;
}
impl FromWideString for OsString {
fn from_wides_until_nul(wide: &[u16]) -> OsString {
let len = wide.iter().take_while(|&&c| c != 0).count();
OsString::from_wide(&wide[..len])
}
}