Struct UStr

Source
pub struct UStr<C: UChar> { /* private fields */ }
Expand description

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§

Source§

impl<C: UChar> UStr<C>

Source

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

Coerces a value into a UStr.

Source

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

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.

Source

pub fn from_slice(slice: &[C]) -> &Self

Constructs a UStr from a slice of code points.

No checks are performed on the slice.

Source

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

Copies the wide string to a new owned UString.

Source

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

Converts to a slice of the wide string.

Source

pub fn as_ptr(&self) -> *const C

Returns a raw pointer to the wide string.

The pointer is valid only as long as the lifetime of this reference.

Source

pub fn len(&self) -> usize

Returns the length of the wide string as number of elements (not number of bytes).

Source

pub fn is_empty(&self) -> bool

Returns whether this wide string contains no data.

Source

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

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

Source§

impl UStr<u16>

Source

pub fn to_os_string(&self) -> OsString

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));
Source

pub fn to_string(&self) -> Result<String, FromUtf16Error>

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);
Source

pub fn to_string_lossy(&self) -> String

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);
Source§

impl UStr<u32>

Source

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

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.

Source

pub fn from_char_slice(slice: &[char]) -> &Self

Constructs a U32Str from a slice of u32 code points.

No checks are performed on the slice.

Source

pub fn to_os_string(&self) -> OsString

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));
Source

pub fn to_string(&self) -> Result<String, FromUtf32Error>

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);
Source

pub fn to_string_lossy(&self) -> String

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§

Source§

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

Source§

fn as_ref(&self) -> &[C]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<C: UChar> AsRef<UStr<C>> for UStr<C>

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<C: UChar> AsRef<UStr<C>> for UString<C>

Source§

fn as_ref(&self) -> &UStr<C>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<C: UChar> Borrow<UStr<C>> for UString<C>

Source§

fn borrow(&self) -> &UStr<C>

Immutably borrows from an owned value. Read more
Source§

impl<C: Debug + UChar> Debug for UStr<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: UChar> Default for Box<UStr<C>>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

fn from(s: &'a UStr<C>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(s: &'a UStr<u16>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(s: &'a UStr<u32>) -> Self

Converts to this type from the input type.
Source§

impl<C: UChar> From<UString<C>> for Box<UStr<C>>

Source§

fn from(s: UString<C>) -> Self

Converts to this type from the input type.
Source§

impl<C: Hash + UChar> Hash for UStr<C>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
Source§

impl<C: Ord + UChar> Ord for UStr<C>

Source§

fn cmp(&self, other: &UStr<C>) -> Ordering

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

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

Source§

fn eq(&self, other: &&'a UStr<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C: UChar> PartialEq<UStr<C>> for UString<C>

Source§

fn eq(&self, other: &UStr<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C: PartialEq + UChar> PartialEq for UStr<C>

Source§

fn eq(&self, other: &UStr<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn partial_cmp(&self, other: &&'a UStr<C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<C: UChar> PartialOrd<UStr<C>> for UString<C>

Source§

fn partial_cmp(&self, other: &UStr<C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<C: PartialOrd + UChar> PartialOrd for UStr<C>

Source§

fn partial_cmp(&self, other: &UStr<C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<C: UChar> ToOwned for UStr<C>

Source§

type Owned = UString<C>

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> UString<C>

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · Source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<C: Eq + UChar> Eq for UStr<C>

Source§

impl<C: UChar> StructuralPartialEq for UStr<C>

Auto Trait Implementations§

§

impl<C> Freeze for UStr<C>
where C: Freeze,

§

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

§

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

§

impl<C> !Sized for UStr<C>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more