Skip to main content

workflow_nw/ipc/
id.rs

1//!
2//! IPC message identifiers. Provides [`Id64`]
3//! and allows for a custom construction of IPC
4//! message ids using the [`Generator`] trait.
5//!
6
7use crate::ipc::imports::*;
8
9/// Trait representing RPC message `Id` trait constraints
10pub trait IdT:
11    Generator
12    + Debug
13    + Clone
14    + Eq
15    + Hash
16    + BorshSerialize
17    + BorshDeserialize
18    + Serialize
19    + DeserializeOwned
20    + Send
21    + Sync
22    + 'static
23{
24}
25impl<T> IdT for T where
26    T: Generator
27        + Debug
28        + Clone
29        + Eq
30        + Hash
31        + BorshSerialize
32        + BorshDeserialize
33        + Serialize
34        + DeserializeOwned
35        + Send
36        + Sync
37        + 'static
38{
39}
40
41/// `Id` generation trait. This is typically meant to be a random number
42/// generator for a cusom message `Id`, but you can also define it to use
43/// a sequential generation.
44pub trait Generator {
45    /// Generates a new message `Id`.
46    fn generate() -> Self;
47}
48
49/// IPC message id represented by a `u64` type
50#[derive(
51    Debug, Clone, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
52)]
53pub struct Id64(u64);
54
55impl Generator for Id64 {
56    fn generate() -> Self {
57        Id64(rand::random())
58    }
59}