Struct widestring::UCString[][src]

pub struct UCString<C: UChar> { /* fields omitted */ }

An owned, mutable C-style "wide" string for FFI that is nul-aware and nul-terminated.

UCString is aware of nul values. Unless unchecked conversions are used, all UCString strings end with a nul-terminator in the underlying buffer and contain no internal nul values. The strings may still contain invalid or ill-formed UTF-16 or UTF-32 data. These strings are intended to be used with FFI functions such as Windows API that may require nul-terminated strings.

UCString can be converted to and from many other string types, including UString, OsString, and String, making proper Unicode FFI safe and easy.

Please prefer using the type aliases U16CString or U32CString or WideCString to using this type directly.

Examples

The following example constructs a U16CString and shows how to convert a U16CString to a regular Rust String.

use widestring::U16CString;
let s = "Test";
// Create a wide string from the rust string
let wstr = U16CString::from_str(s).unwrap();
// Convert back to a rust string
let rust_str = wstr.to_string_lossy();
assert_eq!(rust_str, "Test");

The same example using U32CString:

use widestring::U32CString;
let s = "Test";
// Create a wide string from the rust string
let wstr = U32CString::from_str(s).unwrap();
// Convert back to a rust string
let rust_str = wstr.to_string_lossy();
assert_eq!(rust_str, "Test");

Methods

impl<C: UChar> UCString<C>
[src]

Constructs a UCString from a container of wide character data.

This method will consume the provided data and use the underlying elements to construct a new string. The data will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain the Vec as well as the position of the nul value.

Examples

use widestring::U16CString;
let v = vec![84u16, 104u16, 101u16]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wcstr = U16CString::new(v).unwrap();
use widestring::U32CString;
let v = vec![84u32, 104u32, 101u32]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wcstr = U32CString::new(v).unwrap();

The following example demonstrates errors from nul values in a vector.

use widestring::U16CString;
let v = vec![84u16, 0u16, 104u16, 101u16]; // 'T' NUL 'h' 'e'
// Create a wide string from the vector
let res = U16CString::new(v);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 1);
use widestring::U32CString;
let v = vec![84u32, 0u32, 104u32, 101u32]; // 'T' NUL 'h' 'e'
// Create a wide string from the vector
let res = U32CString::new(v);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 1);

Constructs a UCString from a nul-terminated container of UTF-16 or UTF-32 data.

This method will consume the provided data and use the underlying elements to construct a new string. The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec.

Examples

use widestring::U16CString;
let v = vec![84u16, 104u16, 101u16, 0u16]; // 'T' 'h' 'e' NUL
// Create a wide string from the vector
let wcstr = U16CString::from_vec_with_nul(v).unwrap();
use widestring::U32CString;
let v = vec![84u32, 104u32, 101u32, 0u32]; // 'T' 'h' 'e' NUL
// Create a wide string from the vector
let wcstr = U32CString::from_vec_with_nul(v).unwrap();

The following example demonstrates errors from missing nul values in a vector.

use widestring::U16CString;
let v = vec![84u16, 104u16, 101u16]; // 'T' 'h' 'e'
// Create a wide string from the vector
let res = U16CString::from_vec_with_nul(v);
assert!(res.is_err());
use widestring::U32CString;
let v = vec![84u32, 104u32, 101u32]; // 'T' 'h' 'e'
// Create a wide string from the vector
let res = U32CString::from_vec_with_nul(v);
assert!(res.is_err());

Creates a UCString from a vector without checking for interior nul values.

A terminating nul value will be appended if the vector does not already have a terminating nul.

Safety

This method is equivalent to new except that no runtime assertion is made that v contains no nul values. Providing a vector with nul values will result in an invalid UCString.

Creates a UCString from a vector that should have a nul terminator, without checking for any nul values.

Safety

This method is equivalent to from_vec_with_nul except that no runtime assertion is made that v contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid UCString.

