Skip to content

Commit 3d28228

Browse files
committed
cstr16: add method to get the underlying bytes
1 parent 17e5aa0 commit 3d28228

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- `From<&CStr16>` for `String`
2121
- `From<&CString16>` for `String`
2222
- Added `RuntimeServices::get_variable_boxed` (requires the `alloc` feature).
23+
- Added `CStr16::as_bytes`
24+
- Added `AsRef<[u8]>` and `Borrow<[u8]>` for `Cstr8` and `CStr16`.
2325

2426
### Changed
2527

@@ -44,6 +46,8 @@
4446
- The `MEMORY_DESCRIPTOR_VERSION` constant has been moved to
4547
`MemoryDescriptor::VERSION`.
4648
- The `Revision` struct's one field is now public.
49+
- Renamed `CStr8::to_bytes` to `CStr8::as_bytes` and changed the semantics:
50+
The trailing null character is now always included in the returned slice.
4751

4852
## uefi-macros - [Unreleased]
4953

uefi/src/data_types/strs.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::chars::{Char16, Char8, NUL_16, NUL_8};
22
use super::UnalignedSlice;
33
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
4+
use core::borrow::Borrow;
45
use core::ffi::CStr;
56
use core::iter::Iterator;
67
use core::mem::MaybeUninit;
@@ -118,16 +119,10 @@ impl CStr8 {
118119
self.0.as_ptr()
119120
}
120121

121-
/// Converts this CStr8 to a slice of bytes without the terminating null byte.
122+
/// Returns the underlying bytes as slice including the terminating null
123+
/// character.
122124
#[must_use]
123-
pub fn to_bytes(&self) -> &[u8] {
124-
let chars = self.to_bytes_with_nul();
125-
&chars[..chars.len() - 1]
126-
}
127-
128-
/// Converts this CStr8 to a slice of bytes containing the trailing null byte.
129-
#[must_use]
130-
pub const fn to_bytes_with_nul(&self) -> &[u8] {
125+
pub const fn as_bytes(&self) -> &[u8] {
131126
unsafe { &*(&self.0 as *const [Char8] as *const [u8]) }
132127
}
133128
}
@@ -147,6 +142,18 @@ impl fmt::Display for CStr8 {
147142
}
148143
}
149144

145+
impl AsRef<[u8]> for CStr8 {
146+
fn as_ref(&self) -> &[u8] {
147+
self.as_bytes()
148+
}
149+
}
150+
151+
impl Borrow<[u8]> for CStr8 {
152+
fn borrow(&self) -> &[u8] {
153+
self.as_bytes()
154+
}
155+
}
156+
150157
impl<StrType: AsRef<str> + ?Sized> EqStrUntilNul<StrType> for CStr8 {
151158
fn eq_str_until_nul(&self, other: &StrType) -> bool {
152159
let other = other.as_ref();
@@ -389,6 +396,25 @@ impl CStr16 {
389396
}
390397
Ok(())
391398
}
399+
400+
/// Returns the underlying bytes as slice including the terminating null
401+
/// character.
402+
#[must_use]
403+
pub fn as_bytes(&self) -> &[u8] {
404+
unsafe { slice::from_raw_parts(self.0.as_ptr().cast(), self.num_bytes()) }
405+
}
406+
}
407+
408+
impl AsRef<[u8]> for CStr16 {
409+
fn as_ref(&self) -> &[u8] {
410+
self.as_bytes()
411+
}
412+
}
413+
414+
impl Borrow<[u8]> for CStr16 {
415+
fn borrow(&self) -> &[u8] {
416+
self.as_bytes()
417+
}
392418
}
393419

394420
#[cfg(feature = "alloc")]
@@ -522,6 +548,14 @@ mod tests {
522548
assert!(msg.eq_str_until_nul(cstr8));
523549
}
524550

551+
#[test]
552+
fn test_cstr8_as_bytes() {
553+
let string: &CStr8 = cstr8!("a");
554+
assert_eq!(string.as_bytes(), &[b'a', 0]);
555+
assert_eq!(<CStr8 as AsRef<[u8]>>::as_ref(string), &[b'a', 0]);
556+
assert_eq!(<CStr8 as Borrow<[u8]>>::borrow(string), &[b'a', 0]);
557+
}
558+
525559
#[test]
526560
fn test_cstr16_num_bytes() {
527561
let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();
@@ -617,6 +651,14 @@ mod tests {
617651
);
618652
}
619653

654+
#[test]
655+
fn test_cstr16_as_bytes() {
656+
let string: &CStr16 = cstr16!("a");
657+
assert_eq!(string.as_bytes(), &[b'a', 0]);
658+
assert_eq!(<CStr16 as AsRef<[u8]>>::as_ref(string), &[b'a', 0, 0, 0]);
659+
assert_eq!(<CStr16 as Borrow<[u8]>>::borrow(string), &[b'a', 0, 0, 0]);
660+
}
661+
620662
// Code generation helper for the compare tests of our CStrX types against "str" and "String"
621663
// from the standard library.
622664
#[allow(non_snake_case)]

0 commit comments

Comments
 (0)