diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
| commit | 8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch) | |
| tree | 22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/windows-strings/src/pcstr.rs | |
| parent | 4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff) | |
chore: add vendor directory
Diffstat (limited to 'vendor/windows-strings/src/pcstr.rs')
| -rw-r--r-- | vendor/windows-strings/src/pcstr.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/windows-strings/src/pcstr.rs b/vendor/windows-strings/src/pcstr.rs new file mode 100644 index 00000000..cd752854 --- /dev/null +++ b/vendor/windows-strings/src/pcstr.rs @@ -0,0 +1,64 @@ +use super::*; + +/// A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct PCSTR(pub *const u8); + +impl PCSTR { + /// Construct a new `PCSTR` from a raw pointer + pub const fn from_raw(ptr: *const u8) -> Self { + Self(ptr) + } + + /// Construct a null `PCSTR` + pub const fn null() -> Self { + Self(core::ptr::null()) + } + + /// Returns a raw pointer to the `PCSTR` + pub const fn as_ptr(&self) -> *const u8 { + self.0 + } + + /// Checks whether the `PCSTR` is null + pub fn is_null(&self) -> bool { + self.0.is_null() + } + + /// String data without the trailing 0 + /// + /// # Safety + /// + /// The `PCSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn as_bytes(&self) -> &[u8] { + unsafe { + let len = strlen(*self); + core::slice::from_raw_parts(self.0, len) + } + } + + /// Copy the `PCSTR` into a Rust `String`. + /// + /// # Safety + /// + /// See the safety information for `PCSTR::as_bytes`. + pub unsafe fn to_string(&self) -> core::result::Result<String, alloc::string::FromUtf8Error> { + unsafe { String::from_utf8(self.as_bytes().into()) } + } + + /// Allow this string to be displayed. + /// + /// # Safety + /// + /// See the safety information for `PCSTR::as_bytes`. + pub unsafe fn display(&self) -> impl core::fmt::Display + '_ { + unsafe { Decode(move || decode_utf8(self.as_bytes())) } + } +} + +impl Default for PCSTR { + fn default() -> Self { + Self::null() + } +} |
