use std::borrow::Borrow;
use std::ops::Deref;
use std::borrow::Cow;
use std::str;
use super::prelude::*;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct FromMutf8BytesWithNulError {
kind: NGCesu8CError,
bytes: Vec<u8>,
}
impl FromMutf8BytesWithNulError {
pub fn kind(&self) -> &NGCesu8CError {
&self.kind
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
}
#[derive(PartialEq, Eq, Clone)]
pub struct Mutf8CString {
pub(crate) inner: Vec<u8>,
}
impl Mutf8CString {
impl_string_encoding_meths!(base, "MUTF-8");
impl_string_encoding_meths!(cstring, "MUTF-8");
}
impl Mutf8CString {
pub(crate) unsafe fn _from_bytes_unchecked(v: Vec<u8>) -> Self {
Self { inner: v }
}
pub(crate) const fn _into_bytes_unchecked(self) -> Vec<u8> {
let this = std::mem::ManuallyDrop::new(self);
unsafe { std::ptr::read(&this.inner) }
}
}
impl Mutf8CString {
#[inline]
pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Mutf8CString, NGCesu8CError> {
fn from_bytes(mut b: Vec<u8>) -> Result<Mutf8CString, NGCesu8CError> {
b.reserve_exact(1);
b.push(b'\0');
Mutf8CStr::try_from_bytes_with_nul(&b)
.map(|_| ()) .map(move |_| Mutf8CString { inner: b })
}
from_bytes(t.into())
}
}
impl Borrow<Mutf8CStr> for Mutf8CString {
fn borrow(&self) -> &Mutf8CStr {
self
}
}
impl Default for Mutf8CString {
fn default() -> Self {
let a: &Mutf8CStr = Default::default();
a.to_owned()
}
}
impl Deref for Mutf8CString {
type Target = Mutf8CStr;
fn deref(&self) -> &Self::Target {
unsafe { Mutf8CStr::from_bytes_with_nul_unchecked(&self.inner) }
}
}
impl Drop for Mutf8CString {
fn drop(&mut self) {
unsafe {
*self.inner.get_unchecked_mut(0) = 0;
}
}
}