pub type WideCString = U32CString;
Expand description
Alias for U16String
or U32String
depending on platform. Intended to match typical C
wchar_t
size on platform.
Aliased Type§
struct WideCString { /* private fields */ }
Implementations
Source§impl U32CString
impl U32CString
Sourcepub fn from_chars(v: impl Into<Vec<char>>) -> Result<Self, ContainsNul<u32>>
pub fn from_chars(v: impl Into<Vec<char>>) -> Result<Self, ContainsNul<u32>>
Constructs a U32CString
from a container of character data, checking for invalid nul
values.
This method will consume the provided data and use the underlying elements to construct a new string. The data will be scanned for invalid nul values anywhere except the last character. The resulting string will always be nul-terminated even if the original string is not.
§Errors
This function will return an error if the data contains a nul value anywhere except the
last character.
The returned error will contain the Vec<u32>
as well as the position of the nul value.
§Examples
use widestring::U32CString;
let v: Vec<char> = "Test".chars().collect();
// Create a wide string from the vector
let wcstr = U32CString::from_chars(v).unwrap();
The following example demonstrates errors from nul values in a vector.
use widestring::U32CString;
let v: Vec<char> = "T\u{0}est".chars().collect();
// Create a wide string from the vector
let res = U32CString::from_chars(v);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 1);
Sourcepub fn from_chars_truncate(v: impl Into<Vec<char>>) -> Self
pub fn from_chars_truncate(v: impl Into<Vec<char>>) -> Self
Constructs a U32CString
from a container of character data, truncating at the first nul
value.
This method will consume the provided data and use the underlying elements to construct a new string. The string will be truncated at the first nul value in the string. The resulting string will always be nul-terminated even if the original string is not.
§Examples
use widestring::U32CString;
let v: Vec<char> = "Test\u{0}".chars().collect();
// Create a wide string from the vector
let wcstr = U32CString::from_chars_truncate(v);
Sourcepub unsafe fn from_chars_unchecked(v: impl Into<Vec<char>>) -> Self
pub unsafe fn from_chars_unchecked(v: impl Into<Vec<char>>) -> Self
Constructs a U32CString
from character data without checking for nul values.
A terminating nul value will be appended if the vector does not already have a terminating nul.
§Safety
This method is equivalent to from_chars
except that no runtime
assertion is made that v
contains no interior nul values. Providing a vector with nul
values anywhere but the last character will result in an invalid U32CString
.
Sourcepub fn from_str(s: impl AsRef<str>) -> Result<Self, ContainsNul<u32>>
pub fn from_str(s: impl AsRef<str>) -> Result<Self, ContainsNul<u32>>
Constructs a U32CString
copy from a str
, encoding it as UTF-32 and checking for
invalid interior nul values.
This makes a string copy of the str
. Since str
will always be valid UTF-8, the
resulting U32CString
will also be valid UTF-32.
The string will be scanned for nul values, which are invalid anywhere except the last character. The resulting string will always be nul-terminated even if the original string is not.
§Errors
This function will return an error if the data contains a nul value anywhere except the
last character.
The returned error will contain a Vec<u32>
as well as the position of the nul value.
§Examples
use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = U32CString::from_str(s).unwrap();
The following example demonstrates errors from nul values in a string.
use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let res = U32CString::from_str(s);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 2);
Sourcepub unsafe fn from_str_unchecked(s: impl AsRef<str>) -> Self
pub unsafe fn from_str_unchecked(s: impl AsRef<str>) -> Self
Constructs a U32CString
copy from a str
, encoding it as UTF-32, without checking for
nul values.
This makes a string copy of the str
. Since str
will always be valid UTF-8, the
resulting U32CString
will also be valid UTF-32.
The resulting string will always be nul-terminated even if the original string is not.
§Safety
This method is equivalent to from_str
except that no runtime assertion
is made that s
contains invalid nul values. Providing a string with nul values anywhere
except the last character will result in an invalid U32CString
.
§Examples
use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = unsafe { U32CString::from_str_unchecked(s) };
Sourcepub fn from_str_truncate(s: impl AsRef<str>) -> Self
pub fn from_str_truncate(s: impl AsRef<str>) -> Self
Constructs a U32CString
copy from a str
, encoding it as UTF-32, truncating at the
first nul terminator.
This makes a string copy of the str
. Since str
will always be valid UTF-8, the
resulting U32CString
will also be valid UTF-32.
The string will be truncated at the first nul value in the string. The resulting string will always be nul-terminated even if the original string is not.
§Examples
use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let wcstr = U32CString::from_str_truncate(s);
assert_eq!(wcstr.to_string_lossy(), "My");
Sourcepub unsafe fn from_char_ptr_str(p: *const char) -> Self
pub unsafe fn from_char_ptr_str(p: *const char) -> Self
Constructs a new wide C string copied from a nul-terminated char
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.
If you wish to avoid copying the string pointer, use U32CStr::from_char_ptr_str
instead.
§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.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
§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.
Sourcepub unsafe fn from_char_ptr(
p: *const char,
len: usize,
) -> Result<Self, ContainsNul<u32>>
pub unsafe fn from_char_ptr( p: *const char, len: usize, ) -> Result<Self, ContainsNul<u32>>
Constructs a wide C string copied from a char
pointer and a length, checking for invalid
interior nul values.
The len
argument is the number of elements, not the number of bytes, and does
not include the nul terminator of the string. If len
is 0
, p
is allowed to be a
null pointer.
The resulting string will always be nul-terminated even if the pointer data is not.
§Errors
This will scan the pointer string for an interior nul value and error if one is found. To
avoid scanning for interior nuls, from_ptr_unchecked
may be
used instead.
The returned error will contain a Vec
as well as the position of the nul value.
§Safety
This function is unsafe as there is no guarantee that the given pointer is valid for len
elements.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
§Panics
Panics if len
is greater than 0 but p
is a null pointer.
Sourcepub unsafe fn from_char_ptr_truncate(p: *const char, len: usize) -> Self
pub unsafe fn from_char_ptr_truncate(p: *const char, len: usize) -> Self
Constructs a wide C string copied from a char
pointer and a length, truncating at the
first nul terminator.
The len
argument is the number of elements, not the number of bytes. This will scan
for nul values beginning with p
until offset len
. The first nul value will be used as
the nul terminator for the string, ignoring any remaining values left before len
. If no
nul value is found, the whole string of length len
is used, and a new nul-terminator
will be added to the resulting string. If len
is 0
, p
is allowed to be a null pointer.
§Safety
This function is unsafe as there is no guarantee that the given pointer is valid for len
elements.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
§Panics
Panics if len
is greater than 0 but p
is a null pointer.
Sourcepub unsafe fn from_char_ptr_unchecked(p: *const char, len: usize) -> Self
pub unsafe fn from_char_ptr_unchecked(p: *const char, len: usize) -> Self
Constructs a wide C string copied from a char
pointer and a length without checking for
any nul values.
The len
argument is the number of elements, not the number of bytes, and does
not include the nul terminator of the string. If len
is 0
, p
is allowed to be a
null pointer.
The resulting string will always be nul-terminated even if the pointer data is not.
§Safety
This function is unsafe as there is no guarantee that the given pointer is valid for len
elements.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
The interior values of the pointer are not scanned for nul. Any interior nul values or will result in an invalid C string.
§Panics
Panics if len
is greater than 0 but p
is a null pointer.
Sourcepub fn from_os_str(s: impl AsRef<OsStr>) -> Result<Self, ContainsNul<u32>>
pub fn from_os_str(s: impl AsRef<OsStr>) -> Result<Self, ContainsNul<u32>>
Constructs a U32CString
copy from an OsStr
, checking for invalid
nul values.
This makes a string copy of the OsStr
. Since OsStr
makes no guarantees that it is valid data, there is no guarantee that the resulting
U32CString
will be valid UTF-32.
The string will be scanned for nul values, which are invlaid anywhere except the last character. The resulting string will always be nul-terminated even if the string is not.
Note that the encoding of OsStr
is platform-dependent, so on
some platforms this may make an encoding conversions, while on other platforms no changes to
the string will be made.
§Errors
This function will return an error if the data contains a nul value anywhere except the
last character.
The returned error will contain a Vec<u32>
as well as the position of the nul value.
§Examples
use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = U32CString::from_os_str(s).unwrap();
The following example demonstrates errors from nul values in a string.
use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let res = U32CString::from_os_str(s);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 2);
Sourcepub unsafe fn from_os_str_unchecked(s: impl AsRef<OsStr>) -> Self
pub unsafe fn from_os_str_unchecked(s: impl AsRef<OsStr>) -> Self
Constructs a U32CString
copy from an OsStr
, without checking for
nul values.
This makes a string copy of the OsStr
. Since OsStr
makes no guarantees that it is valid data, there is no guarantee that the resulting
U32CString
will be valid UTF-32.
The resulting string will always be nul-terminated even if the string is not.
Note that the encoding of OsStr
is platform-dependent, so on
some platforms this may make an encoding conversions, while on other platforms no changes to
the string will be made.
§Safety
This method is equivalent to from_os_str
except that no runtime
assertion is made that s
contains invalid nul values. Providing a string with nul values
anywhere except the last character will result in an invalid U32CString
.
§Examples
use widestring::U32CString;
let s = "MyString";
// Create a wide string from the string
let wcstr = unsafe { U32CString::from_os_str_unchecked(s) };
Sourcepub fn from_os_str_truncate(s: impl AsRef<OsStr>) -> Self
pub fn from_os_str_truncate(s: impl AsRef<OsStr>) -> Self
Constructs a U32CString
copy from an OsStr
, truncating at the first
nul terminator.
This makes a string copy of the OsStr
. Since OsStr
makes no guarantees that it is valid data, there is no guarantee that the resulting
U32CString
will be valid UTF-32.
The string will be truncated at the first nul value in the string. The resulting string will always be nul-terminated even if the string is not.
Note that the encoding of OsStr
is platform-dependent, so on
some platforms this may make an encoding conversions, while on other platforms no changes to
the string will be made.
§Examples
use widestring::U32CString;
let s = "My\u{0}String";
// Create a wide string from the string
let wcstr = U32CString::from_os_str_truncate(s);
assert_eq!(wcstr.to_string_lossy(), "My");
Source§impl U32CString
impl U32CString
Sourcepub const NUL_TERMINATOR: u32 = 0u32
pub const NUL_TERMINATOR: u32 = 0u32
The nul terminator character value.
Sourcepub fn from_vec(v: impl Into<Vec<u32>>) -> Result<Self, ContainsNul<u32>>
pub fn from_vec(v: impl Into<Vec<u32>>) -> Result<Self, ContainsNul<u32>>
Constructs a wide C string from a container of wide character data.
This method will consume the provided data and use the underlying elements to construct a new string. The data will be scanned for invalid interior nul values.
§Errors
This function will return an error if the data contains a nul value that is not the
terminating nul.
The returned error will contain the original Vec
as well as the position of the
nul value.
§Examples
use widestring::U32CString;
let v = vec![84u32, 104u32, 101u32]; // 'T' 'h' 'e'
// Create a wide string from the vector
let wcstr = U32CString::from_vec(v).unwrap();
Empty vectors are valid and will return an empty string with a nul terminator:
use widestring::U32CString;
let wcstr = U32CString::from_vec(vec![]).unwrap();
assert_eq!(wcstr, U32CString::default());
The following example demonstrates errors from nul values in a vector.
use widestring::U32CString;
let v = vec![84u32, 0u32, 104u32, 101u32]; // 'T' NUL 'h' 'e'
// Create a wide string from the vector
let res = U32CString::from_vec(v);
assert!(res.is_err());
assert_eq!(res.err().unwrap().nul_position(), 1);
Sourcepub fn from_vec_truncate(v: impl Into<Vec<u32>>) -> Self
pub fn from_vec_truncate(v: impl Into<Vec<u32>>) -> Self
Constructs a wide C string from a container of wide character data, truncating at the first nul terminator.
The string will be truncated at the first nul value in the data.
§Examples
use widestring::U32CString;
let v = vec![84u32, 104u32, 101u32, 0u32]; // 'T' 'h' 'e' NUL
// Create a wide string from the vector
let wcstr = U32CString::from_vec_truncate(v);
Sourcepub unsafe fn from_vec_unchecked(v: impl Into<Vec<u32>>) -> Self
pub unsafe fn from_vec_unchecked(v: impl Into<Vec<u32>>) -> Self
Constructs a wide C string from a vector without checking for interior nul values.
A terminating nul value will be appended if the vector does not already have a terminating nul.
§Safety
This method is equivalent to from_vec
except that no runtime
assertion is made that v
contains no interior nul values. Providing a vector with
any nul values that are not the last value in the vector will result in an invalid
C string.
Sourcepub fn from_ustr(s: impl AsRef<U32Str>) -> Result<Self, ContainsNul<u32>>
pub fn from_ustr(s: impl AsRef<U32Str>) -> Result<Self, ContainsNul<u32>>
Constructs a wide C string from anything that can be converted to a wide string slice.
The string will be scanned for invalid interior nul values.
§Errors
This function will return an error if the data contains a nul value that is not the
terminating nul.
The returned error will contain a Vec
as well as the position of the nul value.
Sourcepub fn from_ustr_truncate(s: impl AsRef<U32Str>) -> Self
pub fn from_ustr_truncate(s: impl AsRef<U32Str>) -> Self
Constructs a wide C string from anything that can be converted to a wide string slice, truncating at the first nul terminator.
The string will be truncated at the first nul value in the string.
Sourcepub unsafe fn from_ustr_unchecked(s: impl AsRef<U32Str>) -> Self
pub unsafe fn from_ustr_unchecked(s: impl AsRef<U32Str>) -> Self
Constructs a wide C string from anything that can be converted to a wide string slice, without scanning for invalid nul values.
§Safety
This method is equivalent to from_ustr
except that no runtime
assertion is made that v
contains no interior nul values. Providing a string with
any nul values that are not the last value in the vector will result in an invalid
C string.
Sourcepub unsafe fn from_ptr_str(p: *const u32) -> Self
pub unsafe fn from_ptr_str(p: *const u32) -> Self
Constructs a new wide C string copied from a 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.
If you wish to avoid copying the string pointer, use U16CStr::from_ptr_str
or
U32CStr::from_ptr_str
instead.
§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.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
§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.
Sourcepub unsafe fn from_ptr(
p: *const u32,
len: usize,
) -> Result<Self, ContainsNul<u32>>
pub unsafe fn from_ptr( p: *const u32, len: usize, ) -> Result<Self, ContainsNul<u32>>
Constructs a wide C string copied from a pointer and a length, checking for invalid interior nul values.
The len
argument is the number of elements, not the number of bytes, and does
not include the nul terminator of the string. If len
is 0
, p
is allowed to
be a null pointer.
The resulting string will always be nul-terminated even if the pointer data is not.
§Errors
This will scan the pointer string for an interior nul value and error if one is
found. To avoid scanning for interior nuls,
from_ptr_unchecked
may be used instead.
The returned error will contain a Vec
as well as the position of the nul value.
§Safety
This function is unsafe as there is no guarantee that the given pointer is valid for
len
elements.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
§Panics
Panics if len
is greater than 0 but p
is a null pointer.
Sourcepub unsafe fn from_ptr_truncate(p: *const u32, len: usize) -> Self
pub unsafe fn from_ptr_truncate(p: *const u32, len: usize) -> Self
Constructs a wide C string copied from a pointer and a length, truncating at the first nul terminator.
The len
argument is the number of elements, not the number of bytes. This will
scan for nul values beginning with p
until offset len
. The first nul value will
be used as the nul terminator for the string, ignoring any remaining values left
before len
. If no nul value is found, the whole string of length len
is used,
and a new nul-terminator will be added to the resulting string. If len
is 0
, p
is allowed to be a null pointer.
§Safety
This function is unsafe as there is no guarantee that the given pointer is valid for
len
elements.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
§Panics
Panics if len
is greater than 0 but p
is a null pointer.
Sourcepub unsafe fn from_ptr_unchecked(p: *const u32, len: usize) -> Self
pub unsafe fn from_ptr_unchecked(p: *const u32, len: usize) -> Self
Constructs a wide C string copied from a pointer and a length without checking for any nul values.
The len
argument is the number of elements, not the number of bytes, and does
not include the nul terminator of the string. If len
is 0
, p
is allowed to
be a null pointer.
The resulting string will always be nul-terminated even if the pointer data is not.
§Safety
This function is unsafe as there is no guarantee that the given pointer is valid for
len
elements.
In addition, the data must meet the safety conditions of std::slice::from_raw_parts.
The interior values of the pointer are not scanned for nul. Any interior nul values or will result in an invalid C string.
§Panics
Panics if len
is greater than 0 but p
is a null pointer.
Sourcepub fn as_mut_ucstr(&mut self) -> &mut U32CStr
pub fn as_mut_ucstr(&mut self) -> &mut U32CStr
Converts to a mutable wide C string slice.
Sourcepub fn into_ustring(self) -> U32String
pub fn into_ustring(self) -> U32String
Converts this string into a wide string without a nul terminator.
The resulting string will not contain a nul-terminator, and will contain no other nul values.
Sourcepub fn into_ustring_with_nul(self) -> U32String
pub fn into_ustring_with_nul(self) -> U32String
Converts this string into a wide string with a nul terminator.
The resulting vector will contain a nul-terminator and no interior nul values.
Sourcepub fn into_vec(self) -> Vec<u32>
pub fn into_vec(self) -> Vec<u32>
Converts the string into a Vec
without a nul terminator, consuming the string in
the process.
The resulting vector will not contain a nul-terminator, and will contain no other nul values.
Sourcepub fn into_vec_with_nul(self) -> Vec<u32>
pub fn into_vec_with_nul(self) -> Vec<u32>
Converts the string into a Vec
, consuming the string in the process.
The resulting vector will contain a nul-terminator and no interior nul values.
Sourcepub unsafe fn from_raw(p: *mut u32) -> Self
pub unsafe fn from_raw(p: *mut u32) -> Self
Retakes ownership of a wide C string that was transferred to C.
This should only be used in combination with into_raw
. To
construct a new wide C string from a pointer, use
from_ptr_str
.
§Safety
This should only ever be called with a pointer that was earlier obtained by calling
into_raw
. Additionally, the length of the string will be
recalculated from the pointer by scanning for the nul-terminator.
§Panics
Panics if p
is a null pointer.
Sourcepub fn into_boxed_ucstr(self) -> Box<U32CStr>
pub fn into_boxed_ucstr(self) -> Box<U32CStr>
Converts this wide C string into a boxed wide C string slice.
§Examples
use widestring::{U32CString, U32CStr};
let mut v = vec![102u32, 111u32, 111u32]; // "foo"
let c_string = U32CString::from_vec(v.clone()).unwrap();
let boxed = c_string.into_boxed_ucstr();
v.push(0);
assert_eq!(&*boxed, U32CStr::from_slice(&v).unwrap());
Trait Implementations
Source§impl AsMut<U32CStr> for U32CString
impl AsMut<U32CStr> for U32CString
Source§impl AsRef<[u32]> for U32CString
impl AsRef<[u32]> for U32CString
Source§impl AsRef<U32CStr> for U32CString
impl AsRef<U32CStr> for U32CString
Source§impl AsRef<U32Str> for U32CString
impl AsRef<U32Str> for U32CString
Source§impl Borrow<U32CStr> for U32CString
impl Borrow<U32CStr> for U32CString
Source§impl BorrowMut<U32CStr> for U32CString
impl BorrowMut<U32CStr> for U32CString
Source§fn borrow_mut(&mut self) -> &mut U32CStr
fn borrow_mut(&mut self) -> &mut U32CStr
Source§impl Clone for U32CString
impl Clone for U32CString
Source§fn clone(&self) -> U32CString
fn clone(&self) -> U32CString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more