Constructs a UCString from anything that can be converted to a UStr.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec as well as the position of the nul value.

Constructs a UCString from anything that can be converted to a UStr, without scanning for invalid nul values.

Safety

This method is equivalent to from_u16_str except that no runtime assertion is made that s contains no nul values. Providing a string with nul values will result in an invalid UCString.

Constructs a UCString from anything that can be converted to a UStr with a nul terminator.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec.

Constructs a UCString from anything that can be converted to a UStr with a nul terminator, without checking the string for any invalid interior nul values.

Safety

This method is equivalent to from_u16_str_with_nul except that no runtime assertion is made that s contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid UCString.

Constructs a new UCString copied from a nul-terminated string pointer.

This will scan for nul values beginning with p. The first nul value will be used as the nul terminator for the string, similar to how libc string functions such as strlen work.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid or has a nul terminator, and the function could scan past the underlying buffer.

p must be non-null.

Panics

This function panics if p is null.

Caveat

The lifetime for the returned string is inferred from its usage. To prevent accidental misuse, it's suggested to tie the lifetime to whichever source lifetime is safe in the context, such as by providing a helper function taking the lifetime of a host value for the string, or by explicit annotation.

Converts to a UCStr reference.

Converts the wide string into a Vec without a nul terminator, consuming the string in the process.

The resulting vector will not contain a nul-terminator, and will contain no other nul values.

Converts the wide string into a Vec, consuming the string in the process.

The resulting vector will contain a nul-terminator and no interior nul values.

Transfers ownership of the wide string to a C caller.

Safety

The pointer must be returned to Rust and reconstituted using from_raw to be properly deallocated. Specifically, one should not use the standard C free function to deallocate this string.

Failure to call from_raw will lead to a memory leak.

Retakes ownership of a UCString that was transferred to C.

Safety

This should only ever be called with a pointer that was earlier obtained by calling into_raw on a UCString. Additionally, the length of the string will be recalculated from the pointer.

Converts this UCString into a boxed UCStr.

Examples

use widestring::{U16CString, U16CStr};

let mut v = vec![102u16, 111u16, 111u16]; // "foo"
let c_string = U16CString::new(v.clone()).unwrap();
let boxed = c_string.into_boxed_ucstr();
v.push(0);
assert_eq!(&*boxed, U16CStr::from_slice_with_nul(&v).unwrap());
use widestring::{U32CString, U32CStr};

let mut v = vec![102u32, 111u32, 111u32]; // "foo"
let c_string = U32CString::new(v.clone()).unwrap();
let boxed = c_string.into_boxed_ucstr();
v.push(0);
assert_eq!(&*boxed, U32CStr::from_slice_with_nul(&v).unwrap());

impl UCString<u16>
[src]

Constructs a U16CString from a str.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u16> as well as the position of the nul value.

Examples

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = U16CString::from_str(s).unwrap();

The following example demonstrates errors from nul values in a vector.

use widestring::U16CString;
let s = "My\u{0}String";
// Create a wide string from the string
let res = U16CString::from_str(s);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 2);

Constructs a U16CString from a str, without checking for interior nul values.

Safety

This method is equivalent to from_str except that no runtime assertion is made that s contains no nul values. Providing a string with nul values will result in an invalid U16CString.

Examples

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = unsafe { U16CString::from_str_unchecked(s) };

Constructs a U16CString from a str with a nul terminator.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u16>.

Examples

use widestring::U16CString;
let s = "My\u{0}String";
// Create a wide string from the string
let wcstr = U16CString::from_str_with_nul(s).unwrap();
assert_eq!(wcstr.to_string_lossy(), "My");

The following example demonstrates errors from missing nul values in a vector.

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let res = U16CString::from_str_with_nul(s);
assert!(res.is_err());

Constructs a U16CString from str str that should have a terminating nul, but without checking for any nul values.

Safety

