stak_profiler/record/
duration_record.rs1use super::{Record, StackedRecord};
2use crate::{COLUMN_SEPARATOR, Error, Stack};
3use core::{
4 fmt::{self, Display, Formatter},
5 str::FromStr,
6};
7
8#[derive(Debug, Clone, Eq, PartialEq)]
10pub struct DurationRecord {
11 stack: Stack,
12 time: u128,
13}
14
15impl DurationRecord {
16 pub const fn new(stack: Stack, time: u128) -> Self {
18 Self { stack, time }
19 }
20
21 pub const fn time(&self) -> u128 {
23 self.time
24 }
25}
26
27impl FromStr for DurationRecord {
28 type Err = Error;
29
30 fn from_str(string: &str) -> Result<Self, Self::Err> {
31 let mut iterator = string.split(COLUMN_SEPARATOR);
32
33 Ok(Self::new(
34 iterator.next().ok_or(Error::MissingStack)?.parse()?,
35 iterator.next().ok_or(Error::MissingTime)?.parse()?,
36 ))
37 }
38}
39
40impl Display for DurationRecord {
41 fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
42 write!(formatter, "{}", &self.stack)?;
43 write!(formatter, "{COLUMN_SEPARATOR}")?;
44 write!(formatter, "{}", &self.time)?;
45
46 Ok(())
47 }
48}
49
50impl Record for DurationRecord {}
51
52impl StackedRecord for DurationRecord {
53 fn stack(&self) -> &Stack {
54 &self.stack
55 }
56
57 fn stack_mut(&mut self) -> &mut Stack {
58 &mut self.stack
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn parse() {
68 let record =
69 DurationRecord::new(Stack::new(vec![Some("foo".into()), Some("bar".into())]), 42);
70
71 assert_eq!(
72 record.to_string().parse::<DurationRecord>().unwrap(),
73 record
74 );
75 }
76}