pub trait UnivString {
// Required methods
fn to_cstr(&self) -> Result<Cow<'_, CStr>, ConversionError<CNulError>>;
fn to_ucstr16(
&self,
) -> Result<Cow<'_, UCStr<u16>>, ConversionError<WideNulError<u16>>>;
fn to_ucstr32(
&self,
) -> Result<Cow<'_, UCStr<u32>>, ConversionError<WideNulError<u32>>>;
fn to_string(&self) -> Result<Cow<'_, str>, ConversionError<CNulError>>;
fn to_wcstr(
&self,
) -> Result<Cow<'_, WideCStr>, ConversionError<WideNulError<WideChar>>>;
}Expand description
The Universal String trait
§Examples
use univstring::UnivString;
use std::ffi::CString;
use widestring::U32CString;
use std::borrow::Cow;
let org: &str = "Hello World";
assert_eq!(org.to_cstr().unwrap(), Cow::Owned(CString::new("Hello World").unwrap()));
assert_eq!(org.to_ucstr32().unwrap(), Cow::Owned(U32CString::from_str("Hello World").unwrap()));
// more optimal way to take some cstrings as argument
fn take_ucstr16<S: UnivString + ?Sized>(s: &S)
{
let _ws = s.to_ucstr16().unwrap();
// do something with the WideCString...
}
// call the function
take_ucstr16("test");
let existing_cstr = CString::new("...").unwrap();
take_ucstr16(&existing_cstr);Required Methods§
Sourcefn to_cstr(&self) -> Result<Cow<'_, CStr>, ConversionError<CNulError>>
fn to_cstr(&self) -> Result<Cow<'_, CStr>, ConversionError<CNulError>>
Converts a string to CString or CStr(if possible)
§Errors
- This function will return a CNulError(
std::ffi::NulError) if the string contains an internal 0 byte. - This function will return a
FromUtf16Errorif the string contains unrecognizable UTF-16 characters as UTF-8.
Sourcefn to_ucstr16(
&self,
) -> Result<Cow<'_, UCStr<u16>>, ConversionError<WideNulError<u16>>>
fn to_ucstr16( &self, ) -> Result<Cow<'_, UCStr<u16>>, ConversionError<WideNulError<u16>>>
Converts a string to U16CString or U16CStr(if possible)
§Errors
- This function will return a WideNulError(
std::ffi::WideNulError) if the string contains an internal 0 byte. - This function will return a
Utf8Errorif the string contains unrecognizable characters as UTF-8.
Sourcefn to_ucstr32(
&self,
) -> Result<Cow<'_, UCStr<u32>>, ConversionError<WideNulError<u32>>>
fn to_ucstr32( &self, ) -> Result<Cow<'_, UCStr<u32>>, ConversionError<WideNulError<u32>>>
Converts a string to U32CString or U32CStr(if possible)
§Errors
- This function will return a WideNulError(
std::ffi::WideNulError) if the string contains an internal 0 byte. - This function will return a
Utf8Errorif the string contains unrecognizable characters as UTF-8.
Sourcefn to_string(&self) -> Result<Cow<'_, str>, ConversionError<CNulError>>
fn to_string(&self) -> Result<Cow<'_, str>, ConversionError<CNulError>>
Converts a string to String or str(if possible)
§Errors
- This function will return a
Utf8Erroror aFromUtf16Errorif the string contains unrecognizable characters as UTF-8.
Sourcefn to_wcstr(
&self,
) -> Result<Cow<'_, WideCStr>, ConversionError<WideNulError<WideChar>>>
fn to_wcstr( &self, ) -> Result<Cow<'_, WideCStr>, ConversionError<WideNulError<WideChar>>>
Converts a string to String or str(if possible)
§Errors
- This function will return a
Utf8Erroror aFromUtf16Errorif the string contains unrecognizable characters as UTF-8.