Skip to content

Commit e6a8e9c

Browse files
Implement Deref for HandleBuffer and ProtocolsPerHandle
These types are smart pointers, so `Deref` is a good fit.
1 parent 2b0a253 commit e6a8e9c

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
unrecoverable and will cause the system to reset.
1616
- Re-export the `cstr8`, `cstr16`, and `entry` macros from the root of the
1717
`uefi` crate.
18+
- `HandleBuffer` and `ProtocolsPerHandle` now implement `Deref`. The
19+
`HandleBuffer::handles` and `ProtocolsPerHandle::protocols` methods have been
20+
deprecated.
1821

1922
## uefi-macros - [Unreleased]
2023

uefi-test-runner/examples/loaded_image.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fn print_image_path(boot_services: &BootServices) -> Result {
3737
// ANCHOR: device_path
3838
let device_path_to_text_handle = *boot_services
3939
.locate_handle_buffer(SearchType::ByProtocol(&DevicePathToText::GUID))?
40-
.handles()
4140
.first()
4241
.expect("DevicePathToText is missing");
4342

uefi-test-runner/src/boot/misc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ fn test_reinstall_protocol_interface(bt: &BootServices) {
120120
info!("Reinstalling TestProtocol");
121121
let handle = bt
122122
.locate_handle_buffer(SearchType::from_proto::<TestProtocol>())
123-
.expect("Failed to find protocol to uninstall")
124-
.handles()[0];
123+
.expect("Failed to find protocol to uninstall")[0];
125124

126125
unsafe {
127126
let _ = bt.reinstall_protocol_interface(
@@ -137,8 +136,7 @@ fn test_uninstall_protocol_interface(bt: &BootServices) {
137136
info!("Uninstalling TestProtocol");
138137
let handle = bt
139138
.locate_handle_buffer(SearchType::from_proto::<TestProtocol>())
140-
.expect("Failed to find protocol to uninstall")
141-
.handles()[0];
139+
.expect("Failed to find protocol to uninstall")[0];
142140

143141
unsafe {
144142
bt.uninstall_protocol_interface(handle, &TestProtocol::GUID, ptr::null_mut())

uefi-test-runner/src/boot/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn test_locate_handle_buffer(bt: &BootServices) {
2020
let handles = bt
2121
.locate_handle_buffer(SearchType::AllHandles)
2222
.expect("Failed to locate handle buffer");
23-
assert!(!handles.handles().is_empty(), "Could not find any handles");
23+
assert!(!handles.is_empty(), "Could not find any handles");
2424
}
2525

2626
{
@@ -29,7 +29,7 @@ fn test_locate_handle_buffer(bt: &BootServices) {
2929
.locate_handle_buffer(SearchType::ByProtocol(&Output::GUID))
3030
.expect("Failed to locate handle buffer");
3131
assert!(
32-
!handles.handles().is_empty(),
32+
!handles.is_empty(),
3333
"Could not find any OUTPUT protocol handles"
3434
);
3535
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ fn test_component_name<'a, C: ComponentNameInterface<'a>>(
103103

104104
// Find the FAT driver by name.
105105
let component_name: C = all_handles
106-
.handles()
107106
.iter()
108107
.find_map(|handle| {
109108
let component_name = C::open(boot_services, *handle).ok()?;
@@ -124,7 +123,6 @@ fn test_component_name<'a, C: ComponentNameInterface<'a>>(
124123

125124
// Now check that the FAT controller can be found by name.
126125
all_handles
127-
.handles()
128126
.iter()
129127
.find(|handle| {
130128
let controller_name = if let Ok(controller_name) =

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ fn test_protocols_per_handle(image: Handle, bt: &BootServices) {
5050
.protocols_per_handle(image)
5151
.expect("Failed to get protocols for image handle");
5252

53-
info!("Image handle has {} protocols", pph.protocols().len());
53+
info!("Image handle has {} protocols", pph.len());
5454

5555
// Check that one of the image's protocols is `LoadedImage`.
56-
assert!(pph
57-
.protocols()
58-
.iter()
59-
.any(|guid| **guid == LoadedImage::GUID));
56+
assert!(pph.iter().any(|guid| **guid == LoadedImage::GUID));
6057
}
6158

6259
mod console;

uefi/src/table/boot.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ impl BootServices {
10131013

10141014
fn get_handle_for_protocol_impl(&self, guid: &Guid) -> Result<Handle> {
10151015
self.locate_handle_buffer(SearchType::ByProtocol(guid))?
1016-
.handles()
10171016
.first()
10181017
.cloned()
10191018
.ok_or_else(|| Status::NOT_FOUND.into())
@@ -2214,10 +2213,19 @@ impl<'a> Drop for ProtocolsPerHandle<'a> {
22142213
}
22152214
}
22162215

2216+
impl<'a> Deref for ProtocolsPerHandle<'a> {
2217+
type Target = [&'a Guid];
2218+
2219+
fn deref(&self) -> &Self::Target {
2220+
unsafe { slice::from_raw_parts(self.protocols, self.count) }
2221+
}
2222+
}
2223+
22172224
impl<'a> ProtocolsPerHandle<'a> {
22182225
/// Get the protocol interface [`Guids`][Guid] that are installed on the
22192226
/// [`Handle`].
22202227
#[allow(clippy::missing_const_for_fn)] // Required until we bump the MSRV.
2228+
#[deprecated = "use Deref instead"]
22212229
#[must_use]
22222230
pub fn protocols<'b>(&'b self) -> &'b [&'a Guid] {
22232231
// convert raw pointer to slice here so that we can get
@@ -2243,9 +2251,18 @@ impl<'a> Drop for HandleBuffer<'a> {
22432251
}
22442252
}
22452253

2254+
impl<'a> Deref for HandleBuffer<'a> {
2255+
type Target = [Handle];
2256+
2257+
fn deref(&self) -> &Self::Target {
2258+
unsafe { slice::from_raw_parts(self.buffer, self.count) }
2259+
}
2260+
}
2261+
22462262
impl<'a> HandleBuffer<'a> {
22472263
/// Get an array of [`Handles`][Handle] that support the requested protocol.
22482264
#[allow(clippy::missing_const_for_fn)] // Required until we bump the MSRV.
2265+
#[deprecated = "use Deref instead"]
22492266
#[must_use]
22502267
pub fn handles(&self) -> &[Handle] {
22512268
// convert raw pointer to slice here so that we can get

0 commit comments

Comments
 (0)