[][src]Struct widestring::UStr

pub struct UStr<C: UChar> { /* fields omitted */ }

String slice reference for U16String.

UStr is to UString as str is to String.

UStr is not aware of nul values. Strings may or may not be nul-terminated, and may contain invalid and ill-formed UTF-16 or UTF-32 data. These strings are intended to be used with 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.

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

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

Please prefer using the type aliases U16Str or U32Str or WideStr to using this type directly.

Implementations

impl<C: UChar> UStr<C>[src]

pub fn new<S: AsRef<Self> + ?Sized>(s: &S) -> &Self[src]

Coerces a value into a UStr.

pub unsafe fn from_ptr<'a>(p: *const C, len: usize) -> &'a Self[src]

Constructs a UStr from a pointer and a length.

The len argument is the number of 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(slice: &[C]) -> &Self[src]

Constructs a UStr from a slice of code points.

No checks are performed on the slice.

pub fn to_ustring(&self) -> UString<C>[src]

Copies the wide string to a new owned UString.

pub fn as_slice(&self) -> &[C][src]

Converts to a slice of the wide string.

pub fn as_ptr(&self) -> *const C[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 elements (not number of bytes).

pub fn is_empty(&self) -> bool[src]

Returns whether this wide string contains no data.

pub fn into_ustring(self: Box<Self>) -> UString<C>[src]

Converts a Box<UStr> into a UString without copying or allocating.

impl UStr<u16>[src]

pub fn to_os_string(&self) -> OsString[src]

Decodes a wide string to an owned OsString.

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

Examples

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

assert_eq!(lossy, s);

impl UStr<u32>[src]

pub unsafe fn from_char_ptr<'a>(p: *const char, len: usize) -> &'a Self[src]

Constructs a U32Str from a char pointer and a length.

The len argument is the number of char 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_char_slice(slice: &[char]) -> &Self[src]

Constructs a U32Str from a slice of u32 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 U32Str. Since U32Str makes no guarantees that it is valid UTF-32, there is no guarantee that the resulting OsString will be valid data.

Examples

use widestring::U32String;
use std::ffi::OsString;
let s = "MyString";
// Create a wide string from the string
let wstr = U32String::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_string(&self) -> Result<String, FromUtf32Error>[src]

Copies the wide string to a String if it contains valid UTF-32 data.

Failures

Returns an error if the string contains any invalid UTF-32 data.

Examples

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

assert_eq!(lossy, s);

Trait Implementations

impl<C: UChar> AsRef<[C]> for UStr<C>[src]

impl<C: UChar> AsRef<UStr<C>> for UStr<C>[src]

impl<C: UChar> AsRef<UStr<C>> for UString<C>[src]

impl<C: UChar> Borrow<UStr<C>> for UString<C>[src]

impl<C: Debug + UChar> Debug for UStr<C>[src]

impl<C: Eq + UChar> Eq for UStr<C>[src]

impl<'a, C: UChar> From<&'a UStr<C>> for Box<UStr<C>>[src]

impl<'a> From<&'a UStr<u16>> for Cow<'a, UStr<u16>>[src]

impl<'a> From<&'a UStr<u32>> for Cow<'a, UStr<u32>>[src]

impl<C: Hash + UChar> Hash for UStr<C>[src]

impl<C: Ord + UChar> Ord for UStr<C>[src]

impl<'a, C: UChar> PartialEq<&'a UStr<C>> for UString<C>[src]

impl<C: PartialEq + UChar> PartialEq<UStr<C>> for UStr<C>[src]

impl<C: UChar> PartialEq<UStr<C>> for UString<C>[src]

impl<'a, C: UChar> PartialOrd<&'a UStr<C>> for UString<C>[src]

impl<C: PartialOrd + UChar> PartialOrd<UStr<C>> for UStr<C>[src]

impl<C: UChar> PartialOrd<UStr<C>> for UString<C>[src]

impl<C: UChar> StructuralEq for UStr<C>[src]

impl<C: UChar> StructuralPartialEq for UStr<C>[src]

impl<C: UChar> ToOwned for UStr<C>[src]

type Owned = UString<C>

The resulting type after obtaining ownership.

Auto Trait Implementations

impl<C> RefUnwindSafe for UStr<C> where
    C: RefUnwindSafe

impl<C> Send for UStr<C> where
    C: Send

impl<C> Sync for UStr<C> where
    C: Sync

impl<C> Unpin for UStr<C> where
    C: Unpin

impl<C> UnwindSafe for UStr<C> where
    C: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.