Sha256: de99eb08f1f3c351854d1ed1f978460669b53ae39dc118e788a9f66a562d2c7c

Contents?: true

Size: 1.81 KB

Versions: 19

Compression:

Stored size: 1.81 KB

Contents

use super::*;

/// A pointer to a null-terminated string of 16-bit Unicode characters.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PWSTR(pub *mut u16);

impl PWSTR {
    /// Construct a new `PWSTR` from a raw pointer.
    pub const fn from_raw(ptr: *mut u16) -> Self {
        Self(ptr)
    }

    /// Construct a null `PWSTR`.
    pub fn null() -> Self {
        Self(std::ptr::null_mut())
    }

    /// Returns a raw pointer to the `PWSTR`.
    pub fn as_ptr(&self) -> *mut u16 {
        self.0
    }

    /// Checks whether the `PWSTR` is null.
    pub fn is_null(&self) -> bool {
        self.0.is_null()
    }

    /// String data without the trailing 0.
    ///
    /// # Safety
    ///
    /// The `PWSTR`'s pointer needs to be valid for reads up until and including the next `\0`.
    pub unsafe fn as_wide(&self) -> &[u16] {
        let len = super::wcslen(PCWSTR::from_raw(self.0));
        std::slice::from_raw_parts(self.0, len)
    }

    /// Copy the `PWSTR` into a Rust `String`.
    ///
    /// # Safety
    ///
    /// See the safety information for `PWSTR::as_wide`.
    pub unsafe fn to_string(&self) -> std::result::Result<String, std::string::FromUtf16Error> {
        String::from_utf16(self.as_wide())
    }

    /// Copy the `PWSTR` into an `HSTRING`.
    ///
    /// # Safety
    ///
    /// See the safety information for `PWSTR::as_wide`.
    pub unsafe fn to_hstring(&self) -> Result<HSTRING> {
        HSTRING::from_wide(self.as_wide())
    }

    /// Allow this string to be displayed.
    ///
    /// # Safety
    ///
    /// See the safety information for `PWSTR::as_wide`.
    pub unsafe fn display(&self) -> impl std::fmt::Display + '_ {
        Decode(move || std::char::decode_utf16(self.as_wide().iter().cloned()))
    }
}

impl TypeKind for PWSTR {
    type TypeKind = CopyType;
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-28.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-27.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-26.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-25.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-25.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-25.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-24.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-23.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-22.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-21.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-20.0.2 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-20.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-18.0.3 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-17.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-17.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-16.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-15.0.1 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs
wasmtime-15.0.0 ./ext/cargo-vendor/windows-core-0.52.0/src/strings/pwstr.rs