Skip to main content

unified_bridge/
network_id.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4
5/// A network ID.
6///
7/// 0 refers to ethereum, and rollups are numbered from 1 to `u32::MAX`.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
9#[cfg_attr(feature = "testutils", derive(arbitrary::Arbitrary))]
10pub struct NetworkId(u32);
11
12impl Display for NetworkId {
13    #[inline]
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        self.0.fmt(f)
16    }
17}
18
19impl NetworkId {
20    pub const BITS: usize = u32::BITS as usize;
21
22    pub const ETH_L1: NetworkId = NetworkId::new(0);
23
24    #[inline]
25    pub const fn new(value: u32) -> Self {
26        Self(value)
27    }
28
29    #[inline]
30    pub const fn to_u32(self) -> u32 {
31        self.0
32    }
33
34    #[inline]
35    pub const fn to_be_bytes(self) -> [u8; 4] {
36        self.0.to_be_bytes()
37    }
38
39    #[inline]
40    pub const fn to_le_bytes(self) -> [u8; 4] {
41        self.0.to_le_bytes()
42    }
43}
44
45impl From<u32> for NetworkId {
46    #[inline]
47    fn from(value: u32) -> Self {
48        Self(value)
49    }
50}
51
52impl From<NetworkId> for u32 {
53    #[inline]
54    fn from(value: NetworkId) -> Self {
55        value.0
56    }
57}