Skip to content

Commit a1c61c8

Browse files
device path: implement PartialEq+Eq for DevicePath (#265)
1 parent 4ec91ab commit a1c61c8

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/proto/device_path.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! total size of the Node including the header.
1818
1919
use crate::{proto::Protocol, unsafe_guid};
20+
use core::slice;
2021

2122
/// Header that appears at the start of every [`DevicePath`] node.
2223
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -35,7 +36,7 @@ pub struct DevicePathHeader {
3536
/// This can be opened on a `LoadedImage.device()` handle using the `HandleProtocol` boot service.
3637
#[repr(C, packed)]
3738
#[unsafe_guid("09576e91-6d3f-11d2-8e39-00a0c969723b")]
38-
#[derive(Protocol)]
39+
#[derive(Eq, Protocol)]
3940
pub struct DevicePath {
4041
header: DevicePathHeader,
4142
}
@@ -70,6 +71,27 @@ impl DevicePath {
7071
}
7172
}
7273

74+
impl PartialEq for DevicePath {
75+
fn eq(&self, other: &DevicePath) -> bool {
76+
// Check for equality with a byte-by-byte comparison of the device
77+
// paths. Note that this covers the entire payload of the device path
78+
// using the `length` field in the header, so it's not the same as just
79+
// comparing the fields of the `DevicePath` struct.
80+
unsafe {
81+
let self_bytes = slice::from_raw_parts(
82+
self as *const DevicePath as *const u8,
83+
self.length() as usize,
84+
);
85+
let other_bytes = slice::from_raw_parts(
86+
other as *const DevicePath as *const u8,
87+
other.length() as usize,
88+
);
89+
90+
self_bytes == other_bytes
91+
}
92+
}
93+
}
94+
7395
/// Iterator over [`DevicePath`] nodes.
7496
///
7597
/// Iteration ends when a path is reached where [`DevicePath::is_end_entire`]

0 commit comments

Comments
 (0)