Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ed34335

Browse files
Merge pull request #788 from phip1611/cstr16-as-bytes
cstr16: add method to get the underlying bytes
2 parents 5db374a + a57928e commit ed34335

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
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-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
262262
/// assert_eq!(cstr8!().to_u16_slice_with_nul(), [0]);
263263
/// assert_eq!(cstr8!("").to_u16_slice_with_nul(), [0]);
264264
/// // Non-empty string
265-
/// assert_eq!(cstr8!("test").to_bytes_with_nul(), [116, 101, 115, 116, 0]);
265+
/// assert_eq!(cstr8!("test").as_bytes(), [116, 101, 115, 116, 0]);
266266
/// ```
267267
#[proc_macro]
268268
pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

‎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, 0, 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)]

‎uefi/src/proto/string/unicode_collation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ impl UnicodeCollation {
103103
fat: &CStr8,
104104
buf: &'a mut [u16],
105105
) -> Result<&'a CStr16, StrConversionError> {
106-
if buf.len() < fat.to_bytes_with_nul().len() {
106+
if buf.len() < fat.as_bytes().len() {
107107
return Err(StrConversionError::BufferTooSmall);
108108
}
109109
(self.fat_to_str)(
110110
self,
111-
fat.to_bytes_with_nul().len(),
111+
fat.as_bytes().len(),
112112
fat.as_ptr(),
113113
buf.as_ptr() as *mut _,
114114
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /