stak_profiler/
collapse.rs1use crate::{Error, StackedRecord};
2
3pub fn collapse_stacks<R: StackedRecord>(
5 records: impl IntoIterator<Item = Result<R, Error>>,
6) -> impl Iterator<Item = Result<R, Error>> {
7 records.into_iter().map(|record| {
8 let mut record = record?;
9
10 record.stack_mut().collapse_frames();
11
12 Ok(record)
13 })
14}
15
16#[cfg(test)]
17mod tests {
18 use super::*;
19 use crate::{DurationRecord, Stack};
20 use pretty_assertions::assert_eq;
21
22 #[test]
23 fn collapse() {
24 assert_eq!(
25 collapse_stacks([
26 Ok(DurationRecord::new(
27 Stack::new(vec![Some("bar".into()), Some("foo".into())]),
28 123,
29 )),
30 Ok(DurationRecord::new(
31 Stack::new(vec![
32 Some("baz".into()),
33 Some("bar".into()),
34 Some("bar".into()),
35 Some("foo".into()),
36 ]),
37 42,
38 )),
39 ])
40 .collect::<Vec<_>>(),
41 vec![
42 Ok(DurationRecord::new(
43 Stack::new(vec![Some("bar".into()), Some("foo".into())]),
44 123,
45 )),
46 Ok(DurationRecord::new(
47 Stack::new(vec![
48 Some("baz".into()),
49 Some("bar".into()),
50 Some("foo".into()),
51 ]),
52 42,
53 )),
54 ]
55 );
56 }
57}