windows-strings 0.100.0

Windows string types
Documentation
use super::*;
use alloc::vec::Vec;
use core::ops::Deref;

/// A length-prefixed wide string.
#[repr(transparent)]
pub struct BSTR(*const u16);

impl BSTR {
    /// Creates an empty `BSTR` without allocating.
    pub const fn new() -> Self {
        Self(core::ptr::null_mut())
    }

    /// Creates a `BSTR` from UTF-16 code units.
    pub fn from_wide(value: &[u16]) -> Self {
        if value.is_empty() {
            return Self::new();
        }

        let result = unsafe {
            Self(bindings::SysAllocStringLen(
                value.as_ptr(),
                value.len().try_into().unwrap(),
            ))
        };

        assert!(!result.is_empty(), "allocation failed");

        result
    }

    /// Returns a display adapter for the string.
    pub fn display(&self) -> impl core::fmt::Display + '_ {
        Decode(move || core::char::decode_utf16(self.iter().copied()))
    }

    /// # Safety
    ///
    /// `raw` must be null or an owned `BSTR` pointer. The returned value takes ownership and
    /// frees the string when dropped.
    #[doc(hidden)]
    pub unsafe fn from_raw(raw: *const u16) -> Self {
        Self(raw)
    }

    /// Consumes the `BSTR`, transferring ownership of the underlying pointer to the caller.
    /// The caller is responsible for freeing the returned pointer.
    #[doc(hidden)]
    pub fn into_raw(self) -> *const u16 {
        core::mem::ManuallyDrop::new(self).0
    }
}

impl Deref for BSTR {
    type Target = [u16];

    fn deref(&self) -> &[u16] {
        let len = if self.0.is_null() {
            0
        } else {
            unsafe { bindings::SysStringLen(self.0) as usize }
        };

        if len > 0 {
            unsafe { core::slice::from_raw_parts(self.0, len) }
        } else {
            // Keep `as_ptr` on the empty slice null-terminated.
            const EMPTY: [u16; 1] = [0];
            &EMPTY[..0]
        }
    }
}

impl Clone for BSTR {
    fn clone(&self) -> Self {
        Self::from_wide(self)
    }
}

impl From<&str> for BSTR {
    fn from(value: &str) -> Self {
        let value: Vec<u16> = value.encode_utf16().collect();
        Self::from_wide(&value)
    }
}

impl From<String> for BSTR {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl From<&String> for BSTR {
    fn from(value: &String) -> Self {
        value.as_str().into()
    }
}

impl TryFrom<&BSTR> for String {
    type Error = alloc::string::FromUtf16Error;

    fn try_from(value: &BSTR) -> Result<Self, Self::Error> {
        Self::from_utf16(value)
    }
}

impl TryFrom<BSTR> for String {
    type Error = alloc::string::FromUtf16Error;

    fn try_from(value: BSTR) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl Default for BSTR {
    fn default() -> Self {
        Self(core::ptr::null_mut())
    }
}

impl core::fmt::Debug for BSTR {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        core::write!(f, "{}", self.display())
    }
}

impl PartialEq for BSTR {
    fn eq(&self, other: &Self) -> bool {
        self.deref() == other.deref()
    }
}

impl Eq for BSTR {}

impl PartialEq<BSTR> for &str {
    fn eq(&self, other: &BSTR) -> bool {
        other == self
    }
}

impl PartialEq<BSTR> for String {
    fn eq(&self, other: &BSTR) -> bool {
        other == self
    }
}

impl<T: AsRef<str> + ?Sized> PartialEq<T> for BSTR {
    fn eq(&self, other: &T) -> bool {
        self.iter().copied().eq(other.as_ref().encode_utf16())
    }
}

impl Drop for BSTR {
    fn drop(&mut self) {
        if !self.0.is_null() {
            unsafe { bindings::SysFreeString(self.0) }
        }
    }
}