Skip to content

Commit f7b2243

Browse files
authored
Merge pull request #1014 from nicholasbishop/bishop-simplify-to-string
Simplify DevicePath to_string return type
2 parents a6182b2 + 872993f commit f7b2243

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

uefi-test-runner/src/proto/device_path.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn test(image: Handle, bt: &BootServices) {
7777
let path_components = device_path
7878
.node_iter()
7979
.map(|node| node.to_string(bt, DisplayOnly(false), AllowShortcuts(false)))
80-
.map(|str| str.unwrap().unwrap().to_string())
80+
.map(|str| str.unwrap().to_string())
8181
.collect::<Vec<_>>();
8282

8383
let expected_device_path_str_components = &[
@@ -104,7 +104,6 @@ pub fn test(image: Handle, bt: &BootServices) {
104104
let path = device_path
105105
.to_string(bt, DisplayOnly(false), AllowShortcuts(false))
106106
.unwrap()
107-
.unwrap()
108107
.to_string();
109108

110109
assert_eq!(path, expected_device_path_str);

uefi/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
- Implemented `PartialEq<char>` for `Char8` and `Char16`.
55
- Added `CStr16::from_char16_with_nul` and `Char16::from_char16_with_nul_unchecked`.
66

7+
## Changed
8+
- `DevicePath::to_string` and `DevicePathNode::to_string` now return
9+
out-of-memory errors as part of the error type rather than with an `Option`.
10+
711
# uefi - 0.26.0 (2023-11-12)
812

913
## Added

uefi/src/proto/device_path/mod.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,24 @@ impl DevicePathNode {
217217

218218
/// Transforms the device path node to its string representation using the
219219
/// [`DevicePathToText`] protocol.
220-
///
221-
/// The resulting string is only None, if there was not enough memory.
222220
#[cfg(feature = "alloc")]
223221
pub fn to_string(
224222
&self,
225223
bs: &BootServices,
226224
display_only: DisplayOnly,
227225
allow_shortcuts: AllowShortcuts,
228-
) -> Result<Option<CString16>, DevicePathToTextError> {
226+
) -> Result<CString16, DevicePathToTextError> {
229227
let to_text_protocol = open_text_protocol(bs)?;
230228

231-
let cstring16 = to_text_protocol
229+
to_text_protocol
232230
.convert_device_node_to_text(bs, self, display_only, allow_shortcuts)
233-
.ok()
234231
.map(|pool_string| {
235232
let cstr16 = &*pool_string;
236233
// Another allocation; pool string is dropped. This overhead
237234
// is negligible. CString16 is more convenient to use.
238235
CString16::from(cstr16)
239-
});
240-
Ok(cstring16)
236+
})
237+
.map_err(|_| DevicePathToTextError::OutOfMemory)
241238
}
242239
}
243240

@@ -430,27 +427,24 @@ impl DevicePath {
430427

431428
/// Transforms the device path to its string representation using the
432429
/// [`DevicePathToText`] protocol.
433-
///
434-
/// The resulting string is only None, if there was not enough memory.
435430
#[cfg(feature = "alloc")]
436431
pub fn to_string(
437432
&self,
438433
bs: &BootServices,
439434
display_only: DisplayOnly,
440435
allow_shortcuts: AllowShortcuts,
441-
) -> Result<Option<CString16>, DevicePathToTextError> {
436+
) -> Result<CString16, DevicePathToTextError> {
442437
let to_text_protocol = open_text_protocol(bs)?;
443438

444-
let cstring16 = to_text_protocol
439+
to_text_protocol
445440
.convert_device_path_to_text(bs, self, display_only, allow_shortcuts)
446-
.ok()
447441
.map(|pool_string| {
448442
let cstr16 = &*pool_string;
449443
// Another allocation; pool string is dropped. This overhead
450444
// is negligible. CString16 is more convenient to use.
451445
CString16::from(cstr16)
452-
});
453-
Ok(cstring16)
446+
})
447+
.map_err(|_| DevicePathToTextError::OutOfMemory)
454448
}
455449
}
456450

@@ -789,6 +783,8 @@ pub enum DevicePathToTextError {
789783
/// The handle supporting the [`DevicePathToText`] protocol exists but it
790784
/// could not be opened.
791785
CantOpenProtocol(crate::Error),
786+
/// Failed to allocate pool memory.
787+
OutOfMemory,
792788
}
793789

794790
impl Display for DevicePathToTextError {

0 commit comments

Comments
 (0)