      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

    use std::io::{self, Write};

    #[cfg(feature = "brotli")]
    use brotli2::write::BrotliDecoder;
    use bytes::{Bytes, BytesMut};
    use error::PayloadError;
    #[cfg(feature = "flate2")]
    use flate2::write::{GzDecoder, ZlibDecoder};
    use header::ContentEncoding;
    use http::header::{HeaderMap, CONTENT_ENCODING};
    use payload::{PayloadSender, PayloadStatus, PayloadWriter};

    pub(crate) enum PayloadType {
        Sender(PayloadSender),
        Encoding(Box<EncodedPayload>),
    }

    impl PayloadType {
        #[cfg(any(feature = "brotli", feature = "flate2"))]
        pub fn new(headers: &HeaderMap, sender: PayloadSender) -> PayloadType {
            // check content-encoding
            let enc = if let Some(enc) = headers.get(CONTENT_ENCODING) {
                if let Ok(enc) = enc.to_str() {
                    ContentEncoding::from(enc)
                } else {
                    ContentEncoding::Auto
                }
            } else {
                ContentEncoding::Auto
            };

            match enc {
                ContentEncoding::Auto | ContentEncoding::Identity => {
                    PayloadType::Sender(sender)
                }
                _ => PayloadType::Encoding(Box::new(EncodedPayload::new(sender, enc))),
            }
        }

        #[cfg(not(any(feature = "brotli", feature = "flate2")))]
        pub fn new(headers: &HeaderMap, sender: PayloadSender) -> PayloadType {
            PayloadType::Sender(sender)
        }
    }

    impl PayloadWriter for PayloadType {
        #[inline]
        fn set_error(&mut self, err: PayloadError) {
            match *self {
                PayloadType::Sender(ref mut sender) => sender.set_error(err),
                PayloadType::Encoding(ref mut enc) => enc.set_error(err),
            }
        }

        #[inline]
        fn feed_eof(&mut self) {
            match *self {
                PayloadType::Sender(ref mut sender) => sender.feed_eof(),
                PayloadType::Encoding(ref mut enc) => enc.feed_eof(),
            }
        }

        #[inline]
        fn feed_data(&mut self, data: Bytes) {
            match *self {
                PayloadType::Sender(ref mut sender) => sender.feed_data(data),
                PayloadType::Encoding(ref mut enc) => enc.feed_data(data),
            }
        }

        #[inline]
        fn need_read(&self) -> PayloadStatus {
            match *self {
                PayloadType::Sender(ref sender) => sender.need_read(),
                PayloadType::Encoding(ref enc) => enc.need_read(),
            }
        }
    }

    /// Payload wrapper with content decompression support
    pub(crate) struct EncodedPayload {
        inner: PayloadSender,
        error: bool,
        payload: PayloadStream,
    }

    impl EncodedPayload {
        pub fn new(inner: PayloadSender, enc: ContentEncoding) -> EncodedPayload {
            EncodedPayload {
                inner,
                error: false,
                payload: PayloadStream::new(enc),
            }
        }
    }

    impl PayloadWriter for EncodedPayload {
        fn set_error(&mut self, err: PayloadError) {
            self.inner.set_error(err)
        }

        fn feed_eof(&mut self) {
            if !self.error {
                match self.payload.feed_eof() {
                    Err(err) => {
                        self.error = true;
                        self.set_error(PayloadError::Io(err));
                    }
                    Ok(value) => {
                        if let Some(b) = value {
                            self.inner.feed_data(b);
                        }
                        self.inner.feed_eof();
                    }
                }
            }
        }

        fn feed_data(&mut self, data: Bytes) {
            if self.error {
                return;
            }

            match self.payload.feed_data(data) {
                Ok(Some(b)) => self.inner.feed_data(b),
                Ok(None) => (),
                Err(e) => {
                    self.error = true;
                    self.set_error(e.into());
                }
            }
        }

        #[inline]
        fn need_read(&self) -> PayloadStatus {
            self.inner.need_read()
        }
    }

    pub(crate) enum Decoder {
        #[cfg(feature = "flate2")]
        Deflate(Box<ZlibDecoder<Writer>>),
        #[cfg(feature = "flate2")]
        Gzip(Box<GzDecoder<Writer>>),
        #[cfg(feature = "brotli")]
        Br(Box<BrotliDecoder<Writer>>),
        Identity,
    }

    pub(crate) struct Writer {
        buf: BytesMut,
    }

    impl Writer {
        fn new() -> Writer {
            Writer {
                buf: BytesMut::with_capacity(8192),
            }
        }
        fn take(&mut self) -> Bytes {
            self.buf.take().freeze()
        }
    }

    impl io::Write for Writer {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.buf.extend_from_slice(buf);
            Ok(buf.len())
        }
        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    /// Payload stream with decompression support
    pub(crate) struct PayloadStream {
        decoder: Decoder,
    }

    impl PayloadStream {
        pub fn new(enc: ContentEncoding) -> PayloadStream {
            let decoder = match enc {
                #[cfg(feature = "brotli")]
                ContentEncoding::Br => {
                    Decoder::Br(Box::new(BrotliDecoder::new(Writer::new())))
                }
                #[cfg(feature = "flate2")]
                ContentEncoding::Deflate => {
                    Decoder::Deflate(Box::new(ZlibDecoder::new(Writer::new())))
                }
                #[cfg(feature = "flate2")]
                ContentEncoding::Gzip => {
                    Decoder::Gzip(Box::new(GzDecoder::new(Writer::new())))
                }
                _ => Decoder::Identity,
            };
            PayloadStream { decoder }
        }
    }

    impl PayloadStream {
        pub fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
            match self.decoder {
                #[cfg(feature = "brotli")]
                Decoder::Br(ref mut decoder) => match decoder.finish() {
                    Ok(mut writer) => {
                        let b = writer.take();
                        if !b.is_empty() {
                            Ok(Some(b))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(e) => Err(e),
                },
                #[cfg(feature = "flate2")]
                Decoder::Gzip(ref mut decoder) => match decoder.try_finish() {
                    Ok(_) => {
                        let b = decoder.get_mut().take();
                        if !b.is_empty() {
                            Ok(Some(b))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(e) => Err(e),
                },
                #[cfg(feature = "flate2")]
                Decoder::Deflate(ref mut decoder) => match decoder.try_finish() {
                    Ok(_) => {
                        let b = decoder.get_mut().take();
                        if !b.is_empty() {
                            Ok(Some(b))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(e) => Err(e),
                },
                Decoder::Identity => Ok(None),
            }
        }

        pub fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
            match self.decoder {
                #[cfg(feature = "brotli")]
                Decoder::Br(ref mut decoder) => match decoder.write_all(&data) {
                    Ok(_) => {
                        decoder.flush()?;
                        let b = decoder.get_mut().take();
                        if !b.is_empty() {
                            Ok(Some(b))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(e) => Err(e),
                },
                #[cfg(feature = "flate2")]
                Decoder::Gzip(ref mut decoder) => match decoder.write_all(&data) {
                    Ok(_) => {
                        decoder.flush()?;
                        let b = decoder.get_mut().take();
                        if !b.is_empty() {
                            Ok(Some(b))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(e) => Err(e),
                },
                #[cfg(feature = "flate2")]
                Decoder::Deflate(ref mut decoder) => match decoder.write_all(&data) {
                    Ok(_) => {
                        decoder.flush()?;
                        let b = decoder.get_mut().take();
                        if !b.is_empty() {
                            Ok(Some(b))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(e) => Err(e),
                },
                Decoder::Identity => Ok(Some(data)),
            }
        }
    }

# 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`)