This method is equivalent to from_str_with_nul except that no runtime assertion is made that s contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid U16CString.

Examples

use widestring::U16CString;
let s = "My String\u{0}";
// Create a wide string from the string
let wcstr = unsafe { U16CString::from_str_with_nul_unchecked(s) };
assert_eq!(wcstr.to_string_lossy(), "My String");

Constructs a new U16CString copied from a u16 pointer and a length.

The len argument is the number of u16 elements, not the number of bytes.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u16> as well as the position of the nul value.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U16CString copied from a u16 pointer and a length.

The len argument is the number of u16 elements, not the number of bytes.

The string will not be checked for invalid nul values.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements. In addition, no checking for invalid nul values is performed, so if any elements of p are a nul value, the resulting U16CString will be invalid.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U16String copied from a u16 pointer and a length.

The len argument is the number of u16 elements, not the number of bytes.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u16>.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U16String copied from a u16 pointer and a length.

The len argument is the number of u16 elements, not the number of bytes.

The data should end with a nul terminator, but no checking is done on whether the data actually ends with a nul terminator, or if the data contains any interior nul values.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements. In addition, no checking for nul values is performed, so if there data does not end with a nul terminator, or if there are any interior nul values, the resulting U16CString will be invalid.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a U16CString from anything that can be converted to an OsStr.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u16> as well as the position of the nul value.

Examples

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = U16CString::from_os_str(s).unwrap();

The following example demonstrates errors from nul values in a vector.

use widestring::U16CString;
let s = "My\u{0}String";
// Create a wide string from the string
let res = U16CString::from_os_str(s);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 2);

Constructs a U16CString from anything that can be converted to an OsStr, without checking for interior nul values.

Safety

This method is equivalent to from_os_str except that no runtime assertion is made that s contains no nul values. Providing a string with nul values will result in an invalid U16CString.

Examples

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = unsafe { U16CString::from_os_str_unchecked(s) };

Constructs a U16CString from anything that can be converted to an OsStr with a nul terminator.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u16>.

Examples

use widestring::U16CString;
let s = "My\u{0}String";
// Create a wide string from the string
let wcstr = U16CString::from_os_str_with_nul(s).unwrap();
assert_eq!(wcstr.to_string_lossy(), "My");

The following example demonstrates errors from missing nul values in a vector.

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let res = U16CString::from_os_str_with_nul(s);
assert!(res.is_err());

Constructs a U16CString from anything that can be converted to an OsStr that should have a terminating nul, but without checking for any nul values.

Safety

This method is equivalent to from_os_str_with_nul except that no runtime assertion is made that s contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid U16CString.

Examples

use widestring::U16CString;
let s = "My String\u{0}";
// Create a wide string from the string
let wcstr = unsafe { U16CString::from_os_str_with_nul_unchecked(s) };
assert_eq!(wcstr.to_string_lossy(), "My String");

impl UCString<u32>
[src]

Constructs a U32CString from a container of wide character data.

This method will consume the provided data and use the underlying elements to construct a new string. The data will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain the Vec<u32> as well as the position of the nul value.

Examples

use widestring::U32CString;
let v: Vec<char> = "Test".chars().collect();
// Create a wide string from the vector
let wcstr = U32CString::from_chars(v).unwrap();

The following example demonstrates errors from nul values in a vector.

use widestring::U32CString;
let v: Vec<char> = "T\u{0}est".chars().collect();
// Create a wide string from the vector
let res = U32CString::from_chars(v);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 1);

Constructs a U32CString from a nul-terminated container of UTF-32 data.

This method will consume the provided data and use the underlying elements to construct a new string. The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u32>.

Examples

use widestring::U32CString;
let v: Vec<char> = "Test\u{0}".chars().collect();
// Create a wide string from the vector
let wcstr = U32CString::from_chars_with_nul(v).unwrap();

The following example demonstrates errors from missing nul values in a vector.

