cpu_instructions_reader/
instant.rs

1use std::ops::Sub;
2
3use libc::c_int;
4
5use crate::{InstructionNumber, Error, Result};
6
7#[derive(Debug, Clone, Copy)]
8pub struct InstructionNumberInstant {
9    cpu: c_int,
10    raw: InstructionNumber,
11}
12
13impl InstructionNumberInstant {
14    pub(crate) const fn new(cpu: c_int, raw: InstructionNumber) -> Self {
15        Self { cpu, raw }
16    }
17
18    /// Calculate the number of cpu instruction number between two recordings
19    ///
20    /// # Panics
21    ///
22    /// If the two records are not the same cpu, an error will be returned.
23    #[must_use]
24    pub fn instruction_number_since(&self, other: Self) -> InstructionNumber {
25        self.instruction_number_since_checked(other).unwrap()
26    }
27
28    /// Calculate the number of cpu instruction number number between two recordings
29    ///
30    /// # Errors
31    ///
32    /// If the two records are not the same cpu, an error will be returned
33    pub fn instruction_number_since_checked(&self, other: Self) -> Result<InstructionNumber> {
34        if self.cpu == other.cpu {
35            Ok(self.raw - other.raw)
36        } else {
37            Err(Error::InconsistentCore)
38        }
39    }
40}
41
42impl Sub for InstructionNumberInstant {
43    type Output = InstructionNumber;
44
45    fn sub(self, other: Self) -> Self::Output {
46        self.instruction_number_since(other)
47    }
48}