workflow_rpc/
id.rs

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