Struct widestring::WideCStr
[−]
[src]
pub struct WideCStr { /* 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 FFI functions such as Windows API 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]
pub fn new<'a, S: AsRef<WideCStr> + ?Sized>(s: &'a S) -> &'a WideCStr
[src]
Coerces a value into a WideCStr
.
pub unsafe fn from_ptr_str<'a>(p: *const u16) -> &'a WideCStr
[src]
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.
pub unsafe fn from_ptr_with_nul<'a>(p: *const u16, len: usize) -> &'a WideCStr
[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, 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. Any interior nul values will
result in an invalid WideCStr
.
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.
pub fn from_slice_with_nul<'a>(
slice: &'a [u16]
) -> Result<&'a WideCStr, MissingNulError>
[src]
slice: &'a [u16]
) -> Result<&'a WideCStr, MissingNulError>
Constructs a WideCStr
from a slice of u16
values that has a nul terminator.
The slice will be scanned for nul values. When a nul value is found, it is treated as the
terminator for the string, and the WideCStr
slice will be truncated to that nul.
Failure
If there are no no nul values in slice
, an error is returned.
pub unsafe fn from_slice_with_nul_unchecked<'a>(
slice: &'a [u16]
) -> &'a WideCStr
[src]
slice: &'a [u16]
) -> &'a WideCStr
Constructs a WideCStr
from a slice of u16
values that has a nul terminator. No
checking for nul values is performed.
Safety
This function is unsafe because it can lead to invalid WideCStr
values when slice
is missing a terminating nul value or there are non-terminating interior nul values
in the slice.
pub fn to_wide_c_string(&self) -> WideCString
[src]
Copies the wide string to an new owned WideString
.
pub fn to_os_string(&self) -> OsString
[src]
Decodes a wide string to an owned OsString
.
This makes a string copy of the WideCStr
. Since WideCStr
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::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));
pub fn to_wide_string(&self) -> WideString
[src]
Copies the wide string to a new owned WideString
.
The WideString
will not have a nul terminator.
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::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);
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::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);
pub fn as_slice(&self) -> &[u16]
[src]
Converts to a slice of the wide string.
The slice will not include the nul terminator.
pub fn as_slice_with_nul(&self) -> &[u16]
[src]
Converts to a slice of the wide string, including the nul terminator.
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) not including nul terminator.
pub fn is_empty(&self) -> bool
[src]
Returns whether this wide string contains no data (i.e. is only the nul terminator).
pub fn into_wide_c_string(self: Box<WideCStr>) -> WideCString
[src]
Converts a Box<WideCStr>
into a WideCString
without copying or allocating.
Examples
use widestring::WideCString; let v = vec![102u16, 111u16, 111u16]; // "foo" let c_string = WideCString::new(v.clone()).unwrap(); let boxed = c_string.into_boxed_wide_c_str(); assert_eq!(boxed.into_wide_c_string(), WideCString::new(v).unwrap());
Trait Implementations
impl Debug for WideCStr
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl PartialEq for WideCStr
[src]
fn eq(&self, __arg_0: &WideCStr) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &WideCStr) -> bool
[src]
This method tests for !=
.
impl Eq for WideCStr
[src]
impl PartialOrd for WideCStr
[src]
fn partial_cmp(&self, __arg_0: &WideCStr) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &WideCStr) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &WideCStr) -> 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: &WideCStr) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &WideCStr) -> 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 WideCStr
[src]
fn cmp(&self, __arg_0: &WideCStr) -> 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 WideCStr
[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<'a> Default for &'a WideCStr
[src]
impl Borrow<WideCStr> for WideCString
[src]
impl ToOwned for WideCStr
[src]
type Owned = WideCString
fn to_owned(&self) -> WideCString
[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