mirror of
https://github.com/jixunmoe/tc_tea_rust
synced 2026-03-08 04:29:49 +00:00
refactor: use fold instead of for loops for zero check
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::ops::BitOr;
|
||||||
|
|
||||||
pub trait StreamExt {
|
pub trait StreamExt {
|
||||||
fn read_u32_be(&self, offset: usize) -> u32;
|
fn read_u32_be(&self, offset: usize) -> u32;
|
||||||
fn write_u32_be(&mut self, offset: usize, value: u32);
|
fn write_u32_be(&mut self, offset: usize, value: u32);
|
||||||
@@ -34,13 +36,7 @@ impl StreamExt for [u8] {
|
|||||||
/// Attempts to do constant time comparison,
|
/// Attempts to do constant time comparison,
|
||||||
/// but probably gets optimised away by llvm... lol
|
/// but probably gets optimised away by llvm... lol
|
||||||
fn is_all_zeros(&self) -> bool {
|
fn is_all_zeros(&self) -> bool {
|
||||||
let mut sum = 0;
|
self.iter().fold(0u8, |acc, b| acc.bitor(b)) == 0
|
||||||
|
|
||||||
for b in self {
|
|
||||||
sum |= b;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
Reference in New Issue
Block a user