Skip to main content

gwr_track/
id.rs

1// Copyright (c) 2023 Graphcore Ltd. All rights reserved.
2
3//! Id
4
5/// IDs that should be unique across the simulation
6///
7/// Each _log_/_trace_ event within the application is given a unique ID to
8/// identify it. There are two reserved ID values: [NO_ID](constant.NO_ID.html)
9/// and [ROOT](constant.ROOT.html)
10#[derive(Copy, Clone, Default, Eq, Hash, PartialEq)]
11pub struct Id(pub u64);
12
13impl std::fmt::Display for Id {
14    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15        write!(f, "{}", self.0)
16    }
17}
18
19impl std::fmt::Debug for Id {
20    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21        write!(f, "{}", self.0)
22    }
23}
24
25impl From<&str> for Id {
26    fn from(val: &str) -> Self {
27        let value = val.parse::<u64>().unwrap();
28        Id(value)
29    }
30}
31
32/// The `Unique` trait provides a unique ID for logging
33pub trait Unique {
34    /// Return a unique ID for an object.
35    fn id(&self) -> Id;
36}
37
38impl Unique for Id {
39    fn id(&self) -> Id {
40        *self
41    }
42}
43
44// Provide Unique for primitive types
45impl Unique for i32 {
46    fn id(&self) -> Id {
47        Id(*self as u64)
48    }
49}
50
51impl Unique for usize {
52    fn id(&self) -> Id {
53        Id(*self as u64)
54    }
55}