use std::ffi::c_char;
#[cfg(feature = "ustr_handles")]
mod inner {
use super::*;
use ustr::{Ustr, ustr};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Handle(Ustr);
impl Handle {
#[inline(always)]
pub fn new(s: &str) -> Self {
Self(ustr(s))
}
#[inline(always)]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[inline(always)]
pub fn as_char_ptr(&self) -> *const c_char {
self.0.as_char_ptr()
}
}
impl From<&str> for Handle {
#[inline(always)]
fn from(s: &str) -> Self {
Self::new(s)
}
}
}
#[cfg(not(feature = "ustr_handles"))]
mod inner {
use super::*;
use std::ffi::CString;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Handle(CString);
impl Handle {
#[inline(always)]
pub fn new(s: &str) -> Self {
Self(CString::new(s).expect("Handle string contains NUL byte"))
}
#[inline(always)]
pub fn as_str(&self) -> &str {
self.0.to_str().expect("Handle contains invalid UTF-8")
}
#[inline(always)]
pub fn as_char_ptr(&self) -> *const c_char {
self.0.as_ptr()
}
}
impl From<&str> for Handle {
#[inline(always)]
fn from(s: &str) -> Self {
Self::new(s)
}
}
}
pub use inner::Handle;
pub(crate) type HandleString = Handle;