Skip to main content

gwr_engine/
types.rs

1// Copyright (c) 2023 Graphcore Ltd. All rights reserved.
2
3//! Shared types.
4
5use std::error::Error;
6use std::fmt;
7use std::rc::Rc;
8
9use crate::traits::{Event, Runnable};
10
11/// The return value from a call to [listen()](crate::traits::Event)
12pub type EventResult<T> = T;
13
14pub type Eventable<T> = Box<dyn Event<T> + 'static>;
15
16/// The type of a component that can be registered with the `Engine` so that it
17/// will automatically be spawned.
18pub type Component = Rc<dyn Runnable + 'static>;
19
20// Simulation errors
21
22/// Build a [SimError] from a message that supports `to_string`
23#[macro_export]
24macro_rules! sim_error {
25    ($($arg:tt)+) => {
26        Err($crate::types::SimError(format!($($arg)+).to_string()))
27    };
28}
29
30/// Error returned while parsing, constructing, connecting, or running a
31/// simulation.
32///
33/// Prefer catching static mistakes early. Use parse-time validation when
34/// reading files, and construction-time and connect-time validation where
35/// possible.
36///
37/// Include enough detail to identify the entity, config item, port, node, or
38/// edge that caused the failure clearly enough to fix the model topology or
39/// input.
40#[derive(Debug)]
41pub struct SimError(pub String);
42
43impl fmt::Display for SimError {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        write!(f, "{}", self.0)
46    }
47}
48
49impl Error for SimError {}
50
51/// The SimResult is the return type for most simulation functions
52pub type SimResult = Result<(), SimError>;
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
55pub struct DeviceId(pub u64);
56
57impl fmt::Display for DeviceId {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        self.0.fmt(f)
60    }
61}
62
63/// Generic access types
64#[derive(Copy, Clone, Debug, Default, PartialEq)]
65pub enum AccessType {
66    #[default]
67    ReadRequest,
68    WriteRequest,
69    WriteNonPostedRequest,
70    ReadResponse,
71    WriteNonPostedResponse,
72    Control,
73    BarrierRequest,
74    BarrierResponse,
75}
76
77impl fmt::Display for AccessType {
78    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79        match self {
80            AccessType::ReadRequest => {
81                write!(f, "ReadRequest")
82            }
83            AccessType::WriteRequest => {
84                write!(f, "WriteRequest")
85            }
86            AccessType::WriteNonPostedRequest => {
87                write!(f, "WriteNonPostedRequest")
88            }
89            AccessType::ReadResponse => {
90                write!(f, "ReadResponse")
91            }
92            AccessType::WriteNonPostedResponse => {
93                write!(f, "WriteNonPostedResponse")
94            }
95            AccessType::BarrierRequest => write!(f, "BarrierRequest"),
96            AccessType::BarrierResponse => write!(f, "BarrierResponse"),
97            AccessType::Control => {
98                write!(f, "Control")
99            }
100        }
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    // Test added for code coverage
109    #[test]
110    fn access_type_display_names_match_variants() {
111        assert_eq!(AccessType::ReadRequest.to_string(), "ReadRequest");
112        assert_eq!(AccessType::WriteRequest.to_string(), "WriteRequest");
113        assert_eq!(
114            AccessType::WriteNonPostedRequest.to_string(),
115            "WriteNonPostedRequest"
116        );
117        assert_eq!(AccessType::ReadResponse.to_string(), "ReadResponse");
118        assert_eq!(
119            AccessType::WriteNonPostedResponse.to_string(),
120            "WriteNonPostedResponse"
121        );
122        assert_eq!(AccessType::Control.to_string(), "Control");
123    }
124}