From 9c4737c2a939f83f72328d10caaf58efc1b8c182 Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Wed, 29 Dec 2021 13:42:55 +0000 Subject: [PATCH] refactor: use fold instead of for loops for zero check --- src/stream_ext.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/stream_ext.rs b/src/stream_ext.rs index 28ef856..6b71ef1 100644 --- a/src/stream_ext.rs +++ b/src/stream_ext.rs @@ -1,3 +1,5 @@ +use std::ops::BitOr; + pub trait StreamExt { fn read_u32_be(&self, offset: usize) -> 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, /// but probably gets optimised away by llvm... lol fn is_all_zeros(&self) -> bool { - let mut sum = 0; - - for b in self { - sum |= b; - } - - return sum == 0; + self.iter().fold(0u8, |acc, b| acc.bitor(b)) == 0 } #[inline]