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#[macro_export]
23/// Build a [SimError] from a message that supports `to_string`
24macro_rules! sim_error {
25    ($($arg:tt)+) => {
26        Err($crate::types::SimError(format!($($arg)+).to_string()))
27    };
28}
29
30/// The `SimError` is what should be returned in the case of an error
31#[derive(Debug)]
32pub struct SimError(pub String);
33
34impl fmt::Display for SimError {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        write!(f, "Error: {}", self.0)
37    }
38}
39
40impl Error for SimError {}
41
42/// The SimResult is the return type for most simulation functions
43pub type SimResult = Result<(), SimError>;
44
45/// Generic access types
46#[derive(Copy, Clone, Debug, Default, PartialEq)]
47pub enum AccessType {
48    #[default]
49    ReadRequest,
50    WriteRequest,
51    WriteNonPostedRequest,
52    ReadResponse,
53    WriteNonPostedResponse,
54    Control,
55}
56
57impl fmt::Display for AccessType {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            AccessType::ReadRequest => {
61                write!(f, "ReadRequest")
62            }
63            AccessType::WriteRequest => {
64                write!(f, "WriteRequest")
65            }
66            AccessType::WriteNonPostedRequest => {
67                write!(f, "WriteNonPostedRequest")
68            }
69            AccessType::ReadResponse => {
70                write!(f, "ReadResponse")
71            }
72            AccessType::WriteNonPostedResponse => {
73                write!(f, "WriteNonPostedResponse")
74            }
75            AccessType::Control => {
76                write!(f, "Control")
77            }
78        }
79    }
80}