impl HSTRING { /// Create an empty `HSTRING`. /// /// This function does not allocate memory. pubconstfn new() -> Self { Self(core::ptr::null_mut())
}
/// Returns `true` if the string is empty. pubfn is_empty(&self) -> bool { // An empty HSTRING is represented by a null pointer. self.0.is_null()
}
/// Returns the length of the string. The length is measured in `u16`s (UTF-16 code units), not including the terminating null character. pubfn len(&self) -> usize { iflet Some(header) = self.as_header() {
header.len as usize
} else { 0
}
}
/// Get the string as 16-bit wide characters (wchars). pubfn as_wide(&self) -> &[u16] { unsafe { core::slice::from_raw_parts(self.as_ptr(), self.len()) }
}
/// Returns a raw pointer to the `HSTRING` buffer. pubfn as_ptr(&self) -> *const u16 { iflet Some(header) = self.as_header() {
header.data
} else { const EMPTY: [u16; 1] = [0];
EMPTY.as_ptr()
}
}
/// Create a `HSTRING` from a slice of 16 bit characters (wchars). pubfn from_wide(value: &[u16]) -> Result<Self> { unsafe { Self::from_wide_iter(value.iter().copied(), value.len()) }
}
/// Get the contents of this `HSTRING` as a String lossily. pubfn to_string_lossy(&self) -> String {
String::from_utf16_lossy(self.as_wide())
}
/// Get the contents of this `HSTRING` as a OsString. #[cfg(feature = "std")] pubfn to_os_string(&self) -> std::ffi::OsString {
std::os::windows::ffi::OsStringExt::from_wide(self.as_wide())
}
/// # Safety /// len must not be less than the number of items in the iterator. unsafefn from_wide_iter<I: Iterator<Item = u16>>(iter: I, len: usize) -> Result<Self> { if len == 0 { return Ok(Self::new());
}
let ptr = HStringHeader::alloc(len.try_into()?)?;
// Place each utf-16 character into the buffer and // increase len as we go along. for (index, wide) in iter.enumerate() {
debug_assert!(index < len);
(*ptr).data.add(index).write(wide);
(*ptr).len = index as u32 + 1;
}
// Write a 0 byte to the end of the buffer.
(*ptr).data.offset((*ptr).len as isize).write(0);
Ok(Self(ptr))
}
impl Drop for HSTRING { fn drop(&mutself) { iflet Some(header) = self.as_header() { // HSTRING_REFERENCE_FLAG indicates a string backed by static or stack memory that is // thus not reference-counted and does not need to be freed. unsafe { if header.flags & HSTRING_REFERENCE_FLAG == 0 && header.count.release() == 0 {
HStringHeader::free(self.0);
}
}
}
}
}
unsafeimpl Send for HSTRING {} unsafeimpl Sync for HSTRING {}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.