use std::borrow::Cow;
use std::fmt::{self, Debug, Display};
use std::slice;
use std::str::{self, Utf8Error};
extern "C" {
#[link_name = "cxxbridge03$cxx_string$data"]
fn string_data(_: &CxxString) -> *const u8;
#[link_name = "cxxbridge03$cxx_string$length"]
fn string_length(_: &CxxString) -> usize;
}
#[repr(C)]
pub struct CxxString {
_private: [u8; 0],
}
impl CxxString {
pub fn len(&self) -> usize {
unsafe { string_length(self) }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn as_bytes(&self) -> &[u8] {
let data = self.as_ptr();
let len = self.len();
unsafe { slice::from_raw_parts(data, len) }
}
pub fn as_ptr(&self) -> *const u8 {
unsafe { string_data(self) }
}
pub fn to_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(self.as_bytes())
}
pub fn to_string_lossy(&self) -> Cow<str> {
String::from_utf8_lossy(self.as_bytes())
}
}
impl Display for CxxString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self.to_string_lossy().as_ref(), f)
}
}
impl Debug for CxxString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(self.to_string_lossy().as_ref(), f)
}
}
impl PartialEq for CxxString {
fn eq(&self, other: &CxxString) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl PartialEq<CxxString> for str {
fn eq(&self, other: &CxxString) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl PartialEq<str> for CxxString {
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}