use std::ffi::CString;
use std::fmt;
use std::os::raw::c_char;
use crate::unsafe_send::Sendable;
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ObsString {
c_string: CString,
}
impl ObsString {
pub fn new<S: AsRef<str>>(s: S) -> Self {
let s = s.as_ref().replace("\0", "");
Self {
c_string: CString::new(s).unwrap(),
}
}
pub fn as_ptr(&self) -> Sendable<*const c_char> {
Sendable(self.c_string.as_ptr())
}
}
impl fmt::Display for ObsString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.c_string.to_string_lossy())
}
}
impl From<&str> for ObsString {
fn from(value: &str) -> Self {
let value = value.replace("\0", "");
Self {
c_string: CString::new(value).unwrap(),
}
}
}
impl From<Vec<u8>> for ObsString {
fn from(mut value: Vec<u8>) -> Self {
value.retain(|&c| c != 0);
Self {
c_string: CString::new(value).unwrap(),
}
}
}
impl From<String> for ObsString {
fn from(value: String) -> Self {
let value = value.replace("\0", "");
Self {
c_string: CString::new(value).unwrap(),
}
}
}
impl PartialEq<&str> for ObsString {
fn eq(&self, other: &&str) -> bool {
self.c_string.to_str() == Ok(*other)
}
}