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); assert!(o.next().unwrap() == 0x2e2b34ca);
} }
// writeln!(stdout).unwrap(); // writeln!(stdout).unwrap();
o.finish(None); core::mem::drop(o);
let time_end = clock(); let time_end = clock();
writeln!(stdout, "({} kB/s)", (size as u64) * 1_000 / (time_end - time_start)).unwrap(); 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, len: usize,
iptr: usize, iptr: usize,
optr: usize, optr: usize,
finished: bool,
} }
impl <'a, I> Iterator for OutIterator<'a, I> 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]>) { pub fn finish(&mut self, tag: Option<&mut [u8]>) {
assert!(self.iptr >= self.len && self.optr >= self.len); assert!(self.iptr >= self.len && self.optr >= self.len);
finish(self.aes, self.cipher_mode, tag); 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, len,
iptr: 0, iptr: 0,
optr: 0, optr: 0,
finished: false,
} }
} }