Skip to content

Commit 3f23e92

Browse files
committed
uefi: impl Index and IndexMut for MemoryMapOwned, MemoryMapRef, and MemoryMapRefMut
1 parent 8bf73e1 commit 3f23e92

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

uefi/src/table/boot.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,14 @@ impl<'a> MemoryMap for MemoryMapRef<'a> {
19211921
}
19221922
}
19231923

1924+
impl Index<usize> for MemoryMapRef<'_> {
1925+
type Output = MemoryDescriptor;
1926+
1927+
fn index(&self, index: usize) -> &Self::Output {
1928+
self.get(index).unwrap()
1929+
}
1930+
}
1931+
19241932
/// Implementation of [`MemoryMapMut`] for the given buffer.
19251933
#[derive(Debug)]
19261934
pub struct MemoryMapRefMut<'a> {
@@ -2031,6 +2039,20 @@ impl<'a> MemoryMapRefMut<'a> {
20312039
}
20322040
}
20332041

2042+
impl Index<usize> for MemoryMapRefMut<'_> {
2043+
type Output = MemoryDescriptor;
2044+
2045+
fn index(&self, index: usize) -> &Self::Output {
2046+
self.get(index).unwrap()
2047+
}
2048+
}
2049+
2050+
impl IndexMut<usize> for MemoryMapRefMut<'_> {
2051+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2052+
self.get_mut(index).unwrap()
2053+
}
2054+
}
2055+
20342056
/// Implementation of [`MemoryMapMut`] that owns the buffer on the UEFI heap.
20352057
#[derive(Debug)]
20362058
pub struct MemoryMapOwned {
@@ -2111,6 +2133,20 @@ impl MemoryMapMut for MemoryMapOwned {
21112133
}
21122134
}
21132135

2136+
impl Index<usize> for MemoryMapOwned {
2137+
type Output = MemoryDescriptor;
2138+
2139+
fn index(&self, index: usize) -> &Self::Output {
2140+
self.get(index).unwrap()
2141+
}
2142+
}
2143+
2144+
impl IndexMut<usize> for MemoryMapOwned {
2145+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2146+
self.get_mut(index).unwrap()
2147+
}
2148+
}
2149+
21142150
/// An iterator of [`MemoryDescriptor`]. The underlying memory map is always
21152151
/// associated with a unique [`MemoryMapKey`].
21162152
#[derive(Debug, Clone)]
@@ -2355,7 +2391,10 @@ mod tests_mmap_artificial {
23552391
let mut mem_map = buffer_to_map(&mut buffer);
23562392

23572393
for index in 0..3 {
2358-
assert_eq!(mem_map.get(index), BUFFER.get(index))
2394+
assert_eq!(mem_map.get(index), BUFFER.get(index));
2395+
2396+
// Test Index impl
2397+
assert_eq!(Some(&mem_map[index]), BUFFER.get(index));
23592398
}
23602399

23612400
let mut_desc = mem_map.get_mut(2).unwrap();

0 commit comments

Comments
 (0)