1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//!
//! RPC message identifiers. Provides [`Id32`] and [`Id64`]
//! defaults (based on `u32` and `u64` respectively) and allows
//! for a custom construction of RPC message ids
//! using the [`Generator`] trait.
//!

use crate::imports::*;

/// Trait representing RPC message `Id` trait constraints
pub trait IdT:
    Generator
    + Debug
    + Clone
    + Eq
    + Hash
    + BorshSerialize
    + BorshDeserialize
    + Serialize
    + DeserializeOwned
    + Send
    + Sync
    + 'static
{
}
impl<T> IdT for T where
    T: Generator
        + Debug
        + Clone
        + Eq
        + Hash
        + BorshSerialize
        + BorshDeserialize
        + Serialize
        + DeserializeOwned
        + Send
        + Sync
        + 'static
{
}

/// `Id` generation trait. This is typically meant to be a random number
/// generator for a custom message `Id`, but you can also define it to use
/// a sequential generation.
pub trait Generator {
    fn generate() -> Self;
}

/// RPC message id represented by a `u32` type
#[derive(
    Debug, Clone, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct Id32(u32);

impl Generator for Id32 {
    fn generate() -> Self {
        Id32(rand::random())
    }
}

/// RPC message id represented by a `u64` type
#[derive(
    Debug, Clone, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct Id64(u64);

impl Generator for Id64 {
    fn generate() -> Self {
        Id64(rand::random())
    }
}