Skip to content

Commit 256c884

Browse files
committed
cstr16 to str improvements after review
1 parent ea6b259 commit 256c884

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/data_types/strs.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,23 @@ impl CStr16 {
165165
}
166166
}
167167

168-
/// Write a string slice into the provided buffer. If it fails, then most probably, because
169-
/// the buffer is not big enough. In that case, the buffer will contain the correct string
170-
/// until the point, where the size was not enough.
168+
/// Writes each [`Char16`] as a [´char´] (4 bytes long in Rust language) into the buffer.
169+
/// It is up the the implementer of [`core::fmt::Write`] to convert the char to a string
170+
/// with proper encoding/charset. For example, in the case of [`core::alloc::string::String`]
171+
/// all Rust chars (UTF-32) get converted to UTF-8.
171172
///
172173
/// ## Example
173174
///
174-
/// ```rust
175-
/// use uefi::data_types::ArrayString;
176-
/// use uefi::{CStr16, Char16};
175+
/// ```ignore
177176
/// let firmware_vendor_c16_str: CStr16 = ...;
178-
/// // crate "arrayvec" uses stack-allocated arrays for Strings => no heap
177+
/// // crate "arrayvec" uses stack-allocated arrays for Strings => no heap allocations
179178
/// let mut buf = arrayvec::ArrayString::<128>::new();
180179
/// firmware_vendor_c16_str.as_str_in_buf(&mut buf);
181180
/// log::info!("as rust str: {}", buf.as_str());
182181
/// ```
183-
pub fn as_str_in_buf(&self, buf: &mut dyn core::fmt::Write) -> Result<(), ()> {
182+
pub fn as_str_in_buf(&self, buf: &mut dyn core::fmt::Write) -> core::fmt::Result {
184183
for c16 in self.iter() {
185-
let res = buf.write_char(char::from(*c16));
186-
if let Err(err) = res {
187-
log::error!("Failed to write CStr16 as &str into buffer. Buffer too small? ({})", err);
188-
return Err(())
189-
}
184+
buf.write_char(char::from(*c16))?;
190185
}
191186
Ok(())
192187
}

uefi-test-runner/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ fn efi_main(image: Handle, mut st: SystemTable<Boot>) -> Status {
2525
// Initialize utilities (logging, memory allocation...)
2626
uefi_services::init(&mut st).expect_success("Failed to initialize utilities");
2727

28+
/* unit tests here */
29+
/*let mut buf = String::new();
30+
st.firmware_vendor().as_str_in_buf(&mut buf).unwrap();
31+
st.stdout().write_str(buf.as_str()).unwrap();
32+
assert_eq!("EDK II", buf.as_str());*/
33+
2834
// Reset the console before running all the other tests.
2935
st.stdout()
3036
.reset(false)

0 commit comments

Comments
 (0)