Struct widestring::WideCStr [] [src]

pub struct WideCStr {
    // some fields omitted
}

C-style wide string reference for WideCString.

WideCStr is aware of nul values. Unless unchecked conversions are used, all WideCStr 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 data. These strings are intended to be used with windows FFI functions that may require nul-terminated strings.

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

Methods

impl WideCStr
[src]

Coerces a value into a WideCStr.

Constructs a WideStr from a u16 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.

Constructs a WideStr from a u16 pointer and a length.

The len argument is the number of u16 elements, not the number of bytes, and does not include the nul terminator of the string. Thus, a len of 0 is valid and means that p is a pointer directly to the nul terminator of the string.

Safety

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

p must be non-null, even for zero len.

The interior values of the pointer are not scanned for nul.

Panics

This function panics if p is null or if a nul value is not found at offset len of p. Only pointers with a nul terminator are valid.

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 WideCStr from a slice of u16 values that has a nul terminator.

The slice will be scanned for nul values. If a nul value is found, it is treated as the terminator for the string, and the WideCStr slice will be truncated to that nul. If no nul value is found, an error is returned.

Copies the wide string to an new owned WideString.

Decodes a wide string to an owned OsString.

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

Examples

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

The WideString will not have a nul terminator.

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

assert_eq!(s2, s);

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

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

Trait Implementations

impl Debug for WideCStr
[src]

Formats the value using the given formatter.

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

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

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

impl Hash for WideCStr
[src]

Feeds this value into the state given, updating the hasher as necessary.

Feeds a slice of this type into the state provided.

impl ToOwned for WideCStr
[src]

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

impl AsRef<WideCStr> for WideCStr
[src]

Performs the conversion.

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

Performs the conversion.