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>
impl<C: UChar> UString<C>
Sourcepub fn from_vec(raw: impl Into<Vec<C>>) -> Self
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);Sourcepub fn with_capacity(capacity: usize) -> Self
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.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity this UString can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
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.
Sourcepub fn reserve_exact(&mut self, additional: usize)
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.
Sourcepub fn into_vec(self) -> Vec<C>
pub fn into_vec(self) -> Vec<C>
Converts the wide string into a Vec, consuming the string in the process.
Sourcepub fn push(&mut self, s: impl AsRef<UStr<C>>)
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");Sourcepub fn push_slice(&mut self, s: impl AsRef<[C]>)
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");Sourcepub fn shrink_to_fit(&mut self)
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());Sourcepub fn into_boxed_ustr(self) -> Box<UStr<C>>
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>
impl UString<u16>
Sourcepub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self
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);Sourcepub fn from_os_str<S: AsRef<OsStr> + ?Sized>(s: &S) -> Self
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);Sourcepub fn push_str(&mut self, s: impl AsRef<str>)
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");Sourcepub fn push_os_str(&mut self, s: impl AsRef<OsStr>)
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>
impl UString<u32>
Sourcepub fn from_chars(raw: Vec<char>) -> Self
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);Sourcepub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self
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);Sourcepub fn from_os_str<S: AsRef<OsStr> + ?Sized>(s: &S) -> Self
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);Sourcepub unsafe fn from_char_ptr(p: *const char, len: usize) -> Self
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.
Sourcepub fn push_str(&mut self, s: impl AsRef<str>)
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");Sourcepub fn push_os_str(&mut self, s: impl AsRef<OsStr>)
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>>§
Sourcepub fn to_ustring(&self) -> UString<C>
pub fn to_ustring(&self) -> UString<C>
Copies the wide string to a new owned UString.
Sourcepub fn as_ptr(&self) -> *const C
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.