impl HSTRING { /// Create an empty `HSTRING`. /// /// This function does not allocate memory. pubconstfn new() -> Self { Self(core::ptr::null_mut())
}
/// Create a `HSTRING` from a slice of 16 bit characters (wchars). pubfn from_wide(value: &[u16]) -> 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)
}
/// 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)
}
/// # 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) -> Self { if len == 0 { returnSelf::new();
}
let ptr = HStringHeader::alloc(len.try_into().unwrap());
// 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);
unsafe {
(*ptr).data.add(index).write(wide);
(*ptr).len = index as u32 + 1;
}
}
unsafe { // Write a 0 byte to the end of the buffer.
(*ptr).data.offset((*ptr).len as isize).write(0);
} Self(ptr)
}
fn deref(&self) -> &[u16] { iflet Some(header) = self.as_header() { unsafe { core::slice::from_raw_parts(header.data, header.len as usize) }
} else { // This ensures that if `as_ptr` is called on the slice that the resulting pointer // will still refer to a null-terminated string. const EMPTY: [u16; 1] = [0];
&EMPTY[..0]
}
}
}
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.