use super::*;
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PCSTR(pub *const u8);
impl PCSTR {
pub const fn from_raw(ptr: *const u8) -> Self {
Self(ptr)
}
pub fn null() -> Self {
Self(std::ptr::null())
}
pub fn as_ptr(&self) -> *const u8 {
self.0
}
pub fn is_null(&self) -> bool {
self.0.is_null()
}
pub unsafe fn as_bytes(&self) -> &[u8] {
let len = super::strlen(*self);
std::slice::from_raw_parts(self.0, len)
}
pub unsafe fn to_string(&self) -> std::result::Result<String, std::string::FromUtf8Error> {
String::from_utf8(self.as_bytes().into())
}
pub unsafe fn display(&self) -> impl std::fmt::Display + '_ {
Decode(move || decode_utf8(self.as_bytes()))
}
}
impl TypeKind for PCSTR {
type TypeKind = CopyType;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_display() {
let s = [240, 159, 146, 150, 255, 240, 159, 0];
let s = PCSTR::from_raw(s.as_ptr());
assert_eq!("💖�", format!("{}", unsafe { s.display() }));
}
}