Struct UString

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

An owned, mutable “wide” string for FFI that is not nul-aware.

UString 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.

UCString should be used instead if nul-aware strings are required.

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

Please prefer using the type aliases U16String or U32String or WideString to using this type directly.

§Examples

The following example constructs a U16String and shows how to convert a U16String to a regular Rust String.

use widestring::U16String;
let s = "Test";
// Create a wide string from the rust string
let wstr = U16String::from_str(s);
// Convert back to a rust string
let rust_str = wstr.to_string_lossy();
assert_eq!(rust_str, "Test");

The same example using U32String instead:

use widestring::U32String;
let s = "Test";
// Create a wide string from the rust string
let wstr = U32String::from_str(s);
// Convert back to a rust string
let rust_str = wstr.to_string_lossy();
assert_eq!(rust_str, "Test");

Implementations§

Source§

impl<C: UChar> UString<C>

Source

pub fn new() -> Self

Constructs a new empty UString.

Source

pub fn from_vec(raw: impl Into<Vec<C>>) -> Self

Constructs a UString from a vector of possibly invalid or ill-formed UTF-16 or UTF-32 data.

No checks are made on the contents of the vector.

§Examples
use widestring::U16String;
let v = vec![84u16, 104u16, 101u16]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wstr = U16String::from_vec(v);
use widestring::U32String;
let v = vec![84u32, 104u32, 101u32]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wstr = U32String::from_vec(v);
Source

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

Constructs a UString 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

Panics if len is greater than 0 but p is a null pointer.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a UString with the given capacity.

The string will be able to hold exactly capacity partial code units without reallocating. If capacity is set to 0, the string will not initially allocate.

Source

pub fn capacity(&self) -> usize

Returns the capacity this UString can hold without reallocating.

Source

pub fn clear(&mut self)

Truncate the UString to zero length.

Source

pub fn reserve(&mut self, additional: usize)

Reserves the capacity for at least additional more capacity to be inserted in the given UString.

More space may be reserved to avoid frequent allocations.

Source

pub fn reserve_exact(&mut self, additional: usize)

Reserves the minimum capacity for exactly additional more capacity to be inserted in the given UString. Does nothing if the capcity is already sufficient.

Note that the allocator may give more space than is requested. Therefore capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Source

pub fn into_vec(self) -> Vec<C>

Converts the wide string into a Vec, consuming the string in the process.

Source

pub fn as_ustr(&self) -> &UStr<C>

Converts to a UStr reference.

Source

pub fn push(&mut self, s: impl AsRef<UStr<C>>)

Extends the wide string with the given &UStr.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

§Examples
use widestring::U16String;
let s = "MyString";
let mut wstr = U16String::from_str(s);
let cloned = wstr.clone();
// Push the clone to the end, repeating the string twice.
wstr.push(cloned);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
use widestring::U32String;
let s = "MyString";
let mut wstr = U32String::from_str(s);
let cloned = wstr.clone();
// Push the clone to the end, repeating the string twice.
wstr.push(cloned);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
Source

pub fn push_slice(&mut self, s: impl AsRef<[C]>)

Extends the wide string with the given slice.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

§Examples
use widestring::U16String;
let s = "MyString";
let mut wstr = U16String::from_str(s);
let cloned = wstr.clone();
// Push the clone to the end, repeating the string twice.
wstr.push_slice(cloned);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
use widestring::U32String;
let s = "MyString";
let mut wstr = U32String::from_str(s);
let cloned = wstr.clone();
// Push the clone to the end, repeating the string twice.
wstr.push_slice(cloned);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the UString to match its length.

§Examples
use widestring::U16String;

let mut s = U16String::from_str("foo");

s.reserve(100);
assert!(s.capacity() >= 100);

s.shrink_to_fit();
assert_eq!(3, s.capacity());
use widestring::U32String;

let mut s = U32String::from_str("foo");

s.reserve(100);
assert!(s.capacity() >= 100);

s.shrink_to_fit();
assert_eq!(3, s.capacity());
Source

pub fn into_boxed_ustr(self) -> Box<UStr<C>>

Converts this UString into a boxed UStr.

§Examples
use widestring::{U16String, U16Str};

let s = U16String::from_str("hello");

let b: Box<U16Str> = s.into_boxed_ustr();
use widestring::{U32String, U32Str};

let s = U32String::from_str("hello");

let b: Box<U32Str> = s.into_boxed_ustr();
Source§

impl UString<u16>

Source

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

Encodes a U16String copy from a str.

This makes a wide string copy of the str. Since str will always be valid UTF-8, the resulting U16String will also be valid UTF-16.

§Examples
use widestring::U16String;
let s = "MyString";
// Create a wide string from the string
let wstr = U16String::from_str(s);

assert_eq!(wstr.to_string().unwrap(), s);
Source

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

Encodes a U16String copy from an OsStr.

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

§Examples
use widestring::U16String;
let s = "MyString";
// Create a wide string from the string
let wstr = U16String::from_os_str(s);

assert_eq!(wstr.to_string().unwrap(), s);
Source

pub fn push_str(&mut self, s: impl AsRef<str>)

Extends the string with the given &str.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

§Examples
use widestring::U16String;
let s = "MyString";
let mut wstr = U16String::from_str(s);
// Push the original to the end, repeating the string twice.
wstr.push_str(s);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
Source

pub fn push_os_str(&mut self, s: impl AsRef<OsStr>)

Extends the string with the given &OsStr.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

