Skip to content

Commit 4785e35

Browse files
author
Mohamad Mahmoud
committed
Debug Store: change fold to rfold for reverse-order event processing
Change the fold operation to rfold to process events in reverse chronological order and bump the encode version. Bug: 389026153 Flag: EXEMPT bug fix Test: atest libdebugstore_tests Change-Id: I9f6a02fd69f95062c2f4b3abb37e08a71c039b42 Change-Id: Icdb19fd69b8e91e4e35c62519bd79f73d0cf19b7
1 parent 5bfedbb commit 4785e35

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

libs/debugstore/rust/src/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl DebugStore {
4848
///
4949
/// This constant is used as a part of the debug store's data format,
5050
/// allowing for version tracking and compatibility checks.
51-
const ENCODE_VERSION: u32 = 1;
51+
const ENCODE_VERSION: u32 = 2;
5252

5353
/// Creates a new instance of `DebugStore` with specified event limit and maximum delay.
5454
fn new() -> Self {
@@ -129,7 +129,7 @@ impl fmt::Display for DebugStore {
129129
write!(
130130
f,
131131
"{}",
132-
self.event_store.fold(String::new(), |mut acc, event| {
132+
self.event_store.rfold(String::new(), |mut acc, event| {
133133
if !acc.is_empty() {
134134
acc.push_str("||");
135135
}

libs/debugstore/rust/src/storage.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ impl<T, const N: usize> Storage<T, N> {
3232
self.insertion_buffer.force_push(value);
3333
}
3434

35-
/// Folds over the elements in the storage using the provided function.
36-
pub fn fold<U, F>(&self, init: U, mut func: F) -> U
35+
/// Folds over the elements in the storage in reverse order using the provided function.
36+
pub fn rfold<U, F>(&self, init: U, mut func: F) -> U
3737
where
3838
F: FnMut(U, &T) -> U,
3939
{
40-
let mut acc = init;
40+
let mut items = Vec::new();
4141
while let Some(value) = self.insertion_buffer.pop() {
42-
acc = func(acc, &value);
42+
items.push(value);
43+
}
44+
let mut acc = init;
45+
for value in items.iter().rev() {
46+
acc = func(acc, value);
4347
}
4448
acc
4549
}
@@ -59,18 +63,18 @@ mod tests {
5963
let storage = Storage::<i32, 10>::new();
6064
storage.insert(7);
6165

62-
let sum = storage.fold(0, |acc, &x| acc + x);
66+
let sum = storage.rfold(0, |acc, &x| acc + x);
6367
assert_eq!(sum, 7, "The sum of the elements should be equal to the inserted value.");
6468
}
6569

6670
#[test]
67-
fn test_fold_functionality() {
71+
fn test_rfold_functionality() {
6872
let storage = Storage::<i32, 5>::new();
6973
storage.insert(1);
7074
storage.insert(2);
7175
storage.insert(3);
7276

73-
let sum = storage.fold(0, |acc, &x| acc + x);
77+
let sum = storage.rfold(0, |acc, &x| acc + x);
7478
assert_eq!(
7579
sum, 6,
7680
"The sum of the elements should be equal to the sum of inserted values."
@@ -84,13 +88,13 @@ mod tests {
8488
storage.insert(2);
8589
storage.insert(5);
8690

87-
let first_sum = storage.fold(0, |acc, &x| acc + x);
91+
let first_sum = storage.rfold(0, |acc, &x| acc + x);
8892
assert_eq!(first_sum, 8, "The sum of the elements should be equal to the inserted values.");
8993

9094
storage.insert(30);
9195
storage.insert(22);
9296

93-
let second_sum = storage.fold(0, |acc, &x| acc + x);
97+
let second_sum = storage.rfold(0, |acc, &x| acc + x);
9498
assert_eq!(
9599
second_sum, 52,
96100
"The sum of the elements should be equal to the inserted values."
@@ -103,7 +107,7 @@ mod tests {
103107
storage.insert(1);
104108
// This value should overwrite the previously inserted value (1).
105109
storage.insert(4);
106-
let sum = storage.fold(0, |acc, &x| acc + x);
110+
let sum = storage.rfold(0, |acc, &x| acc + x);
107111
assert_eq!(sum, 4, "The sum of the elements should be equal to the inserted values.");
108112
}
109113

@@ -128,7 +132,24 @@ mod tests {
128132
thread.join().expect("Thread should finish without panicking");
129133
}
130134

131-
let count = storage.fold(0, |acc, _| acc + 1);
135+
let count = storage.rfold(0, |acc, _| acc + 1);
132136
assert_eq!(count, 100, "Storage should be filled to its limit with concurrent insertions.");
133137
}
138+
139+
#[test]
140+
fn test_rfold_order() {
141+
let storage = Storage::<i32, 5>::new();
142+
storage.insert(1);
143+
storage.insert(2);
144+
storage.insert(3);
145+
146+
let mut result = Vec::new();
147+
storage.rfold((), |_, &x| result.push(x));
148+
149+
assert_eq!(
150+
result,
151+
vec![3, 2, 1],
152+
"Elements should be processed in reverse order of insertion"
153+
);
154+
}
134155
}

0 commit comments

Comments
 (0)