1
1
use super :: * ;
2
+ use core:: ops:: Deref ;
2
3
3
4
/// An ([HSTRING](https://docs.microsoft.com/en-us/windows/win32/winrt/hstring))
4
5
/// is a reference-counted and immutable UTF-16 string type.
@@ -13,50 +14,20 @@ impl HSTRING {
13
14
Self ( core:: ptr:: null_mut ( ) )
14
15
}
15
16
16
- /// Returns `true` if the string is empty.
17
- pub fn is_empty ( & self ) -> bool {
18
- // An empty HSTRING is represented by a null pointer.
19
- self . 0 . is_null ( )
20
- }
21
-
22
- /// Returns the length of the string. The length is measured in `u16`s (UTF-16 code units), not including the terminating null character.
23
- pub fn len ( & self ) -> usize {
24
- if let Some ( header) = self . as_header ( ) {
25
- header. len as usize
26
- } else {
27
- 0
28
- }
29
- }
30
-
31
- /// Get the string as 16-bit wide characters (wchars).
32
- pub fn as_wide ( & self ) -> & [ u16 ] {
33
- unsafe { core:: slice:: from_raw_parts ( self . as_ptr ( ) , self . len ( ) ) }
34
- }
35
-
36
- /// Returns a raw pointer to the `HSTRING` buffer.
37
- pub fn as_ptr ( & self ) -> * const u16 {
38
- if let Some ( header) = self . as_header ( ) {
39
- header. data
40
- } else {
41
- const EMPTY : [ u16 ; 1 ] = [ 0 ] ;
42
- EMPTY . as_ptr ( )
43
- }
44
- }
45
-
46
17
/// Create a `HSTRING` from a slice of 16 bit characters (wchars).
47
18
pub fn from_wide ( value : & [ u16 ] ) -> Self {
48
19
unsafe { Self :: from_wide_iter ( value. iter ( ) . copied ( ) , value. len ( ) ) }
49
20
}
50
21
51
22
/// Get the contents of this `HSTRING` as a String lossily.
52
23
pub fn to_string_lossy ( & self ) -> String {
53
- String :: from_utf16_lossy ( self . as_wide ( ) )
24
+ String :: from_utf16_lossy ( self )
54
25
}
55
26
56
27
/// Get the contents of this `HSTRING` as a OsString.
57
28
#[ cfg( feature = "std" ) ]
58
29
pub fn to_os_string ( & self ) -> std:: ffi:: OsString {
59
- std:: os:: windows:: ffi:: OsStringExt :: from_wide ( self . as_wide ( ) )
30
+ std:: os:: windows:: ffi:: OsStringExt :: from_wide ( self )
60
31
}
61
32
62
33
/// # Safety
@@ -87,6 +58,21 @@ impl HSTRING {
87
58
}
88
59
}
89
60
61
+ impl Deref for HSTRING {
62
+ type Target = [ u16 ] ;
63
+
64
+ fn deref ( & self ) -> & [ u16 ] {
65
+ if let Some ( header) = self . as_header ( ) {
66
+ unsafe { core:: slice:: from_raw_parts ( header. data , header. len as usize ) }
67
+ } else {
68
+ // This ensures that if `as_ptr` is called on the slice that the resulting pointer
69
+ // will still refer to a null-terminated string.
70
+ const EMPTY : [ u16 ; 1 ] = [ 0 ] ;
71
+ & EMPTY [ ..0 ]
72
+ }
73
+ }
74
+ }
75
+
90
76
impl Default for HSTRING {
91
77
fn default ( ) -> Self {
92
78
Self :: new ( )
@@ -125,7 +111,7 @@ impl core::fmt::Display for HSTRING {
125
111
write ! (
126
112
f,
127
113
"{}" ,
128
- Decode ( || core:: char :: decode_utf16( self . as_wide ( ) . iter( ) . cloned( ) ) )
114
+ Decode ( || core:: char :: decode_utf16( self . iter( ) . cloned( ) ) )
129
115
)
130
116
}
131
117
}
@@ -191,13 +177,13 @@ impl Eq for HSTRING {}
191
177
192
178
impl Ord for HSTRING {
193
179
fn cmp ( & self , other : & Self ) -> core:: cmp:: Ordering {
194
- self . as_wide ( ) . cmp ( other. as_wide ( ) )
180
+ self . deref ( ) . cmp ( other)
195
181
}
196
182
}
197
183
198
184
impl core:: hash:: Hash for HSTRING {
199
185
fn hash < H : core:: hash:: Hasher > ( & self , hasher : & mut H ) {
200
- self . as_wide ( ) . hash ( hasher)
186
+ self . deref ( ) . hash ( hasher)
201
187
}
202
188
}
203
189
@@ -209,7 +195,7 @@ impl PartialOrd for HSTRING {
209
195
210
196
impl PartialEq for HSTRING {
211
197
fn eq ( & self , other : & Self ) -> bool {
212
- * self . as_wide ( ) == * other. as_wide ( )
198
+ self . deref ( ) == other. deref ( )
213
199
}
214
200
}
215
201
@@ -233,7 +219,7 @@ impl PartialEq<&String> for HSTRING {
233
219
234
220
impl PartialEq < str > for HSTRING {
235
221
fn eq ( & self , other : & str ) -> bool {
236
- self . as_wide ( ) . iter ( ) . copied ( ) . eq ( other. encode_utf16 ( ) )
222
+ self . iter ( ) . copied ( ) . eq ( other. encode_utf16 ( ) )
237
223
}
238
224
}
239
225
@@ -309,8 +295,7 @@ impl PartialEq<&std::ffi::OsString> for HSTRING {
309
295
#[ cfg( feature = "std" ) ]
310
296
impl PartialEq < std:: ffi:: OsStr > for HSTRING {
311
297
fn eq ( & self , other : & std:: ffi:: OsStr ) -> bool {
312
- self . as_wide ( )
313
- . iter ( )
298
+ self . iter ( )
314
299
. copied ( )
315
300
. eq ( std:: os:: windows:: ffi:: OsStrExt :: encode_wide ( other) )
316
301
}
@@ -376,7 +361,7 @@ impl<'a> TryFrom<&'a HSTRING> for String {
376
361
type Error = alloc:: string:: FromUtf16Error ;
377
362
378
363
fn try_from ( hstring : & HSTRING ) -> core:: result:: Result < Self , Self :: Error > {
379
- String :: from_utf16 ( hstring. as_wide ( ) )
364
+ String :: from_utf16 ( hstring)
380
365
}
381
366
}
382
367
0 commit comments