mirror of
https://github.com/laanwj/k210-sdk-stuff.git
synced 2024-11-22 01:16:20 +04:00
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:
parent
10561369b1
commit
8b07675b33
@ -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();
|
||||||
|
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user