Struct widestring::WideString [] [src]

pub struct WideString { /* fields omitted */ }

An owned, mutable "wide" string for windows FFI that is not nul-aware.

WideString is not 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.

WideCString should be used instead if nul-aware strings are required.

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

Examples

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

use widestring::WideString;
let v = vec![84u16, 104u16, 101u16]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wstr = WideString::from_vec(v);
// Convert to a rust string!
let rust_str = wstr.to_string_lossy();
assert_eq!(rust_str, "The");

Methods

impl WideString
[src]

Constructs a new empty WideString.

Constructs a WideString from a vector of possibly invalid or ill-formed UTF-16 data.

No checks are made on the contents of the vector.

Examples

use widestring::WideString;
let v = vec![84u16, 104u16, 101u16]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wstr = WideString::from_vec(v);

Encodes a WideString copy from an OsStr.

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

Examples

use widestring::WideString;
let s = "MyString";
// Create a wide string from the string
let wstr = WideString::from_str(s);

assert_eq!(wstr.to_string().unwrap(), s);

Constructs a WideString 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

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

Creates a WideString with the given capacity.

The string will be able to hold exactly capacity partial code units without reallocating. If capacity is set to 0, the string will not initially allocate.

Returns the capacity this WideString can hold without reallocating.

Truncate the WideString to zero length.

Reserves the capacity for at least additiona more capacity to be inserted in the given WideString.

More space may be reserved to avoid frequent allocations.

Reserves the minimum capacity for exactly additiona more capacity to be inserted in the given WideString. Does nothing if the capcity is already sufficient.

Note that the allocator may give more space than is requested. Therefore capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Converts to a WideStr reference.

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

Extends the wide string with the given &WideStr.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

Examples

use widestring::WideString;
let s = "MyString";
let mut wstr = WideString::from_str(s);
let cloned = wstr.clone();
// Push the clone to the end, repeating the string twice.
wstr.push(cloned);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");

Extends the wide string with the given &[u16] slice.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

Examples

use widestring::WideString;
let s = "MyString";
let mut wstr = WideString::from_str(s);
let cloned = wstr.clone();
// Push the clone to the end, repeating the string twice.
wstr.push_slice(cloned);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");

Extends the string with the given &OsStr.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

Examples

use widestring::WideString;
let s = "MyString";
let mut wstr = WideString::from_str(s);
// Push the original to the end, repeating the string twice.
wstr.push_str(s);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");

Methods from Deref<Target = WideStr>

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 WideString
[src]

Formats the value using the given formatter.

impl Default for WideString
[src]

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

impl Clone for WideString
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for WideString
[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 WideString
[src]

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

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

impl Hash for WideString
[src]

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

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

impl Into<Vec<u16>> for WideString
[src]

Performs the conversion.

impl From<String> for WideString
[src]

Performs the conversion.

impl From<OsString> for WideString
[src]

Performs the conversion.

impl<'a, T: ?Sized + AsRef<WideStr>> From<&'a T> for WideString
[src]

Performs the conversion.

impl Index<RangeFull> for WideString
[src]

The returned type after indexing

The method for the indexing (container[index]) operation

impl Deref for WideString
[src]

The resulting type after dereferencing

The method called to dereference a value

impl PartialEq<WideStr> for WideString
[src]

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

This method tests for !=.

impl PartialOrd<WideStr> for WideString
[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<'a> PartialEq<&'a WideStr> for WideString
[src]

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

This method tests for !=.

impl<'a> PartialOrd<&'a WideStr> for WideString
[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<'a> PartialEq<Cow<'a, WideStr>> for WideString
[src]

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

This method tests for !=.

impl<'a> PartialOrd<Cow<'a, WideStr>> for WideString
[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 Borrow<WideStr> for WideString
[src]

Immutably borrows from an owned value. Read more

impl AsRef<WideStr> for WideString
[src]

Performs the conversion.

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

Performs the conversion.

impl From<WideCString> for WideString
[src]

Performs the conversion.