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
use super::*;

#[repr(transparent)]
pub struct BasicString(*const u16);

impl BasicString {
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        if self.0.is_null() {
            0
        } else {
            unsafe { SysStringLen(self.0) as usize }
        }
    }

    pub fn as_wide(&self) -> &[u16] {
        let len = self.len();
        if len != 0 {
            unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
        } else {
            &[]
        }
    }

    pub fn as_ptr(&self) -> *const u16 {
        if !self.is_empty() {
            self.0
        } else {
            const EMPTY: [u16; 1] = [0];
            EMPTY.as_ptr()
        }
    }
}

impl Default for BasicString {
    fn default() -> Self {
        Self(core::ptr::null_mut())
    }
}

impl Drop for BasicString {
    fn drop(&mut self) {
        if !self.0.is_null() {
            unsafe { SysFreeString(self.0) }
        }
    }
}