Skip to content

Commit

Permalink
Merge pull request rust-osdev#1014 from nicholasbishop/bishop-simplif…
Browse files Browse the repository at this point in the history
…y-to-string

Simplify DevicePath to_string return type
  • Loading branch information
phip1611 authored Dec 2, 2023
2 parents a6182b2 + 872993f commit f7b2243
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
3 changes: 1 addition & 2 deletions uefi-test-runner/src/proto/device_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn test(image: Handle, bt: &BootServices) {
let path_components = device_path
.node_iter()
.map(|node| node.to_string(bt, DisplayOnly(false), AllowShortcuts(false)))
.map(|str| str.unwrap().unwrap().to_string())
.map(|str| str.unwrap().to_string())
.collect::<Vec<_>>();

let expected_device_path_str_components = &[
Expand All @@ -104,7 +104,6 @@ pub fn test(image: Handle, bt: &BootServices) {
let path = device_path
.to_string(bt, DisplayOnly(false), AllowShortcuts(false))
.unwrap()
.unwrap()
.to_string();

assert_eq!(path, expected_device_path_str);
Expand Down
4 changes: 4 additions & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- Implemented `PartialEq<char>` for `Char8` and `Char16`.
- Added `CStr16::from_char16_with_nul` and `Char16::from_char16_with_nul_unchecked`.

## Changed
- `DevicePath::to_string` and `DevicePathNode::to_string` now return
out-of-memory errors as part of the error type rather than with an `Option`.

# uefi - 0.26.0 (2023-11-12)

## Added
Expand Down
24 changes: 10 additions & 14 deletions uefi/src/proto/device_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,24 @@ impl DevicePathNode {

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

let cstring16 = to_text_protocol
to_text_protocol
.convert_device_node_to_text(bs, self, display_only, allow_shortcuts)
.ok()
.map(|pool_string| {
let cstr16 = &*pool_string;
// Another allocation; pool string is dropped. This overhead
// is negligible. CString16 is more convenient to use.
CString16::from(cstr16)
});
Ok(cstring16)
})
.map_err(|_| DevicePathToTextError::OutOfMemory)
}
}

Expand Down Expand Up @@ -430,27 +427,24 @@ impl DevicePath {

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

let cstring16 = to_text_protocol
to_text_protocol
.convert_device_path_to_text(bs, self, display_only, allow_shortcuts)
.ok()
.map(|pool_string| {
let cstr16 = &*pool_string;
// Another allocation; pool string is dropped. This overhead
// is negligible. CString16 is more convenient to use.
CString16::from(cstr16)
});
Ok(cstring16)
})
.map_err(|_| DevicePathToTextError::OutOfMemory)
}
}

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

impl Display for DevicePathToTextError {
Expand Down

0 comments on commit f7b2243

Please sign in to comment.