Struct uefi::data_types::CStr16
source · [−]#[repr(transparent)]pub struct CStr16(_);
Expand description
An UCS-2 null-terminated string
This type is largely inspired by std::ffi::CStr
, see the documentation of
CStr
for more details on its semantics.
Implementations
sourceimpl CStr16
impl CStr16
sourcepub unsafe fn from_ptr<'ptr>(ptr: *const Char16) -> &'ptr Self
pub unsafe fn from_ptr<'ptr>(ptr: *const Char16) -> &'ptr Self
Wraps a raw UEFI string with a safe C string wrapper
Safety
The function will start accessing memory from ptr
until the first
null byte. It’s the callers responsability to ensure ptr
points to
a valid string, in accessible memory.
sourcepub fn from_u16_with_nul(codes: &[u16]) -> Result<&Self, FromSliceWithNulError>
pub fn from_u16_with_nul(codes: &[u16]) -> Result<&Self, FromSliceWithNulError>
Creates a C string wrapper from a u16 slice
Since not every u16 value is a valid UCS-2 code point, this function must do a bit more validity checking than CStr::from_bytes_with_nul
sourcepub unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self
pub unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self
Unsafely creates a C string wrapper from a u16 slice.
Safety
It’s the callers responsability to ensure chars is a valid UCS-2 null-terminated string, with no interior null bytes.
sourcepub fn from_str_with_buf<'a>(
input: &str,
buf: &'a mut [u16]
) -> Result<&'a Self, FromStrWithBufError>
pub fn from_str_with_buf<'a>(
input: &str,
buf: &'a mut [u16]
) -> Result<&'a Self, FromStrWithBufError>
Convert a &str
to a &CStr16
, backed by a buffer.
The input string must contain only characters representable with UCS-2, and must not contain any null characters (even at the end of the input).
The backing buffer must be big enough to hold the converted string as well as a trailing null character.
Examples
Convert the UTF-8 string “ABC” to a &CStr16
:
use uefi::CStr16;
let mut buf = [0; 4];
CStr16::from_str_with_buf("ABC", &mut buf).unwrap();
sourcepub fn as_slice_with_nul(&self) -> &[Char16]
pub fn as_slice_with_nul(&self) -> &[Char16]
Get the underlying Char16
slice, including the trailing null.
sourcepub fn to_u16_slice(&self) -> &[u16]
pub fn to_u16_slice(&self) -> &[u16]
Converts this C string to a u16 slice
sourcepub fn to_u16_slice_with_nul(&self) -> &[u16]
pub fn to_u16_slice_with_nul(&self) -> &[u16]
Converts this C string to a u16 slice containing the trailing 0 char
sourcepub fn num_bytes(&self) -> usize
pub fn num_bytes(&self) -> usize
Get the number of bytes in the string (including the trailing null character).
sourcepub fn as_str_in_buf(&self, buf: &mut dyn Write) -> Result
pub fn as_str_in_buf(&self, buf: &mut dyn Write) -> Result
Writes each Char16
as a [´char´] (4 bytes long in Rust language) into the buffer.
It is up the the implementer of core::fmt::Write
to convert the char to a string
with proper encoding/charset. For example, in the case of alloc::string::String
all Rust chars (UTF-32) get converted to UTF-8.
Example
let firmware_vendor_c16_str: CStr16 = ...;
// crate "arrayvec" uses stack-allocated arrays for Strings => no heap allocations
let mut buf = arrayvec::ArrayString::<128>::new();
firmware_vendor_c16_str.as_str_in_buf(&mut buf);
log::info!("as rust str: {}", buf.as_str());