use widestring::U32CString;
let v: Vec<char> = "Test".chars().collect();
// Create a wide string from the vector
let res = U32CString::from_chars_with_nul(v);
assert!(res.is_err());

Creates a U32CString from a vector without checking for interior nul values.

A terminating nul value will be appended if the vector does not already have a terminating nul.

Safety

This method is equivalent to new except that no runtime assertion is made that v contains no nul values. Providing a vector with nul values will result in an invalid U32CString.

Creates a U32CString from a vector that should have a nul terminator, without checking for any nul values.

Safety

This method is equivalent to from_vec_with_nul except that no runtime assertion is made that v contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid U32CString.

Constructs a U32CString from a str.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u32> as well as the position of the nul value.

Examples

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = U32CString::from_str(s).unwrap();

The following example demonstrates errors from nul values in a vector.

use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let res = U32CString::from_str(s);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 2);

Constructs a U32CString from a str, without checking for interior nul values.

Safety

This method is equivalent to from_str except that no runtime assertion is made that s contains no nul values. Providing a string with nul values will result in an invalid U32CString.

Examples

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = unsafe { U32CString::from_str_unchecked(s) };

Constructs a U32CString from a str with a nul terminator.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u32>.

Examples

use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let wcstr = U32CString::from_str_with_nul(s).unwrap();
assert_eq!(wcstr.to_string_lossy(), "My");

The following example demonstrates errors from missing nul values in a vector.

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let res = U32CString::from_str_with_nul(s);
assert!(res.is_err());

Constructs a U32CString from a str that should have a terminating nul, but without checking for any nul values.

Safety

This method is equivalent to from_str_with_nul except that no runtime assertion is made that s contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid U32CString.

Examples

use widestring::U32CString;
let s = "My String\u{0}";
// Create a wide string from the string
let wcstr = unsafe { U32CString::from_str_with_nul_unchecked(s) };
assert_eq!(wcstr.to_string_lossy(), "My String");

Constructs a new U32CString copied from a u32 pointer and a length.

The len argument is the number of u32 elements, not the number of bytes.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u32> as well as the position of the nul value.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32CString copied from a u32 pointer and a length.

The len argument is the number of u32 elements, not the number of bytes.

The string will not be checked for invalid nul values.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements. In addition, no checking for invalid nul values is performed, so if any elements of p are a nul value, the resulting U16CString will be invalid.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32String copied from a u32 pointer and a length.

The len argument is the number of u32 elements, not the number of bytes.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u32>.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32String copied from a u32 pointer and a length.

The len argument is the number of u32 elements, not the number of bytes.

The data should end with a nul terminator, but no checking is done on whether the data actually ends with a nul terminator, or if the data contains any interior nul values.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements. In addition, no checking for nul values is performed, so if there data does not end with a nul terminator, or if there are any interior nul values, the resulting U32CString will be invalid.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32CString copied from a char pointer and a length.

The len argument is the number of char elements, not the number of bytes.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u32> as well as the position of the nul value.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32CString copied from a char pointer and a length.

The len argument is the number of char elements, not the number of bytes.

The string will not be checked for invalid nul values.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements. In addition, no checking for invalid nul values is performed, so if any elements of p are a nul value, the resulting U32CString will be invalid.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32String copied from a char pointer and a length.

The len argument is the number of char elements, not the number of bytes.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u32>.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a new U32String copied from a char pointer and a length.

The len argument is the number of char elements, not the number of bytes.

The data should end with a nul terminator, but no checking is done on whether the data actually ends with a nul terminator, or if the data contains any interior nul values.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for len elements. In addition, no checking for nul values is performed, so if there data does not end with a nul terminator, or if there are any interior nul values, the resulting U32CString will be invalid.

Panics

Panics if len is greater than 0 but p is a null pointer.

Constructs a U32CString from anything that can be converted to an OsStr.

The string will be scanned for invalid nul values.

