pub struct PCWSTRGuard { /* private fields */ }Expand description
Prevents Self from being dropped before the finish of a FFI call.
Implementations§
Methods from Deref<Target = U16CString>§
Methods from Deref<Target = U16CStr>§
pub const NUL_TERMINATOR: u16 = 0u16
Sourcepub fn to_ucstring(&self) -> U16CString
pub fn to_ucstring(&self) -> U16CString
Copies the string reference to a new owned wide C string.
Sourcepub fn to_ustring(&self) -> U16String
pub fn to_ustring(&self) -> U16String
Copies the string reference to a new owned wide string.
The resulting wide string will not have a nul terminator.
§Examples
use widestring::U16CString;
let wcstr = U16CString::from_str("MyString").unwrap();
// Convert U16CString to a U16String
let wstr = wcstr.to_ustring();
// U16CString will have a terminating nul
let wcvec = wcstr.into_vec_with_nul();
assert_eq!(wcvec[wcvec.len()-1], 0);
// The resulting U16String will not have the terminating nul
let wvec = wstr.into_vec();
assert_ne!(wvec[wvec.len()-1], 0);Sourcepub fn as_slice(&self) -> &[u16]
pub fn as_slice(&self) -> &[u16]
Converts to a slice of the underlying elements.
The slice will not include the nul terminator.
Sourcepub unsafe fn as_mut_slice(&mut self) -> &mut [u16]
pub unsafe fn as_mut_slice(&mut self) -> &mut [u16]
Converts to a mutable slice of the underlying elements.
The slice will not include the nul terminator.
§Safety
This method is unsafe because you can violate the invariants of this type when mutating the slice (i.e. by adding interior nul values).
Sourcepub fn as_slice_with_nul(&self) -> &[u16]
pub fn as_slice_with_nul(&self) -> &[u16]
Converts to a slice of the underlying elements, including the nul terminator.
Sourcepub fn as_ptr(&self) -> *const u16
pub fn as_ptr(&self) -> *const u16
Returns a raw pointer to the string.
The caller must ensure that the string outlives the pointer this function returns, or else it will end up pointing to garbage.
The caller must also ensure that the memory the pointer (non-transitively) points to
is never written to (except inside an UnsafeCell) using this pointer or any
pointer derived from it. If you need to mutate the contents of the string, use
as_mut_ptr.
Modifying the container referenced by this string may cause its buffer to be reallocated, which would also make any pointers to it invalid.
Sourcepub fn as_mut_ptr(&mut self) -> *mut u16
pub fn as_mut_ptr(&mut self) -> *mut u16
Returns a mutable raw pointer to the string.
The caller must ensure that the string outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the container referenced by this string may cause its buffer to be reallocated, which would also make any pointers to it invalid.
Sourcepub fn as_ptr_range(&self) -> Range<*const u16>
pub fn as_ptr_range(&self) -> Range<*const u16>
Returns the two raw pointers spanning the string slice.
The returned range is half-open, which means that the end pointer points one past the last element of the slice. This way, an empty slice is represented by two equal pointers, and the difference between the two pointers represents the size of the slice.
See as_ptr for warnings on using these pointers. The end pointer
requires extra caution, as it does not point to a valid element in the slice.
This function is useful for interacting with foreign interfaces which use two pointers to refer to a range of elements in memory, as is common in C++.
Sourcepub fn as_mut_ptr_range(&mut self) -> Range<*mut u16>
pub fn as_mut_ptr_range(&mut self) -> Range<*mut u16>
Returns the two unsafe mutable pointers spanning the string slice.
The returned range is half-open, which means that the end pointer points one past the last element of the slice. This way, an empty slice is represented by two equal pointers, and the difference between the two pointers represents the size of the slice.
See as_mut_ptr for warnings on using these pointers. The end
pointer requires extra caution, as it does not point to a valid element in the
slice.
This function is useful for interacting with foreign interfaces which use two pointers to refer to a range of elements in memory, as is common in C++.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the string as number of elements (not number of bytes) not including nul terminator.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether this string contains no data (i.e. is only the nul terminator).
Sourcepub fn as_ustr(&self) -> &U16Str
pub fn as_ustr(&self) -> &U16Str
Returns a wide string slice to this wide C string slice.
The wide string slice will not include the nul-terminator.
Sourcepub fn as_ustr_with_nul(&self) -> &U16Str
pub fn as_ustr_with_nul(&self) -> &U16Str
Returns a wide string slice to this wide C string slice.
The wide string slice will include the nul-terminator.
Sourcepub unsafe fn as_mut_ustr(&mut self) -> &mut U16Str
pub unsafe fn as_mut_ustr(&mut self) -> &mut U16Str
Returns a mutable wide string slice to this wide C string slice.
The wide string slice will not include the nul-terminator.
§Safety
This method is unsafe because you can violate the invariants of this type when mutating the string (i.e. by adding interior nul values).
Sourcepub fn display(&self) -> Display<'_, U16CStr>
pub fn display(&self) -> Display<'_, U16CStr>
Returns an object that implements Display for printing
strings that may contain non-Unicode data.
A wide C string might data of any encoding. This function assumes the string is encoded in
UTF-16, and returns a struct implements the
Display trait in a way that decoding the string is lossy but
no heap allocations are performed, such as by
to_string_lossy.
By default, invalid Unicode data is replaced with
U+FFFD REPLACEMENT CHARACTER (�). If you wish
to simply skip any invalid Uncode data and forego the replacement, you may use the
alternate formatting with {:#}.
§Examples
Basic usage:
use widestring::U16CStr;
// 𝄞mus<invalid>ic<invalid>
let s = U16CStr::from_slice(&[
0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834, 0x0000,
]).unwrap();
assert_eq!(format!("{}", s.display()),
"𝄞mus�ic�"
);Using alternate formatting style to skip invalid values entirely:
use widestring::U16CStr;
// 𝄞mus<invalid>ic<invalid>
let s = U16CStr::from_slice(&[
0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834, 0x0000,
]).unwrap();
assert_eq!(format!("{:#}", s.display()),
"𝄞music"
);Sourcepub fn get<I>(&self, i: I) -> Option<&U16Str>
pub fn get<I>(&self, i: I) -> Option<&U16Str>
Returns a subslice of the string.
This is the non-panicking alternative to indexing the string. Returns None
whenever equivalent indexing operation would panic.
Sourcepub unsafe fn get_mut<I>(&mut self, i: I) -> Option<&mut U16Str>
pub unsafe fn get_mut<I>(&mut self, i: I) -> Option<&mut U16Str>
Returns a mutable subslice of the string.
This is the non-panicking alternative to indexing the string. Returns None
whenever equivalent indexing operation would panic.
§Safety
This method is unsafe because you can violate the invariants of this type when mutating the memory the pointer points to (i.e. by adding interior nul values).
Sourcepub unsafe fn get_unchecked<I>(&self, i: I) -> &U16Str
pub unsafe fn get_unchecked<I>(&self, i: I) -> &U16Str
Returns an unchecked subslice of the string.
This is the unchecked alternative to indexing the string.
§Safety
Callers of this function are responsible that these preconditions are satisfied:
- The starting index must not exceed the ending index;
- Indexes must be within bounds of the original slice.
Failing that, the returned string slice may reference invalid memory.
Sourcepub unsafe fn get_unchecked_mut<I>(&mut self, i: I) -> &mut U16Str
pub unsafe fn get_unchecked_mut<I>(&mut self, i: I) -> &mut U16Str
Returns aa mutable, unchecked subslice of the string.
This is the unchecked alternative to indexing the string.
§Safety
Callers of this function are responsible that these preconditions are satisfied:
- The starting index must not exceed the ending index;
- Indexes must be within bounds of the original slice.
Failing that, the returned string slice may reference invalid memory.
This method is unsafe because you can violate the invariants of this type when mutating the memory the pointer points to (i.e. by adding interior nul values).
Sourcepub fn split_at(&self, mid: usize) -> (&U16Str, &U16Str)
pub fn split_at(&self, mid: usize) -> (&U16Str, &U16Str)
Divide one string slice into two at an index.
The argument, mid, should be an offset from the start of the string.
The two slices returned go from the start of the string slice to mid, and from
mid to the end of the string slice.
To get mutable string slices instead, see the split_at_mut
method.
Sourcepub unsafe fn split_at_mut(&mut self, mid: usize) -> (&mut U16Str, &mut U16Str)
pub unsafe fn split_at_mut(&mut self, mid: usize) -> (&mut U16Str, &mut U16Str)
Divide one mutable string slice into two at an index.
The argument, mid, should be an offset from the start of the string.
The two slices returned go from the start of the string slice to mid, and from
mid to the end of the string slice.
To get immutable string slices instead, see the split_at method.
§Safety
This method is unsafe because you can violate the invariants of this type when mutating the memory the pointer points to (i.e. by adding interior nul values).
Sourcepub fn repeat(&self, n: usize) -> U16CString
pub fn repeat(&self, n: usize) -> U16CString
Creates a new owned string by repeating this string n times.
§Panics
This function will panic if the capacity would overflow.
Sourcepub fn to_os_string(&self) -> OsString
pub fn to_os_string(&self) -> OsString
Copys a string to an owned OsString.
This makes a string copy of the U16CStr. Since U16CStr makes no guarantees that it
is valid UTF-16, there is no guarantee that the resulting OsString
will be valid data. The OsString will not have a nul
terminator.
Note that the encoding of OsString is platform-dependent, so on
some platforms this may make an encoding conversions, while on other platforms (such as
windows) no changes to the string will be made.
§Examples
use widestring::U16CString;
use std::ffi::OsString;
let s = "MyString";
// Create a wide string from the string
let wstr = U16CString::from_str(s).unwrap();
// Create an OsString from the wide string
let osstr = wstr.to_os_string();
assert_eq!(osstr, OsString::from(s));Sourcepub fn to_string(&self) -> Result<String, Utf16Error>
pub fn to_string(&self) -> Result<String, Utf16Error>
Copies the string to a String if it contains valid UTF-16 data.
This method assumes this string is encoded as UTF-16 and attempts to decode it as such. It will *not have a nul terminator.
§Errors
Returns an error if the string contains any invalid UTF-16 data.
§Examples
use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wstr = U16CString::from_str(s).unwrap();
// Create a regular string from the wide string
let s2 = wstr.to_string().unwrap();
assert_eq!(s2, s);Sourcepub fn to_string_lossy(&self) -> String
pub fn to_string_lossy(&self) -> String
Decodes the string reference to a String even if it is invalid UTF-16 data.
This method assumes this string is encoded as UTF-16 and attempts to decode it as such. Any
invalid sequences are replaced with
U+FFFD REPLACEMENT CHARACTER, which looks like this:
�. It will *not have a nul terminator.
§Examples
use widestring::U16CString;
let s = "MyString";
// Create a wide string from the string
let wstr = U16CString::from_str(s).unwrap();
// Create a regular string from the wide string
let s2 = wstr.to_string_lossy();
assert_eq!(s2, s);Sourcepub fn chars(&self) -> CharsUtf16<'_>
pub fn chars(&self) -> CharsUtf16<'_>
Returns an iterator over the chars of a string slice.
As this string has no defined encoding, this method assumes the string is UTF-16. Since it
may consist of invalid UTF-16, the iterator returned by this method
is an iterator over Result<char, DecodeUtf16Error> instead of chars
directly. If you would like a lossy iterator over charss directly, instead
use chars_lossy.
It’s important to remember that char represents a Unicode Scalar Value, and
may not match your idea of what a ‘character’ is. Iteration over grapheme clusters may be
what you actually want. That functionality is not provided by by this crate.
Sourcepub fn chars_lossy(&self) -> CharsLossyUtf16<'_>
pub fn chars_lossy(&self) -> CharsLossyUtf16<'_>
Returns a lossy iterator over the chars of a string slice.
As this string has no defined encoding, this method assumes the string is UTF-16. Since it
may consist of invalid UTF-16, the iterator returned by this method will replace unpaired
surrogates with
U+FFFD REPLACEMENT CHARACTER (�). This is a lossy
version of chars.
It’s important to remember that char represents a Unicode Scalar Value, and
may not match your idea of what a ‘character’ is. Iteration over grapheme clusters may be
what you actually want. That functionality is not provided by by this crate.
Sourcepub fn char_indices(&self) -> CharIndicesUtf16<'_>
pub fn char_indices(&self) -> CharIndicesUtf16<'_>
Returns an iterator over the chars of a string slice, and their positions.
As this string has no defined encoding, this method assumes the string is UTF-16. Since it
may consist of invalid UTF-16, the iterator returned by this method is an iterator over
is an iterator over Result<char, DecodeUtf16Error> as well as their positions, instead of
chars directly. If you would like a lossy indices iterator over
charss directly, instead use
char_indices_lossy.
The iterator yields tuples. The position is first, the char is second.
Sourcepub fn char_indices_lossy(&self) -> CharIndicesLossyUtf16<'_>
pub fn char_indices_lossy(&self) -> CharIndicesLossyUtf16<'_>
Returns a lossy iterator over the chars of a string slice, and their positions.
As this string slice may consist of invalid UTF-16, the iterator returned by this method
will replace unpaired surrogates with
U+FFFD REPLACEMENT CHARACTER (�), as well as the
positions of all characters. This is a lossy version of
char_indices.
The iterator yields tuples. The position is first, the char is second.
Trait Implementations§
Source§impl AsRef<PCWSTRGuard> for PCWSTRGuard
Included for postfix .as_ref() convenience.
impl AsRef<PCWSTRGuard> for PCWSTRGuard
Included for postfix .as_ref() convenience.
Source§fn as_ref(&self) -> &PCWSTRGuard
fn as_ref(&self) -> &PCWSTRGuard
Source§impl Deref for PCWSTRGuard
impl Deref for PCWSTRGuard
impl Param<PCWSTR> for &PCWSTRGuard
MUST NOT implement this for PCWSTRGuard itself, only for &PCWSTRGuard,
to ensure the data the PCWSTR points to is valid for the lifetime of the parameter.
Auto Trait Implementations§
impl Freeze for PCWSTRGuard
impl RefUnwindSafe for PCWSTRGuard
impl Send for PCWSTRGuard
impl Sync for PCWSTRGuard
impl Unpin for PCWSTRGuard
impl UnwindSafe for PCWSTRGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more