Skip to content

Commit 22f7b7b

Browse files
committed
uefi: Add copy_from_iter() method to AlignedBuffer
1 parent 733ae1b commit 22f7b7b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

uefi/src/mem/aligned_buffer.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ impl AlignedBuffer {
8989
}
9090
}
9191

92+
/// Fill the aligned memory region with data from the given iterator.
93+
/// If the given iterator is shorter than the buffer, the remaining area will be left untouched.
94+
pub fn copy_from_iter(&mut self, src: impl Iterator<Item = u8>) {
95+
self.iter_mut()
96+
.zip(src)
97+
.for_each(|(dst, src_byte)| *dst = src_byte);
98+
}
99+
92100
/// Check the buffer's alignment against the `required_alignment`.
93101
pub fn check_alignment(&self, required_alignment: usize) -> Result<(), AlignmentError> {
94102
//TODO: use bfr.addr() when it's available
@@ -142,4 +150,28 @@ mod tests {
142150
}
143151
}
144152
}
153+
154+
#[test]
155+
fn test_copy_from_iter() {
156+
let src8: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
157+
{
158+
// src as large as dst
159+
let mut bfr = AlignedBuffer::from_size_align(8, 8).unwrap();
160+
bfr.copy_from_iter(src8.iter().cloned());
161+
assert_eq!(bfr.as_slice(), src8);
162+
}
163+
{
164+
// src larger than dst
165+
let mut bfr = AlignedBuffer::from_size_align(7, 8).unwrap();
166+
bfr.copy_from_iter(src8.iter().cloned());
167+
assert_eq!(bfr.as_slice(), [1, 2, 3, 4, 5, 6, 7]);
168+
}
169+
{
170+
// src smaller than dst
171+
let mut bfr = AlignedBuffer::from_size_align(9, 8).unwrap();
172+
bfr.iter_mut().for_each(|dst| *dst = 0); // fill with 0s
173+
bfr.copy_from_iter(src8.iter().cloned());
174+
assert_eq!(bfr.as_slice(), [1, 2, 3, 4, 5, 6, 7, 8, 0]);
175+
}
176+
}
145177
}

0 commit comments

Comments
 (0)