      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
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319

    // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    //! Serialization for client<->server communication.

    use std::any::Any;
    use std::char;
    use std::io::Write;
    use std::num::NonZeroU32;
    use std::ops::Bound;
    use std::str;

    pub(super) type Writer = super::buffer::Buffer<u8>;

    pub(super) trait Encode<S>: Sized {
        fn encode(self, w: &mut Writer, s: &mut S);
    }

    pub(super) type Reader<'a> = &'a [u8];

    pub(super) trait Decode<'a, 's, S>: Sized {
        fn decode(r: &mut Reader<'a>, s: &'s S) -> Self;
    }

    pub(super) trait DecodeMut<'a, 's, S>: Sized {
        fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self;
    }

    macro_rules! rpc_encode_decode {
        (uleb128 $ty:ty) => {
            impl<S> Encode<S> for $ty {
                fn encode(mut self, w: &mut Writer, s: &mut S) {
                    let mut byte = 0x80;
                    while byte & 0x80 != 0 {
                        byte = (self & 0x7f) as u8;
                        self >>= 7;
                        if self != 0 {
                            byte |= 0x80;
                        }
                        byte.encode(w, s);
                    }
                }
            }

            impl<S> DecodeMut<'_, '_, S> for $ty {
                fn decode(r: &mut Reader, s: &mut S) -> Self {
                    let mut byte = 0x80;
                    let mut v = 0;
                    let mut shift = 0;
                    while byte & 0x80 != 0 {
                        byte = u8::decode(r, s);
                        v |= ((byte & 0x7f) as Self) << shift;
                        shift += 7;
                    }
                    v
                }
            }
        };
        (struct $name:ident { $($field:ident),* $(,)* }) => {
            impl<S> Encode<S> for $name {
                fn encode(self, w: &mut Writer, s: &mut S) {
                    $(self.$field.encode(w, s);)*
                }
            }

            impl<S> DecodeMut<'_, '_, S> for $name {
                fn decode(r: &mut Reader, s: &mut S) -> Self {
                    $name {
                        $($field: DecodeMut::decode(r, s)),*
                    }
                }
            }
        };
        (enum $name:ident $(<$($T:ident),+>)* { $($variant:ident $(($field:ident))*),* $(,)* }) => {
            impl<S, $($($T: Encode<S>),+)*> Encode<S> for $name $(<$($T),+>)* {
                fn encode(self, w: &mut Writer, s: &mut S) {
                    // HACK(eddyb) `Tag` enum duplicated between the
                    // two impls as there's no other place to stash it.
                    #[repr(u8)] enum Tag { $($variant),* }
                    #[allow(non_upper_case_globals)]
                    impl Tag { $(const $variant: u8 = Tag::$variant as u8;)* }

                    match self {
                        $($name::$variant $(($field))* => {
                            <Tag>::$variant.encode(w, s);
                            $($field.encode(w, s);)*
                        })*
                    }
                }
            }

            impl<S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)*> DecodeMut<'a, '_, S>
                for $name $(<$($T),+>)*
            {
                fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
                    // HACK(eddyb) `Tag` enum duplicated between the
                    // two impls as there's no other place to stash it.
                    #[repr(u8)] enum Tag { $($variant),* }
                    #[allow(non_upper_case_globals)]
                    impl Tag { $(const $variant: u8 = Tag::$variant as u8;)* }

                    match u8::decode(r, s) {
                        $(<Tag>::$variant => {
                            $(let $field = DecodeMut::decode(r, s);)*
                            $name::$variant $(($field))*
                        })*
                        _ => unreachable!(),
                    }
                }
            }
        }
    }

    impl<S> Encode<S> for () {
        fn encode(self, _: &mut Writer, _: &mut S) {}
    }

    impl<S> DecodeMut<'_, '_, S> for () {
        fn decode(_: &mut Reader, _: &mut S) -> Self {}
    }

    impl<S> Encode<S> for u8 {
        fn encode(self, w: &mut Writer, _: &mut S) {
            w.write_all(&[self]).unwrap();
        }
    }

    impl<S> DecodeMut<'_, '_, S> for u8 {
        fn decode(r: &mut Reader, _: &mut S) -> Self {
            let x = r[0];
            *r = &r[1..];
            x
        }
    }

    rpc_encode_decode!(uleb128 u32);
    rpc_encode_decode!(uleb128 usize);

    impl<S> Encode<S> for bool {
        fn encode(self, w: &mut Writer, s: &mut S) {
            (self as u8).encode(w, s);
        }
    }

    impl<S> DecodeMut<'_, '_, S> for bool {
        fn decode(r: &mut Reader, s: &mut S) -> Self {
            match u8::decode(r, s) {
                0 => false,
                1 => true,
                _ => unreachable!(),
            }
        }
    }

    impl<S> Encode<S> for char {
        fn encode(self, w: &mut Writer, s: &mut S) {
            (self as u32).encode(w, s);
        }
    }

    impl<S> DecodeMut<'_, '_, S> for char {
        fn decode(r: &mut Reader, s: &mut S) -> Self {
            char::from_u32(u32::decode(r, s)).unwrap()
        }
    }

    impl<S> Encode<S> for NonZeroU32 {
        fn encode(self, w: &mut Writer, s: &mut S) {
            self.get().encode(w, s);
        }
    }

    impl<S> DecodeMut<'_, '_, S> for NonZeroU32 {
        fn decode(r: &mut Reader, s: &mut S) -> Self {
            Self::new(u32::decode(r, s)).unwrap()
        }
    }

    impl<S, A: Encode<S>, B: Encode<S>> Encode<S> for (A, B) {
        fn encode(self, w: &mut Writer, s: &mut S) {
            self.0.encode(w, s);
            self.1.encode(w, s);
        }
    }

    impl<S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S>
        for (A, B)
    {
        fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
            (DecodeMut::decode(r, s), DecodeMut::decode(r, s))
        }
    }

    rpc_encode_decode!(
        enum Bound<T> {
            Included(x),
            Excluded(x),
            Unbounded,
        }
    );

    rpc_encode_decode!(
        enum Option<T> {
            None,
            Some(x),
        }
    );

    rpc_encode_decode!(
        enum Result<T, E> {
            Ok(x),
            Err(e),
        }
    );

    impl<S> Encode<S> for &[u8] {
        fn encode(self, w: &mut Writer, s: &mut S) {
            self.len().encode(w, s);
            w.write_all(self).unwrap();
        }
    }

    impl<S> DecodeMut<'a, '_, S> for &'a [u8] {
        fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
            let len = usize::decode(r, s);
            let xs = &r[..len];
            *r = &r[len..];
            xs
        }
    }

    impl<S> Encode<S> for &str {
        fn encode(self, w: &mut Writer, s: &mut S) {
            self.as_bytes().encode(w, s);
        }
    }

    impl<S> DecodeMut<'a, '_, S> for &'a str {
        fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
            str::from_utf8(<&[u8]>::decode(r, s)).unwrap()
        }
    }

    impl<S> Encode<S> for String {
        fn encode(self, w: &mut Writer, s: &mut S) {
            self[..].encode(w, s);
        }
    }

    impl<S> DecodeMut<'_, '_, S> for String {
        fn decode(r: &mut Reader, s: &mut S) -> Self {
            <&str>::decode(r, s).to_string()
        }
    }

    /// Simplied version of panic payloads, ignoring
    /// types other than `&'static str` and `String`.
    pub enum PanicMessage {
        StaticStr(&'static str),
        String(String),
        Unknown,
    }

    impl From<Box<dyn Any + Send>> for PanicMessage {
        fn from(payload: Box<dyn Any + Send + 'static>) -> Self {
            if let Some(s) = payload.downcast_ref::<&'static str>() {
                return PanicMessage::StaticStr(s);
            }
            if let Ok(s) = payload.downcast::<String>() {
                return PanicMessage::String(*s);
            }
            PanicMessage::Unknown
        }
    }

    impl Into<Box<dyn Any + Send>> for PanicMessage {
        fn into(self) -> Box<dyn Any + Send> {
            match self {
                PanicMessage::StaticStr(s) => Box::new(s),
                PanicMessage::String(s) => Box::new(s),
                PanicMessage::Unknown => {
                    struct UnknownPanicMessage;
                    Box::new(UnknownPanicMessage)
                }
            }
        }
    }

    impl PanicMessage {
        pub fn as_str(&self) -> Option<&str> {
            match self {
                PanicMessage::StaticStr(s) => Some(s),
                PanicMessage::String(s) => Some(s),
                PanicMessage::Unknown => None,
            }
        }
    }

    impl<S> Encode<S> for PanicMessage {
        fn encode(self, w: &mut Writer, s: &mut S) {
            self.as_str().encode(w, s);
        }
    }

    impl<S> DecodeMut<'_, '_, S> for PanicMessage {
        fn decode(r: &mut Reader, s: &mut S) -> Self {
            match Option::<String>::decode(r, s) {
                Some(s) => PanicMessage::String(s),
                None => PanicMessage::Unknown,
            }
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)