1 //! Serialization for client-server communication.
6 use std::num::NonZeroU32;
10 pub(super) type Writer = super::buffer::Buffer<u8>;
12 pub(super) trait Encode<S>: Sized {
13 fn encode(self, w: &mut Writer, s: &mut S);
16 pub(super) type Reader<'a> = &'a [u8];
18 pub(super) trait Decode<'a, 's, S>: Sized {
19 fn decode(r: &mut Reader<'a>, s: &'s S) -> Self;
22 pub(super) trait DecodeMut<'a, 's, S>: Sized {
23 fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self;
26 macro_rules! rpc_encode_decode {
28 impl<S> Encode<S> for $ty {
29 fn encode(mut self, w: &mut Writer, s: &mut S) {
31 while byte & 0x80 != 0 {
32 byte = (self & 0x7f) as u8;
42 impl<S> DecodeMut<'_, '_, S> for $ty {
43 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
47 while byte & 0x80 != 0 {
48 byte = u8::decode(r, s);
49 v |= ((byte & 0x7f) as Self) << shift;
56 (struct $name:ident { $($field:ident),* $(,)? }) => {
57 impl<S> Encode<S> for $name {
58 fn encode(self, w: &mut Writer, s: &mut S) {
59 $(self.$field.encode(w, s);)*
63 impl<S> DecodeMut<'_, '_, S> for $name {
64 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
66 $($field: DecodeMut::decode(r, s)),*
71 (enum $name:ident $(<$($T:ident),+>)? { $($variant:ident $(($field:ident))*),* $(,)? }) => {
72 impl<S, $($($T: Encode<S>),+)?> Encode<S> for $name $(<$($T),+>)* {
73 fn encode(self, w: &mut Writer, s: &mut S) {
74 // HACK(eddyb): `Tag` enum duplicated between the
75 // two impls as there's no other place to stash it.
76 #[allow(non_upper_case_globals)]
78 #[repr(u8)] enum Tag { $($variant),* }
80 $(pub const $variant: u8 = Tag::$variant as u8;)*
84 $($name::$variant $(($field))* => {
85 tag::$variant.encode(w, s);
86 $($field.encode(w, s);)*
92 impl<S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)*> DecodeMut<'a, '_, S>
93 for $name $(<$($T),+>)*
95 fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
96 // HACK(eddyb): `Tag` enum duplicated between the
97 // two impls as there's no other place to stash it.
98 #[allow(non_upper_case_globals)]
100 #[repr(u8)] enum Tag { $($variant),* }
102 $(pub const $variant: u8 = Tag::$variant as u8;)*
105 match u8::decode(r, s) {
107 $(let $field = DecodeMut::decode(r, s);)*
108 $name::$variant $(($field))*
117 impl<S> Encode<S> for () {
118 fn encode(self, _: &mut Writer, _: &mut S) {}
121 impl<S> DecodeMut<'_, '_, S> for () {
122 fn decode(_: &mut Reader<'_>, _: &mut S) -> Self {}
125 impl<S> Encode<S> for u8 {
126 fn encode(self, w: &mut Writer, _: &mut S) {
127 w.write_all(&[self]).unwrap();
131 impl<S> DecodeMut<'_, '_, S> for u8 {
132 fn decode(r: &mut Reader<'_>, _: &mut S) -> Self {
139 rpc_encode_decode!(uleb128 u32);
140 rpc_encode_decode!(uleb128 usize);
142 impl<S> Encode<S> for bool {
143 fn encode(self, w: &mut Writer, s: &mut S) {
144 (self as u8).encode(w, s);
148 impl<S> DecodeMut<'_, '_, S> for bool {
149 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
150 match u8::decode(r, s) {
158 impl<S> Encode<S> for char {
159 fn encode(self, w: &mut Writer, s: &mut S) {
160 (self as u32).encode(w, s);
164 impl<S> DecodeMut<'_, '_, S> for char {
165 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
166 char::from_u32(u32::decode(r, s)).unwrap()
170 impl<S> Encode<S> for NonZeroU32 {
171 fn encode(self, w: &mut Writer, s: &mut S) {
172 self.get().encode(w, s);
176 impl<S> DecodeMut<'_, '_, S> for NonZeroU32 {
177 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
178 Self::new(u32::decode(r, s)).unwrap()
182 impl<S, A: Encode<S>, B: Encode<S>> Encode<S> for (A, B) {
183 fn encode(self, w: &mut Writer, s: &mut S) {
189 impl<S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S>
192 fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
193 (DecodeMut::decode(r, s), DecodeMut::decode(r, s))
219 impl<S> Encode<S> for &[u8] {
220 fn encode(self, w: &mut Writer, s: &mut S) {
221 self.len().encode(w, s);
222 w.write_all(self).unwrap();
226 impl<S> DecodeMut<'a, '_, S> for &'a [u8] {
227 fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
228 let len = usize::decode(r, s);
235 impl<S> Encode<S> for &str {
236 fn encode(self, w: &mut Writer, s: &mut S) {
237 self.as_bytes().encode(w, s);
241 impl<S> DecodeMut<'a, '_, S> for &'a str {
242 fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
243 str::from_utf8(<&[u8]>::decode(r, s)).unwrap()
247 impl<S> Encode<S> for String {
248 fn encode(self, w: &mut Writer, s: &mut S) {
249 self[..].encode(w, s);
253 impl<S> DecodeMut<'_, '_, S> for String {
254 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
255 <&str>::decode(r, s).to_string()
259 /// Simplied version of panic payloads, ignoring
260 /// types other than `&'static str` and `String`.
261 pub enum PanicMessage {
262 StaticStr(&'static str),
267 impl From<Box<dyn Any + Send>> for PanicMessage {
268 fn from(payload: Box<dyn Any + Send + 'static>) -> Self {
269 if let Some(s) = payload.downcast_ref::<&'static str>() {
270 return PanicMessage::StaticStr(s);
272 if let Ok(s) = payload.downcast::<String>() {
273 return PanicMessage::String(*s);
275 PanicMessage::Unknown
279 impl Into<Box<dyn Any + Send>> for PanicMessage {
280 fn into(self) -> Box<dyn Any + Send> {
282 PanicMessage::StaticStr(s) => Box::new(s),
283 PanicMessage::String(s) => Box::new(s),
284 PanicMessage::Unknown => {
285 struct UnknownPanicMessage;
286 Box::new(UnknownPanicMessage)
293 pub fn as_str(&self) -> Option<&str> {
295 PanicMessage::StaticStr(s) => Some(s),
296 PanicMessage::String(s) => Some(s),
297 PanicMessage::Unknown => None,
302 impl<S> Encode<S> for PanicMessage {
303 fn encode(self, w: &mut Writer, s: &mut S) {
304 self.as_str().encode(w, s);
308 impl<S> DecodeMut<'_, '_, S> for PanicMessage {
309 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
310 match Option::<String>::decode(r, s) {
311 Some(s) => PanicMessage::String(s),
312 None => PanicMessage::Unknown,