Debian Codegraph @

third_party/rust/windows-result/src/bstr.rs

raw
1 use super::*;
2
3 #[repr(transparent)]
4 pub struct BasicString(*const u16);
5
6 impl BasicString {
7 pub fn is_empty(&self) -> bool {
8 self.len() == 0
9 }
10
11 pub fn len(&self) -> usize {
12 if self.0.is_null() {
13 0
14 } else {
15 unsafe { SysStringLen(self.0) as usize }
16 }
17 }
18
19 pub fn as_wide(&self) -> &[u16] {
20 let len = self.len();
21 if len != 0 {
22 unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
23 } else {
24 &[]
25 }
26 }
27
28 pub fn as_ptr(&self) -> *const u16 {
29 if !self.is_empty() {
30 self.0
31 } else {
32 const EMPTY: [u16; 1] = [0];
33 EMPTY.as_ptr()
34 }
35 }
36 }
37
38 impl Default for BasicString {
39 fn default() -> Self {
40 Self(core::ptr::null_mut())
41 }
42 }
43
44 impl Drop for BasicString {
45 fn drop(&mut self) {
46 if !self.0.is_null() {
47 unsafe { SysFreeString(self.0) }
48 }
49 }
50 }