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
FFI functions (such as Windows API) 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]
pub fn new<'a, S: AsRef<WideStr> + ?Sized>(s: &'a S) -> &'a WideStr
[src]
Coerces a value into a WideStr
.
pub unsafe fn from_ptr<'a>(p: *const u16, len: usize) -> &'a WideStr
[src]
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.
pub fn from_slice<'a>(slice: &'a [u16]) -> &'a WideStr
[src]
Constructs a WideStr
from a slice of u16
code points.
No checks are performed on the slice.
pub fn to_os_string(&self) -> OsString
[src]
Decodes a wide string to an owned OsString
.
This makes a string copy of the WideStr
. Since WideStr
makes no guarantees that it is
valid UTF-16, there is no guarantee 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));
pub fn to_wide_string(&self) -> WideString
[src]
Copies the wide string to a new owned WideString
.
pub fn to_string(&self) -> Result<String, FromUtf16Error>
[src]
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);
pub fn to_string_lossy(&self) -> String
[src]
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);
pub fn as_slice(&self) -> &[u16]
[src]
Converts to a slice of the wide string.
pub fn as_ptr(&self) -> *const u16
[src]
Returns a raw pointer to the wide string.
The pointer is valid only as long as the lifetime of this reference.
pub fn len(&self) -> usize
[src]
Returns the length of the wide string as number of UTF-16 code units (not code points and not number of bytes).
pub fn is_empty(&self) -> bool
[src]
Returns whether this wide string contains no data.
pub fn into_wide_string(self: Box<WideStr>) -> WideString
[src]
Converts a Box<WideStr>
into a WideString
without copying or allocating.
Trait Implementations
impl Debug for WideStr
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl PartialEq for WideStr
[src]
fn eq(&self, __arg_0: &WideStr) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &WideStr) -> bool
[src]
This method tests for !=
.
impl Eq for WideStr
[src]
impl PartialOrd for WideStr
[src]
fn partial_cmp(&self, __arg_0: &WideStr) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &WideStr) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &WideStr) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &WideStr) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &WideStr) -> bool
[src]
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]
fn cmp(&self, __arg_0: &WideStr) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
impl Hash for WideStr
[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl PartialEq<WideStr> for WideString
[src]
fn eq(&self, other: &WideStr) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialOrd<WideStr> for WideString
[src]
fn partial_cmp(&self, other: &WideStr) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
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]
fn eq(&self, other: &&'a WideStr) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<'a> PartialOrd<&'a WideStr> for WideString
[src]
fn partial_cmp(&self, other: &&'a WideStr) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
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]
impl ToOwned for WideStr
[src]
type Owned = WideString
fn to_owned(&self) -> WideString
[src]
Creates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut Self::Owned)
[src]
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more