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    ($msg:expr) => {
26        Err($crate::types::SimError($msg.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    Read,
50    Write,
51    WriteNonPosted,
52    Control,
53}
54
55impl fmt::Display for AccessType {
56    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57        match self {
58            AccessType::Read => {
59                write!(f, "Read")
60            }
61            AccessType::Write => {
62                write!(f, "Write")
63            }
64            AccessType::WriteNonPosted => {
65                write!(f, "WriteNonPosted")
66            }
67            AccessType::Control => {
68                write!(f, "Control")
69            }
70        }
71    }
72}