Failures

This function will return an error if the data contains a nul value. The returned error will contain a Vec<u16> as well as the position of the nul value.

Examples

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = U32CString::from_os_str(s).unwrap();

The following example demonstrates errors from nul values in a vector.

use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let res = U32CString::from_os_str(s);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 2);

Constructs a U32CString from anything that can be converted to an OsStr, without checking for interior nul values.

Safety

This method is equivalent to from_os_str except that no runtime assertion is made that s contains no nul values. Providing a string with nul values will result in an invalid U32CString.

Examples

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = unsafe { U32CString::from_os_str_unchecked(s) };

Constructs a U32CString from anything that can be converted to an OsStr with a nul terminator.

The string will be truncated at the first nul value in the string.

Failures

This function will return an error if the data does not contain a nul to terminate the string. The returned error will contain the consumed Vec<u16>.

Examples

use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let wcstr = U32CString::from_os_str_with_nul(s).unwrap();
assert_eq!(wcstr.to_string_lossy(), "My");

The following example demonstrates errors from missing nul values in a vector.

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let res = U32CString::from_os_str_with_nul(s);
assert!(res.is_err());

Constructs a U32CString from anything that can be converted to an OsStr that should have a terminating nul, but without checking for any nul values.

Safety

This method is equivalent to from_os_str_with_nul except that no runtime assertion is made that s contains no nul values. Providing a vector with interior nul values or without a terminating nul value will result in an invalid U32CString.

Examples

use widestring::U32CString;
let s = "My String\u{0}";
// Create a wide string from the string
let wcstr = unsafe { U32CString::from_os_str_with_nul_unchecked(s) };
assert_eq!(wcstr.to_string_lossy(), "My String");

Methods from Deref<Target = UCStr<C>>

Copies the wide string to an new owned UString.

Copies the wide string to a new owned UString.

The UString will not have a nul terminator.

Examples

use widestring::U16CString;
let wcstr = U16CString::from_str("MyString").unwrap();
// Convert U16CString to a U16String
let wstr = wcstr.to_ustring();

// U16CString will have a terminating nul
let wcvec = wcstr.into_vec_with_nul();
assert_eq!(wcvec[wcvec.len()-1], 0);
// The resulting U16String will not have the terminating nul
let wvec = wstr.into_vec();
assert_ne!(wvec[wvec.len()-1], 0);
use widestring::U32CString;
let wcstr = U32CString::from_str("MyString").unwrap();
// Convert U32CString to a U32String
let wstr = wcstr.to_ustring();

// U32CString will have a terminating nul
let wcvec = wcstr.into_vec_with_nul();
assert_eq!(wcvec[wcvec.len()-1], 0);
// The resulting U32String will not have the terminating nul
let wvec = wstr.into_vec();
assert_ne!(wvec[wvec.len()-1], 0);

Converts to a slice of the wide string.

The slice will not include the nul terminator.

Converts to a slice of the wide string, including the nul terminator.

Returns a raw pointer to the wide string.

The pointer is valid only as long as the lifetime of this reference.

Returns the length of the wide string as number of elements (not number of bytes) not including nul terminator.

Returns whether this wide string contains no data (i.e. is only the nul terminator).

Decodes a wide string to an owned OsString.

This makes a string copy of the U16CStr. Since U16CStr makes no guarantees that it is valid UTF-16, there is no guarantee that the resulting OsString will be valid data. The OsString will not have a nul terminator.

Examples

use widestring::U16CString;
use std::ffi::OsString;
let s = "MyString";
// Create a wide string from the string
let wstr = U16CString::from_str(s).unwrap();
// Create an OsString from the wide string
let osstr = wstr.to_os_string();

assert_eq!(osstr, OsString::from(s));

Copies the wide string to a String if it contains valid UTF-16 data.

Failures

Returns an error if the string contains any invalid UTF-16 data.

