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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::ffi::OsStr;
use std::ffi::OsString;
use std::os::windows::ffi::OsStrExt;
use std::path::Path;
use std::path::PathBuf;

/// Implemented for types that can be converted into wide char arrays.
pub trait AsWide {
    type Iter: Iterator<Item = u16>;

    /// Get an iterator over wide chars.
    ///
    /// Depending on where this is passed,
    /// it is a logic error to have a NUL wide char.
    fn as_wide(&self) -> Self::Iter;
}

impl<'a> AsWide for &'a str {
    type Iter = std::str::EncodeUtf16<'a>;

    fn as_wide(&self) -> Self::Iter {
        self.encode_utf16()
    }
}

impl<'a> AsWide for &'a String {
    type Iter = std::str::EncodeUtf16<'a>;

    fn as_wide(&self) -> Self::Iter {
        self.as_str().encode_utf16()
    }
}

impl<'a> AsWide for &'a [u16] {
    type Iter = std::iter::Copied<std::slice::Iter<'a, u16>>;

    fn as_wide(&self) -> Self::Iter {
        self.iter().copied()
    }
}

impl<'a> AsWide for &'a Vec<u16> {
    type Iter = std::iter::Copied<std::slice::Iter<'a, u16>>;

    fn as_wide(&self) -> Self::Iter {
        self.iter().copied()
    }
}

impl<'a> AsWide for &'a OsStr {
    type Iter = std::os::windows::ffi::EncodeWide<'a>;

    fn as_wide(&self) -> Self::Iter {
        self.encode_wide()
    }
}

impl<'a> AsWide for &'a OsString {
    type Iter = std::os::windows::ffi::EncodeWide<'a>;

    fn as_wide(&self) -> Self::Iter {
        self.encode_wide()
    }
}

impl<'a> AsWide for &'a Path {
    type Iter = std::os::windows::ffi::EncodeWide<'a>;

    fn as_wide(&self) -> Self::Iter {
        self.as_os_str().encode_wide()
    }
}

impl<'a> AsWide for &'a PathBuf {
    type Iter = std::os::windows::ffi::EncodeWide<'a>;

    fn as_wide(&self) -> Self::Iter {
        self.as_os_str().encode_wide()
    }
}