protobuf_codec/
field_num.rs

1use bytecodec::{ErrorKind, Result};
2
3/// Field number.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
5pub struct FieldNum(u32);
6impl FieldNum {
7    /// Makes a new `FieldNum` instance.
8    ///
9    /// # Errors
10    ///
11    /// [The language guide] says about the valid values of a field number as follows:
12    ///
13    /// > The smallest field number you can specify is `1`, and the largest is `2^29 - 1`, or `536,870,911`.
14    /// > You also cannot use the numbers `19000` through `19999`, as they are reserved for
15    /// > the Protocol Buffers implementation
16    ///
17    /// If `n` violates this restriction, an `ErrorKind::InvalidInput` error will be returned.
18    ///
19    /// [the language guide]: https://developers.google.com/protocol-buffers/docs/proto3
20    pub fn new(n: u32) -> Result<Self> {
21        track_assert_ne!(n, 0, ErrorKind::InvalidInput);
22        track_assert!(n < (1 << 29), ErrorKind::InvalidInput; n);
23        track_assert!(!(19_000 <= n && n < 20_000), ErrorKind::InvalidInput; n);
24        Ok(FieldNum(n))
25    }
26
27    /// Makes a new `FieldNum` instance without checking the value.
28    pub unsafe fn new_unchecked(n: u32) -> Self {
29        FieldNum(n)
30    }
31
32    /// Returns the value of the field number.
33    pub fn as_u32(self) -> u32 {
34        self.0
35    }
36}
37
38macro_rules! impl_from {
39    ($ty:ty, $n:expr) => {
40        impl From<$ty> for FieldNum {
41            fn from(_: $ty) -> Self {
42                FieldNum($n)
43            }
44        }
45    };
46}
47
48/// Field number `1`.
49#[derive(Debug, Default, Clone, Copy)]
50pub struct F1;
51impl_from!(F1, 1);
52
53/// Field number `2`.
54#[derive(Debug, Default, Clone, Copy)]
55pub struct F2;
56impl_from!(F2, 2);
57
58/// Field number `3`.
59#[derive(Debug, Default, Clone, Copy)]
60pub struct F3;
61impl_from!(F3, 3);
62
63/// Field number `4`.
64#[derive(Debug, Default, Clone, Copy)]
65pub struct F4;
66impl_from!(F4, 4);
67
68/// Field number `5`.
69#[derive(Debug, Default, Clone, Copy)]
70pub struct F5;
71impl_from!(F5, 5);
72
73/// Field number `6`.
74#[derive(Debug, Default, Clone, Copy)]
75pub struct F6;
76impl_from!(F6, 6);
77
78/// Field number `7`.
79#[derive(Debug, Default, Clone, Copy)]
80pub struct F7;
81impl_from!(F7, 7);
82
83/// Field number `8`.
84#[derive(Debug, Default, Clone, Copy)]
85pub struct F8;
86impl_from!(F8, 8);
87
88/// Field number `9`.
89#[derive(Debug, Default, Clone, Copy)]
90pub struct F9;
91impl_from!(F9, 9);
92
93/// Field number `10`.
94#[derive(Debug, Default, Clone, Copy)]
95pub struct F10;
96impl_from!(F10, 10);
97
98/// Field number `11`.
99#[derive(Debug, Default, Clone, Copy)]
100pub struct F11;
101impl_from!(F11, 11);
102
103/// Field number `12`.
104#[derive(Debug, Default, Clone, Copy)]
105pub struct F12;
106impl_from!(F12, 12);
107
108/// Field number `13`.
109#[derive(Debug, Default, Clone, Copy)]
110pub struct F13;
111impl_from!(F13, 13);
112
113/// Field number `14`.
114#[derive(Debug, Default, Clone, Copy)]
115pub struct F14;
116impl_from!(F14, 14);
117
118/// Field number `15`.
119#[derive(Debug, Default, Clone, Copy)]
120pub struct F15;
121impl_from!(F15, 15);
122
123/// Field number `16`.
124#[derive(Debug, Default, Clone, Copy)]
125pub struct F16;
126impl_from!(F16, 16);