rust: Implement Drop for AES iterator

Implement drop for the AES iterator to automatically finish
the hardware operation when the iterator goes out of scope.
This commit is contained in:
Wladimir J. van der Laan 2020-05-01 12:42:45 +00:00
parent 10561369b1
commit 8b07675b33
2 changed files with 15 additions and 1 deletions

View File

@ -631,7 +631,7 @@ fn main() -> ! {
assert!(o.next().unwrap() == 0x2e2b34ca);
}
// writeln!(stdout).unwrap();
o.finish(None);
core::mem::drop(o);
let time_end = clock();
writeln!(stdout, "({} kB/s)", (size as u64) * 1_000 / (time_end - time_start)).unwrap();

View File

@ -184,6 +184,7 @@ pub struct OutIterator<'a, I>
len: usize,
iptr: usize,
optr: usize,
finished: bool,
}
impl <'a, I> Iterator for OutIterator<'a, I>
@ -215,6 +216,18 @@ impl <'a, I> OutIterator<'a, I>
pub fn finish(&mut self, tag: Option<&mut [u8]>) {
assert!(self.iptr >= self.len && self.optr >= self.len);
finish(self.aes, self.cipher_mode, tag);
self.finished = true;
}
}
impl <'a, I> Drop for OutIterator<'a, I>
where I: Iterator<Item=u32> {
/** Implement drop so that the AES hardware operation is finished up when the iterator goes out
* of scope. */
fn drop(&mut self) {
if !self.finished {
self.finish(None);
}
}
}
@ -241,5 +254,6 @@ pub fn run_iter32<'a, X>(
len,
iptr: 0,
optr: 0,
finished: false,
}
}