1use super::*;
2
3/// A pointer to a constant null-terminated string of 16-bit Unicode characters.
4#[repr(transparent)]
5#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6pub struct PCWSTR(pub *const u16);
7
8impl PCWSTR {
9 /// Construct a new `PCWSTR` from a raw pointer
10 pub const fn from_raw(ptr: *const u16) -> Self {
11 Self(ptr)
12 }
13
14 /// Construct a null `PCWSTR`
15 pub const fn null() -> Self {
16 Self(std::ptr::null())
17 }
18
19 /// Returns a raw pointer to the `PCWSTR`
20 pub const fn as_ptr(&self) -> *const u16 {
21 self.0
22 }
23
24 /// Checks whether the `PCWSTR` is null
25 pub fn is_null(&self) -> bool {
26 self.0.is_null()
27 }
28
29 /// String data without the trailing 0
30 ///
31 /// # Safety
32 ///
33 /// The `PCWSTR`'s pointer needs to be valid for reads up until and including the next `\0`.
34 pub unsafe fn as_wide(&self) -> &[u16] {
35 let len = super::wcslen(*self);
36 std::slice::from_raw_parts(self.0, len)
37 }
38
39 /// Copy the `PCWSTR` into a Rust `String`.
40 ///
41 /// # Safety
42 ///
43 /// See the safety information for `PCWSTR::as_wide`.
44 pub unsafe fn to_string(&self) -> std::result::Result<String, std::string::FromUtf16Error> {
45 String::from_utf16(self.as_wide())
46 }
47
48 /// Copy the `PCWSTR` into an `HSTRING`.
49 ///
50 /// # Safety
51 ///
52 /// See the safety information for `PCWSTR::as_wide`.
53 pub unsafe fn to_hstring(&self) -> Result<HSTRING> {
54 HSTRING::from_wide(self.as_wide())
55 }
56
57 /// Allow this string to be displayed.
58 ///
59 /// # Safety
60 ///
61 /// See the safety information for `PCWSTR::as_wide`.
62 pub unsafe fn display(&self) -> impl std::fmt::Display + '_ {
63 Decode(move || std::char::decode_utf16(self.as_wide().iter().cloned()))
64 }
65}
66
67impl TypeKind for PCWSTR {
68 type TypeKind = CopyType;
69}
70