1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
use std::convert::TryFrom; use std::ffi::CStr; use std::mem::ManuallyDrop; use std::os::raw::c_char; use std::str; use crate::{Error, JsString, Result, Status}; pub struct JsStringUtf8 { pub(crate) inner: JsString, pub(crate) buf: ManuallyDrop<Vec<c_char>>, } impl JsStringUtf8 { #[inline] pub fn as_str(&self) -> Result<&str> { unsafe { CStr::from_ptr(self.buf.as_ptr()) } .to_str() .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e))) } #[inline] pub fn as_slice(&self) -> &[u8] { unsafe { CStr::from_ptr(self.buf.as_ptr()) }.to_bytes() } #[inline] pub fn len(&self) -> usize { self.buf.len() } #[inline] pub fn is_empty(&self) -> bool { self.buf.is_empty() } #[inline] pub fn into_owned(self) -> Result<String> { Ok(self.as_str()?.to_owned()) } #[inline] pub fn take(self) -> Vec<u8> { self.as_slice().to_vec() } #[inline] pub fn into_value(self) -> JsString { self.inner } } impl TryFrom<JsStringUtf8> for String { type Error = Error; fn try_from(value: JsStringUtf8) -> Result<String> { Ok(value.as_str()?.to_owned()) } } impl From<JsStringUtf8> for Vec<u8> { fn from(value: JsStringUtf8) -> Self { value.take() } }