| 1 |
use super::*; |
| 2 |
|
| 3 |
/// An ([HSTRING](https://docs.microsoft.com/en-us/windows/win32/winrt/hstring)) |
| 4 |
/// is a reference-counted and immutable UTF-16 string type. |
| 5 |
#[repr(transparent)] |
| 6 |
pub struct HSTRING(pub(crate) *mut HStringHeader); |
| 7 |
|
| 8 |
impl HSTRING { |
| 9 |
/// Create an empty `HSTRING`. |
| 10 |
/// |
| 11 |
/// This function does not allocate memory. |
| 12 |
pub const fn new() -> Self { |
| 13 |
Self(core::ptr::null_mut()) |
| 14 |
} |
| 15 |
|
| 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 |
/// Create a `HSTRING` from a slice of 16 bit characters (wchars). |
| 47 |
pub fn from_wide(value: &[u16]) -> Result<Self> { |
| 48 |
unsafe { Self::from_wide_iter(value.iter().copied(), value.len()) } |
| 49 |
} |
| 50 |
|
| 51 |
/// Get the contents of this `HSTRING` as a String lossily. |
| 52 |
pub fn to_string_lossy(&self) -> String { |
| 53 |
String::from_utf16_lossy(self.as_wide()) |
| 54 |
} |
| 55 |
|
| 56 |
/// Get the contents of this `HSTRING` as a OsString. |
| 57 |
#[cfg(feature = "std")] |
| 58 |
pub fn to_os_string(&self) -> std::ffi::OsString { |
| 59 |
std::os::windows::ffi::OsStringExt::from_wide(self.as_wide()) |
| 60 |
} |
| 61 |
|
| 62 |
/// # Safety |
| 63 |
/// len must not be less than the number of items in the iterator. |
| 64 |
unsafe fn from_wide_iter<I: Iterator<Item = u16>>(iter: I, len: usize) -> Result<Self> { |
| 65 |
if len == 0 { |
| 66 |
return Ok(Self::new()); |
| 67 |
} |
| 68 |
|
| 69 |
let ptr = HStringHeader::alloc(len.try_into()?)?; |
| 70 |
|
| 71 |
// Place each utf-16 character into the buffer and |
| 72 |
// increase len as we go along. |
| 73 |
for (index, wide) in iter.enumerate() { |
| 74 |
debug_assert!(index < len); |
| 75 |
|
| 76 |
(*ptr).data.add(index).write(wide); |
| 77 |
(*ptr).len = index as u32 + 1; |
| 78 |
} |
| 79 |
|
| 80 |
// Write a 0 byte to the end of the buffer. |
| 81 |
(*ptr).data.offset((*ptr).len as isize).write(0); |
| 82 |
Ok(Self(ptr)) |
| 83 |
} |
| 84 |
|
| 85 |
fn as_header(&self) -> Option<&HStringHeader> { |
| 86 |
unsafe { self.0.as_ref() } |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl Default for HSTRING { |
| 91 |
fn default() -> Self { |
| 92 |
Self::new() |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl Clone for HSTRING { |
| 97 |
fn clone(&self) -> Self { |
| 98 |
if let Some(header) = self.as_header() { |
| 99 |
Self(header.duplicate().unwrap()) |
| 100 |
} else { |
| 101 |
Self::new() |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl Drop for HSTRING { |
| 107 |
fn drop(&mut self) { |
| 108 |
if let Some(header) = self.as_header() { |
| 109 |
// HSTRING_REFERENCE_FLAG indicates a string backed by static or stack memory that is |
| 110 |
// thus not reference-counted and does not need to be freed. |
| 111 |
unsafe { |
| 112 |
if header.flags & HSTRING_REFERENCE_FLAG == 0 && header.count.release() == 0 { |
| 113 |
HStringHeader::free(self.0); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
unsafe impl Send for HSTRING {} |
| 121 |
unsafe impl Sync for HSTRING {} |
| 122 |
|
| 123 |
impl core::fmt::Display for HSTRING { |
| 124 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 125 |
write!( |
| 126 |
f, |
| 127 |
"{}", |
| 128 |
Decode(|| core::char::decode_utf16(self.as_wide().iter().cloned())) |
| 129 |
) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
impl core::fmt::Debug for HSTRING { |
| 134 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 135 |
write!(f, "\"{}\"", self) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl From<&str> for HSTRING { |
| 140 |
fn from(value: &str) -> Self { |
| 141 |
unsafe { Self::from_wide_iter(value.encode_utf16(), value.len()).unwrap() } |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
impl From<String> for HSTRING { |
| 146 |
fn from(value: String) -> Self { |
| 147 |
value.as_str().into() |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl From<&String> for HSTRING { |
| 152 |
fn from(value: &String) -> Self { |
| 153 |
value.as_str().into() |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#[cfg(feature = "std")] |
| 158 |
impl From<&std::path::Path> for HSTRING { |
| 159 |
fn from(value: &std::path::Path) -> Self { |
| 160 |
value.as_os_str().into() |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
#[cfg(feature = "std")] |
| 165 |
impl From<&std::ffi::OsStr> for HSTRING { |
| 166 |
fn from(value: &std::ffi::OsStr) -> Self { |
| 167 |
unsafe { |
| 168 |
Self::from_wide_iter( |
| 169 |
std::os::windows::ffi::OsStrExt::encode_wide(value), |
| 170 |
value.len(), |
| 171 |
) |
| 172 |
.unwrap() |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[cfg(feature = "std")] |
| 178 |
impl From<std::ffi::OsString> for HSTRING { |
| 179 |
fn from(value: std::ffi::OsString) -> Self { |
| 180 |
value.as_os_str().into() |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
#[cfg(feature = "std")] |
| 185 |
impl From<&std::ffi::OsString> for HSTRING { |
| 186 |
fn from(value: &std::ffi::OsString) -> Self { |
| 187 |
value.as_os_str().into() |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
impl Eq for HSTRING {} |
| 192 |
|
| 193 |
impl Ord for HSTRING { |
| 194 |
fn cmp(&self, other: &Self) -> core::cmp::Ordering { |
| 195 |
self.as_wide().cmp(other.as_wide()) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
impl core::hash::Hash for HSTRING { |
| 200 |
fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) { |
| 201 |
self.as_wide().hash(hasher) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl PartialOrd for HSTRING { |
| 206 |
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
| 207 |
Some(self.cmp(other)) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
impl PartialEq for HSTRING { |
| 212 |
fn eq(&self, other: &Self) -> bool { |
| 213 |
*self.as_wide() == *other.as_wide() |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl PartialEq<String> for HSTRING { |
| 218 |
fn eq(&self, other: &String) -> bool { |
| 219 |
*self == **other |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
impl PartialEq<String> for &HSTRING { |
| 224 |
fn eq(&self, other: &String) -> bool { |
| 225 |
**self == **other |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
impl PartialEq<&String> for HSTRING { |
| 230 |
fn eq(&self, other: &&String) -> bool { |
| 231 |
*self == ***other |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
impl PartialEq<str> for HSTRING { |
| 236 |
fn eq(&self, other: &str) -> bool { |
| 237 |
self.as_wide().iter().copied().eq(other.encode_utf16()) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
impl PartialEq<str> for &HSTRING { |
| 242 |
fn eq(&self, other: &str) -> bool { |
| 243 |
**self == *other |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
impl PartialEq<&str> for HSTRING { |
| 248 |
fn eq(&self, other: &&str) -> bool { |
| 249 |
*self == **other |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
impl PartialEq<HSTRING> for str { |
| 254 |
fn eq(&self, other: &HSTRING) -> bool { |
| 255 |
*other == *self |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
impl PartialEq<HSTRING> for &str { |
| 260 |
fn eq(&self, other: &HSTRING) -> bool { |
| 261 |
*other == **self |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
impl PartialEq<&HSTRING> for str { |
| 266 |
fn eq(&self, other: &&HSTRING) -> bool { |
| 267 |
**other == *self |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
impl PartialEq<HSTRING> for String { |
| 272 |
fn eq(&self, other: &HSTRING) -> bool { |
| 273 |
*other == **self |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
impl PartialEq<HSTRING> for &String { |
| 278 |
fn eq(&self, other: &HSTRING) -> bool { |
| 279 |
*other == ***self |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
impl PartialEq<&HSTRING> for String { |
| 284 |
fn eq(&self, other: &&HSTRING) -> bool { |
| 285 |
**other == **self |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
#[cfg(feature = "std")] |
| 290 |
impl PartialEq<std::ffi::OsString> for HSTRING { |
| 291 |
fn eq(&self, other: &std::ffi::OsString) -> bool { |
| 292 |
*self == **other |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
#[cfg(feature = "std")] |
| 297 |
impl PartialEq<std::ffi::OsString> for &HSTRING { |
| 298 |
fn eq(&self, other: &std::ffi::OsString) -> bool { |
| 299 |
**self == **other |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
#[cfg(feature = "std")] |
| 304 |
impl PartialEq<&std::ffi::OsString> for HSTRING { |
| 305 |
fn eq(&self, other: &&std::ffi::OsString) -> bool { |
| 306 |
*self == ***other |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
#[cfg(feature = "std")] |
| 311 |
impl PartialEq<std::ffi::OsStr> for HSTRING { |
| 312 |
fn eq(&self, other: &std::ffi::OsStr) -> bool { |
| 313 |
self.as_wide() |
| 314 |
.iter() |
| 315 |
.copied() |
| 316 |
.eq(std::os::windows::ffi::OsStrExt::encode_wide(other)) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
#[cfg(feature = "std")] |
| 321 |
impl PartialEq<std::ffi::OsStr> for &HSTRING { |
| 322 |
fn eq(&self, other: &std::ffi::OsStr) -> bool { |
| 323 |
**self == *other |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#[cfg(feature = "std")] |
| 328 |
impl PartialEq<&std::ffi::OsStr> for HSTRING { |
| 329 |
fn eq(&self, other: &&std::ffi::OsStr) -> bool { |
| 330 |
*self == **other |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
#[cfg(feature = "std")] |
| 335 |
impl PartialEq<HSTRING> for std::ffi::OsStr { |
| 336 |
fn eq(&self, other: &HSTRING) -> bool { |
| 337 |
*other == *self |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
#[cfg(feature = "std")] |
| 342 |
impl PartialEq<HSTRING> for &std::ffi::OsStr { |
| 343 |
fn eq(&self, other: &HSTRING) -> bool { |
| 344 |
*other == **self |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
#[cfg(feature = "std")] |
| 349 |
impl PartialEq<&HSTRING> for std::ffi::OsStr { |
| 350 |
fn eq(&self, other: &&HSTRING) -> bool { |
| 351 |
**other == *self |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
#[cfg(feature = "std")] |
| 356 |
impl PartialEq<HSTRING> for std::ffi::OsString { |
| 357 |
fn eq(&self, other: &HSTRING) -> bool { |
| 358 |
*other == **self |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
#[cfg(feature = "std")] |
| 363 |
impl PartialEq<HSTRING> for &std::ffi::OsString { |
| 364 |
fn eq(&self, other: &HSTRING) -> bool { |
| 365 |
*other == ***self |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
#[cfg(feature = "std")] |
| 370 |
impl PartialEq<&HSTRING> for std::ffi::OsString { |
| 371 |
fn eq(&self, other: &&HSTRING) -> bool { |
| 372 |
**other == **self |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
impl<'a> TryFrom<&'a HSTRING> for String { |
| 377 |
type Error = alloc::string::FromUtf16Error; |
| 378 |
|
| 379 |
fn try_from(hstring: &HSTRING) -> core::result::Result<Self, Self::Error> { |
| 380 |
String::from_utf16(hstring.as_wide()) |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
impl TryFrom<HSTRING> for String { |
| 385 |
type Error = alloc::string::FromUtf16Error; |
| 386 |
|
| 387 |
fn try_from(hstring: HSTRING) -> core::result::Result<Self, Self::Error> { |
| 388 |
String::try_from(&hstring) |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
#[cfg(feature = "std")] |
| 393 |
impl<'a> From<&'a HSTRING> for std::ffi::OsString { |
| 394 |
fn from(hstring: &HSTRING) -> Self { |
| 395 |
hstring.to_os_string() |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
#[cfg(feature = "std")] |
| 400 |
impl From<HSTRING> for std::ffi::OsString { |
| 401 |
fn from(hstring: HSTRING) -> Self { |
| 402 |
Self::from(&hstring) |
| 403 |
} |
| 404 |
} |