wininskit 0.1.2

Thin checked wrappers over the Win32 an installer needs: elevation, ACLs, services, the Restart Manager, the registry and shortcuts.
use std::{ffi::OsStr, os::windows::ffi::OsStringExt, path::Path};

/// A NUL-terminated UTF-16 string, which is what every W-suffixed call wants.
pub(crate) fn wide(value: impl AsRef<OsStr>) -> Vec<u16> {
    use std::os::windows::ffi::OsStrExt;
    value.as_ref().encode_wide().chain(Some(0)).collect()
}

/// Same, for a path. Paths reaching the API here are already absolute; the
/// verbatim prefix is left alone because the callers that need it added it.
pub(crate) fn wide_path(path: &Path) -> Vec<u16> {
    wide(path.as_os_str())
}

/// Reads a NUL-terminated UTF-16 buffer back into a `String`, stopping at the
/// terminator rather than trusting a returned length.
pub(crate) fn from_wide(buffer: &[u16]) -> String {
    let end = buffer.iter().position(|&c| c == 0).unwrap_or(buffer.len());
    std::ffi::OsString::from_wide(&buffer[..end])
        .to_string_lossy()
        .into_owned()
}