[][src]Trait univstring::UnivString

pub trait UnivString {
    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>>>; }

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

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 FromUtf16Error if the string contains unrecognizable UTF-16 characters as UTF-8.

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 Utf8Error if the string contains unrecognizable characters as UTF-8.

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 Utf8Error if the string contains unrecognizable characters as UTF-8.

fn to_string(&self) -> Result<Cow<'_, str>, ConversionError<CNulError>>

Converts a string to String or str(if possible)

Errors

  • This function will return a Utf8Error or a FromUtf16Error if the string contains unrecognizable characters as UTF-8.

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 Utf8Error or a FromUtf16Error if the string contains unrecognizable characters as UTF-8.
Loading content...

Implementations on Foreign Types

impl UnivString for str[src]

impl UnivString for String[src]

impl UnivString for CStr[src]

impl UnivString for CString[src]

impl UnivString for U16CStr[src]

impl UnivString for U16CString[src]

impl UnivString for U32CStr[src]

impl UnivString for U32CString[src]

Loading content...

Implementors

Loading content...