Skip to content

Commit 8d9392a

Browse files
committed
app: refactor packed len calculation
1 parent 64577a7 commit 8d9392a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

crates/common/src/saves.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ pub type Packed = u8;
33
const PACKED_SIZE: usize = Packed::BITS as usize;
44

55
pub const KEYS_COUNT: usize = 374;
6-
const PACKED_LEN: usize = {
7-
if KEYS_COUNT % PACKED_SIZE == 0 {
8-
KEYS_COUNT / PACKED_SIZE
6+
const PACKED_LEN: usize = packed_len(KEYS_COUNT);
7+
8+
const fn packed_len(bools: usize) -> usize {
9+
if bools % PACKED_SIZE == 0 {
10+
bools / PACKED_SIZE
911
} else {
10-
KEYS_COUNT / PACKED_SIZE + 1
12+
bools / PACKED_SIZE + 1
1113
}
12-
};
14+
}
1315

1416
pub fn empty() -> Vec<Packed> {
1517
(0..PACKED_LEN).map(|_| 0).collect()
1618
}
1719

1820
pub fn pack_bools(bools: &[bool]) -> Vec<Packed> {
19-
let mut bytes = Vec::with_capacity(bools.len() / PACKED_SIZE + 1);
21+
let mut bytes = Vec::with_capacity(packed_len(bools.len()));
2022
for chunk in bools.chunks(PACKED_SIZE) {
2123
let mut byte = 0;
2224
for (i, b) in chunk.iter().enumerate() {

0 commit comments

Comments
 (0)