Skip to main content

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    /// Generate a new `Id` value.
47    fn generate() -> Self;
48}
49
50/// RPC message id represented by a `u32` type
51#[derive(
52    Debug, Clone, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
53)]
54pub struct Id32(u32);
55
56impl Generator for Id32 {
57    fn generate() -> Self {
58        Id32(rand::random())
59    }
60}
61
62/// RPC message id represented by a `u64` type
63#[derive(
64    Debug, Clone, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
65)]
66pub struct Id64(u64);
67
68impl Generator for Id64 {
69    fn generate() -> Self {
70        Id64(rand::random())
71    }
72}