Struct widestring::WideStr [] [src]

pub struct WideStr { /* fields omitted */ }

Wide string reference for WideString.

WideStr is aware of nul values. Strings may or may not be nul-terminated, and may contain invalid and ill-formed UTF-16 data. These strings are intended to be used with windows FFI functions that directly use string length, where the strings are known to have proper nul-termination already, or where strings are merely being passed through without modification.

WideCStr should be used instead of nul-aware strings are required.

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

Methods

impl WideStr
[src]

Coerces a value into a WideStr.

Constructs a WideStr from a u16 pointer and a length.

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

Safety

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

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.

Constructs a WideStr from a slice of u16 partial code points.

No checks are performed on the slice.

Decodes a wide string to an owned OsString.

This makes a string copy of the WideStr. Since WideStr makes no guaruntees that it is valid UTF-16, there is no guaruntee that the resulting OsString will be valid data.

Examples

use widestring::WideString;
use std::ffi::OsString;
let s = "MyString";
// Create a wide string from the string
let wstr = WideString::from_str(s);
// 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 new owned WideString.

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::WideString;
let s = "MyString";
// Create a wide string from the string
let wstr = WideString::from_str(s);
// 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::WideString;
let s = "MyString";
// Create a wide string from the string
let wstr = WideString::from_str(s);
// Create a regular string from the wide string
let lossy = wstr.to_string_lossy();

assert_eq!(lossy, s);

Converts to a slice of the wide string.

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 UTF-16 partial code units (not code points and not number of bytes).

Returns whether this wide string contains no data.

Trait Implementations

impl Debug for WideStr
[src]

Formats the value using the given formatter.

impl PartialEq for WideStr
[src]

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

This method tests for !=.

impl Eq for WideStr
[src]

impl PartialOrd for WideStr
[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 Ord for WideStr
[src]

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

impl Hash for WideStr
[src]

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

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

impl ToOwned for WideStr
[src]

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl AsRef<WideStr> for WideStr
[src]

Performs the conversion.

impl AsRef<[u16]> for WideStr
[src]

Performs the conversion.