Examples

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wstr = U16CString::from_str(s).unwrap();
// Create a regular string from the wide string
let s2 = wstr.to_string().unwrap();

assert_eq!(s2, s);

Copies the wide string to a String.

Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.

Examples

use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wstr = U16CString::from_str(s).unwrap();
// Create a regular string from the wide string
let s2 = wstr.to_string_lossy();

assert_eq!(s2, s);

Decodes a wide string to an owned OsString.

This makes a string copy of the U32CStr. Since U32CStr makes no guarantees that it is valid UTF-32, there is no guarantee that the resulting OsString will be valid data. The OsString will not have a nul terminator.

Examples

use widestring::U32CString;
use std::ffi::OsString;
let s = "MyString";
// Create a wide string from the string
let wstr = U32CString::from_str(s).unwrap();
// Create an OsString from the wide string
let osstr = wstr.to_os_string();

assert_eq!(osstr, OsString::from(s));

Copies the wide string to a String if it contains valid UTF-32 data.

Failures

Returns an error if the string contains any invalid UTF-32 data.

Examples

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wstr = U32CString::from_str(s).unwrap();
// Create a regular string from the wide string
let s2 = wstr.to_string().unwrap();

assert_eq!(s2, s);

Copies the wide string to a String.

Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.

Examples

use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wstr = U32CString::from_str(s).unwrap();
// Create a regular string from the wide string
let s2 = wstr.to_string_lossy();

assert_eq!(s2, s);

Trait Implementations

impl<C: Debug + UChar> Debug for UCString<C>
[src]

Formats the value using the given formatter. Read more

impl<C: Clone + UChar> Clone for UCString<C>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<C: PartialEq + UChar> PartialEq for UCString<C>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<C: Eq + UChar> Eq for UCString<C>
[src]

impl<C: PartialOrd + UChar> PartialOrd for UCString<C>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<C: Ord + UChar> Ord for UCString<C>
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl<C: Hash + UChar> Hash for UCString<C>
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<C: UChar> Into<Vec<C>> for UCString<C>
[src]

Performs the conversion.

impl<'a> From<UCString<u16>> for Cow<'a, UCStr<u16>>
[src]

Performs the conversion.

impl<'a> From<UCString<u32>> for Cow<'a, UCStr<u32>>
[src]

Performs the conversion.

impl From<UCString<u16>> for OsString
[src]

Performs the conversion.

impl From<UCString<u32>> for OsString
[src]

Performs the conversion.

impl<C: UChar> From<UCString<C>> for UString<C>
[src]

Performs the conversion.

impl<'a, C: UChar, T: ?Sized + AsRef<UCStr<C>>> From<&'a T> for UCString<C>
[src]

Performs the conversion.

impl<C: UChar> Index<RangeFull> for UCString<C>
[src]

The returned type after indexing.

Performs the indexing (container[index]) operation.

impl<C: UChar> Deref for UCString<C>
[src]

The resulting type after dereferencing.

Dereferences the value.

impl Default for UCString<u16>
[src]

Returns the "default value" for a type. Read more

impl Default for UCString<u32>
[src]

Returns the "default value" for a type. Read more

impl<C: UChar> Drop for UCString<C>
[src]

Executes the destructor for this type. Read more

impl<C: UChar> Borrow<UCStr<C>> for UCString<C>
[src]

Immutably borrows from an owned value. Read more

impl<C: UChar> AsRef<UCStr<C>> for UCString<C>
[src]

Performs the conversion.

impl<C: UChar> AsRef<[C]> for UCString<C>
[src]

Performs the conversion.

impl<C: UChar> From<Box<UCStr<C>>> for UCString<C>
[src]

Performs the conversion.

impl<C: UChar> From<UCString<C>> for Box<UCStr<C>>
[src]

Performs the conversion.

Auto Trait Implementations

impl<C> Send for UCString<C> where
    C: Send

impl<C> Sync for UCString<C> where
    C: Sync