§Examples
use widestring::U16String;
let s = "MyString";
let mut wstr = U16String::from_str(s);
// Push the original to the end, repeating the string twice.
wstr.push_os_str(s);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
Source§

impl UString<u32>

Source

pub fn from_chars(raw: Vec<char>) -> Self

Constructs a U32String from a vector of UTF-32 data.

No checks are made on the contents of the vector.

§Examples
use widestring::U32String;
let v: Vec<char> = "Test".chars().collect();
// Create a wide string from the vector
let wstr = U32String::from_chars(v);
Source

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

Encodes a U32String copy from a str.

This makes a wide string copy of the str. Since str will always be valid UTF-8, the resulting U32String will also be valid UTF-32.

§Examples
use widestring::U32String;
let s = "MyString";
// Create a wide string from the string
let wstr = U32String::from_str(s);

assert_eq!(wstr.to_string().unwrap(), s);
Source

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

Encodes a U32String copy from an OsStr.

This makes a wide string copy of the OsStr. Since OsStr makes no guarantees that it is valid data, there is no guarantee that the resulting U32String will be valid UTF-32.

§Examples
use widestring::U32String;
let s = "MyString";
// Create a wide string from the string
let wstr = U32String::from_os_str(s);

assert_eq!(wstr.to_string().unwrap(), s);
Source

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

Constructs a U32String 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

Panics if len is greater than 0 but p is a null pointer.

Source

pub fn push_str(&mut self, s: impl AsRef<str>)

Extends the string with the given &str.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

§Examples
use widestring::U32String;
let s = "MyString";
let mut wstr = U32String::from_str(s);
// Push the original to the end, repeating the string twice.
wstr.push_str(s);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");
Source

pub fn push_os_str(&mut self, s: impl AsRef<OsStr>)

Extends the string with the given &OsStr.

No checks are performed on the strings. It is possible to end up nul values inside the string, and it is up to the caller to determine if that is acceptable.

§Examples
use widestring::U32String;
let s = "MyString";
let mut wstr = U32String::from_str(s);
// Push the original to the end, repeating the string twice.
wstr.push_os_str(s);

assert_eq!(wstr.to_string().unwrap(), "MyStringMyString");

Methods from Deref<Target = UStr<C>>§

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.

Trait Implementations§

Source§

impl<C: UChar> AsRef<[C]> for UString<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 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: Clone + UChar> Clone for UString<C>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

impl<C: Default + UChar> Default for UString<C>

Source§

fn default() -> UString<C>

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

impl<C: UChar> Deref for UString<C>

Source§

type Target = UStr<C>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl From<&String> for UString<u16>

Source§

fn from(s: &String) -> Self

Converts to this type from the input type.
Source§

impl From<&String> for UString<u32>

Source§

fn from(s: &String) -> Self

Converts to this type from the input type.
Source§

impl<'a, C: UChar, T: ?Sized + AsRef<UStr<C>>> From<&'a T> for UString<C>

Source§

fn from(s: &'a T) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for UString<u16>

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for UString<u32>

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(boxed: Box<UStr<C>>) -> Self

Converts to this type from the input type.
Source§

impl From<OsString> for UString<u16>

Source§

fn from(s: OsString) -> Self

Converts to this type from the input type.
Source§

impl From<OsString> for UString<u32>

Source§

fn from(s: OsString) -> Self

Converts to this type from the input type.
Source§

impl From<String> for UString<u16>

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<String> for UString<u32>

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<C: UChar> From<UCString<C>> for UString<C>

Source§

fn from(s: UCString<C>) -> 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<'a> From<UString<u16>> for Cow<'a, UStr<u16>>

Source§

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

Converts to this type from the input type.
Source§

impl From<UString<u16>> for OsString

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<UString<u32>> for OsString

Source§

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

Converts to this type from the input type.
Source§

impl FromStr for UString<u16>

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl FromStr for UString<u32>

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

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

Source§

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

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

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<C: UChar> Index<RangeFull> for UString<C>

Source§

type Output = UStr<C>

The returned type after indexing.
Source§

fn index(&self, _index: RangeFull) -> &UStr<C>

Performs the indexing (container[index]) operation. Read more
Source§

impl Into<UString<u16>> for Vec<u16>

Source§

fn into(self) -> UString<u16>

Converts this type into the (usually inferred) input type.
Source§

impl Into<UString<u32>> for Vec<char>

Source§

fn into(self) -> UString<u32>

Converts this type into the (usually inferred) input type.
Source§

impl Into<UString<u32>> for Vec<u32>

Source§

fn into(self) -> UString<u32>

Converts this type into the (usually inferred) input type.
Source§

impl<C: UChar> Into<Vec<C>> for UString<C>

Source§

fn into(self) -> Vec<C>

Converts this type into the (usually inferred) input type.
Source§

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

Source§

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

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

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. 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<'a, C: UChar> PartialEq<Cow<'a, UStr<C>>> for UString<C>

Source§

fn eq(&self, other: &Cow<'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 UString<C>

Source§

fn eq(&self, other: &UString<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<'a, C: UChar> PartialOrd<Cow<'a, UStr<C>>> for UString<C>

Source§

fn partial_cmp(&self, other: &Cow<'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 UString<C>

Source§

fn partial_cmp(&self, other: &UString<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: Eq + UChar> Eq for UString<C>

Source§

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

Auto Trait Implementations§

§

impl<C> Freeze for UString<C>

§

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

§

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

§

impl<C> Sync for UString<C>
where C: Sync,

§

impl<C> Unpin for UString<C>
where C: Unpin,

§

impl<C> UnwindSafe for UString<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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.