memberlist_proto/
bad_state.rs1use super::{Data, DataRef, DecodeError, EncodeError, WireType};
2
3macro_rules! bad_bail {
4 (
5 $(#[$meta:meta])*
6 $name: ident
7 ) => {
8 $(#[$meta])*
9 #[viewit::viewit(
10 getters(vis_all = "pub"),
11 setters(vis_all = "pub", prefix = "with")
12 )]
13 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
14 #[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
15 #[cfg_attr(any(feature = "arbitrary", test), derive(arbitrary::Arbitrary))]
16 pub struct $name<I> {
17 #[viewit(
19 getter(const, attrs(doc = "Returns the incarnation of the message")),
20 setter(
21 const,
22 attrs(doc = "Sets the incarnation of the message (Builder pattern)")
23 )
24 )]
25 incarnation: u32,
26 #[viewit(
28 getter(const, style = "ref", attrs(doc = "Returns the node of the message")),
29 setter(attrs(doc = "Sets the node of the message (Builder pattern)"))
30 )]
31 node: I,
32 #[viewit(
34 getter(const, style = "ref", attrs(doc = "Returns the source node of the message")),
35 setter(attrs(doc = "Sets the source node of the message (Builder pattern)"))
36 )]
37 from: I,
38 }
39
40 paste::paste! {
41 const [< $name:upper _INCARNATION_TAG >]: u8 = 1;
42 const [< $name:upper _INCARNATION_BYTE >]: u8 = super::merge(WireType::Varint, [< $name:upper _ INCARNATION_TAG >]);
43
44 const [< $name:upper _NODE_TAG >]: u8 = 2;
45 const [< $name:upper _FROM_TAG >]: u8 = 3;
46
47 impl<'a, I: Data> DataRef<'a, $name<I>> for $name<I::Ref<'a>> {
48 fn decode(src: &'a [u8]) -> Result<(usize, Self), DecodeError>
49 where
50 Self: Sized,
51 {
52 let mut node = None;
53 let mut from = None;
54 let mut incarnation = None;
55
56 let mut offset = 0;
57 while offset < src.len() {
58 match src[offset] {
59 [< $name:upper _INCARNATION_BYTE >] => {
60 if incarnation.is_some() {
61 return Err(DecodeError::duplicate_field(stringify!($name), "incarnation", [< $name:upper _INCARNATION_TAG >]));
62 }
63 offset += 1;
64
65 let (bytes_read, value) = <u32 as DataRef<u32>>::decode(&src[offset..])?;
66 offset += bytes_read;
67 incarnation = Some(value);
68 }
69 b if b == $name::<I>::node_byte() => {
70 if node.is_some() {
71 return Err(DecodeError::duplicate_field(stringify!($name), "node", $name::<I>::node_byte()));
72 }
73 offset += 1;
74
75 let (bytes_read, value) = I::Ref::decode_length_delimited(&src[offset..])?;
76 offset += bytes_read;
77 node = Some(value);
78 }
79 b if b == $name::<I>::from_byte() => {
80 if from.is_some() {
81 return Err(DecodeError::duplicate_field(stringify!($name), "from", $name::<I>::from_byte()));
82 }
83 offset += 1;
84
85 let (bytes_read, value) = I::Ref::decode_length_delimited(&src[offset..])?;
86 offset += bytes_read;
87 from = Some(value);
88 }
89 _ => offset += super::skip(stringify!($name), &src[offset..])?,
90 }
91 }
92
93 let incarnation = incarnation.ok_or_else(|| DecodeError::missing_field(stringify!($name), "incarnation"))?;
94 let node = node.ok_or_else(|| DecodeError::missing_field(stringify!($name), "node"))?;
95 let from = from.ok_or_else(|| DecodeError::missing_field(stringify!($name), "from"))?;
96
97 Ok((offset, Self {
98 incarnation,
99 node,
100 from,
101 }))
102 }
103 }
104
105 impl<I: Data> Data for $name<I> {
106 type Ref<'a> = $name<I::Ref<'a>>;
107
108 fn from_ref(val: Self::Ref<'_>) -> Result<Self, DecodeError> {
109 let Self::Ref { incarnation, node, from } = val;
110 I::from_ref(node)
111 .and_then(|node| I::from_ref(from).map(|from| Self::new(incarnation, node, from)))
112 }
113
114 fn encoded_len(&self) -> usize {
115 let mut len = 1 + self.incarnation.encoded_len();
116 len += 1 + self.node.encoded_len_with_length_delimited();
117 len += 1 + self.from.encoded_len_with_length_delimited();
118 len
119 }
120
121 fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError> {
122 macro_rules! bail {
123 ($this:ident($offset:expr, $len:ident)) => {
124 if $offset >= $len {
125 return Err(EncodeError::insufficient_buffer($this.encoded_len(), $len));
126 }
127 };
128 }
129
130 let len = buf.len();
131 bail!(self(0, len));
132
133 let mut offset = 0;
134 buf[offset] = [< $name:upper _INCARNATION_BYTE >];
135 offset += 1;
136 offset += self.incarnation.encode(&mut buf[offset..]).map_err(|e| e.update(self.encoded_len(), len))?;
137
138 bail!(self(offset, len));
139 buf[offset] = Self::node_byte();
140 offset += 1;
141 offset += self.node.encode_length_delimited(&mut buf[offset..]).map_err(|e| e.update(self.encoded_len(), len))?;
142
143 bail!(self(offset, len));
144 buf[offset] = Self::from_byte();
145 offset += 1;
146 offset += self.from.encode_length_delimited(&mut buf[offset..]).map_err(|e| e.update(self.encoded_len(), len))?;
147
148 #[cfg(debug_assertions)]
149 super::debug_assert_write_eq::<Self>(offset, self.encoded_len());
150 Ok(offset)
151 }
152 }
153
154 impl<I> $name<I> {
155 #[inline]
156 const fn node_byte() -> u8
157 where
158 I: Data,
159 {
160 super::merge(I::WIRE_TYPE, [< $name:upper _NODE_TAG >])
161 }
162
163 #[inline]
164 const fn from_byte() -> u8
165 where
166 I: Data,
167 {
168 super::merge(I::WIRE_TYPE, [< $name:upper _FROM_TAG >])
169 }
170 }
171 }
172
173 impl<I> $name<I> {
174 #[inline]
176 pub const fn new(incarnation: u32, node: I, from: I) -> Self {
177 Self {
178 incarnation,
179 node,
180 from,
181 }
182 }
183
184 #[inline]
186 pub fn set_incarnation(&mut self, incarnation: u32) -> &mut Self {
187 self.incarnation = incarnation;
188 self
189 }
190
191 #[inline]
193 pub fn set_from(&mut self, source: I) -> &mut Self {
194 self.from = source;
195 self
196 }
197
198 #[inline]
200 pub fn set_node(&mut self, target: I) -> &mut Self {
201 self.node = target;
202 self
203 }
204 }
205 };
206}
207
208bad_bail!(
209 Suspect
211);
212bad_bail!(
213 Dead
215);