      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
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349

    use std::io;
    use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian};
    use bytes::Bytes;
    use compact::Compact;
    use hash::{H32, H48, H96, H160, H256, H264, H512, H520};
    use compact_integer::CompactInteger;
    use {Serializable, Stream, Deserializable, Reader, Error};

    impl Serializable for bool {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_u8(*self as u8).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		1
    	}
    }

    impl Serializable for i32 {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_i32::<LittleEndian>(*self).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		4
    	}
    }

    impl Serializable for i64 {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_i64::<LittleEndian>(*self).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		8
    	}
    }

    impl Serializable for u8 {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_u8(*self).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		1
    	}
    }

    impl Serializable for u16 {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_u16::<LittleEndian>(*self).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		2
    	}
    }

    impl Serializable for u32 {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_u32::<LittleEndian>(*self).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		4
    	}
    }

    impl Serializable for u64 {
    	#[inline]
    	fn serialize(&self, s: &mut Stream) {
    		s.write_u64::<LittleEndian>(*self).unwrap();
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		8
    	}
    }

    impl Deserializable for bool {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		let value = try!(reader.read_u8());
    		match value {
    			0 => Ok(false),
    			1 => Ok(true),
    			_ => Err(Error::MalformedData),
    		}
    	}
    }

    impl Deserializable for i32 {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		Ok(try!(reader.read_i32::<LittleEndian>()))
    	}
    }

    impl Deserializable for i64 {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		Ok(try!(reader.read_i64::<LittleEndian>()))
    	}
    }

    impl Deserializable for u8 {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		Ok(try!(reader.read_u8()))
    	}
    }

    impl Deserializable for u16 {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		Ok(try!(reader.read_u16::<LittleEndian>()))
    	}
    }

    impl Deserializable for u32 {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		Ok(try!(reader.read_u32::<LittleEndian>()))
    	}
    }

    impl Deserializable for u64 {
    	#[inline]
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		Ok(try!(reader.read_u64::<LittleEndian>()))
    	}
    }

    impl Serializable for String {
    	fn serialize(&self, stream: &mut Stream) {
    		let bytes: &[u8] = self.as_ref();
    		stream
    			.append(&CompactInteger::from(bytes.len()))
    			.append_slice(bytes);
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		let bytes: &[u8] = self.as_ref();
    		CompactInteger::from(bytes.len()).serialized_size() + bytes.len()
    	}
    }

    impl<'a> Serializable for &'a str {
    	fn serialize(&self, stream: &mut Stream) {
    		let bytes: &[u8] = self.as_bytes();
    		stream
    			.append(&CompactInteger::from(bytes.len()))
    			.append_slice(bytes);
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		let bytes: &[u8] = self.as_bytes();
    		CompactInteger::from(bytes.len()).serialized_size() + bytes.len()
    	}
    }

    impl Deserializable for String {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		let bytes: Bytes = try!(reader.read());
    		Ok(String::from_utf8_lossy(&bytes).into_owned())
    	}
    }

    macro_rules! impl_ser_for_hash {
    	($name: ident, $size: expr) => {
    		impl Serializable for $name {
    			fn serialize(&self, stream: &mut Stream) {
    				stream.append_slice(&**self);
    			}

    			#[inline]
    			fn serialized_size(&self) -> usize {
    				$size
    			}
    		}

    		impl Deserializable for $name {
    			fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    				let mut result = Self::default();
    				try!(reader.read_slice(&mut *result));
    				Ok(result)
    			}
    		}
    	}
    }

    impl_ser_for_hash!(H32, 4);
    impl_ser_for_hash!(H48, 6);
    impl_ser_for_hash!(H96, 12);
    impl_ser_for_hash!(H160, 20);
    impl_ser_for_hash!(H256, 32);
    impl_ser_for_hash!(H264, 33);
    impl_ser_for_hash!(H512, 64);
    impl_ser_for_hash!(H520, 65);

    impl Serializable for Bytes {
    	fn serialize(&self, stream: &mut Stream) {
    		stream
    			.append(&CompactInteger::from(self.len()))
    			.append_slice(self);
    	}

    	#[inline]
    	fn serialized_size(&self) -> usize {
    		CompactInteger::from(self.len()).serialized_size() + self.len()
    	}
    }

    impl Deserializable for Bytes {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		let len = try!(reader.read::<CompactInteger>());
    		let mut bytes = Bytes::new_with_len(len.into());
    		try!(reader.read_slice(&mut bytes));
    		Ok(bytes)
    	}
    }

    impl Serializable for Compact {
    	fn serialize(&self, stream: &mut Stream) {
    		stream.append(&u32::from(*self));
    	}
    }

    impl Deserializable for Compact {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, Error> where T: io::Read {
    		reader.read::<u32>().map(Compact::new)
    	}
    }

    #[cfg(test)]
    mod tests {
    	use bytes::Bytes;
    	use {serialize, deserialize, deserialize_iterator, Stream, Reader, Error};

    	#[test]
    	fn test_reader_read() {
    		let buffer = vec![
    			1,
    			2, 0,
    			3, 0, 0, 0,
    			4, 0, 0, 0, 0, 0, 0, 0
    		];

    		let mut reader = Reader::new(&buffer);
    		assert!(!reader.is_finished());
    		assert_eq!(1u8, reader.read().unwrap());
    		assert_eq!(2u16, reader.read().unwrap());
    		assert_eq!(3u32, reader.read().unwrap());
    		assert_eq!(4u64, reader.read().unwrap());
    		assert!(reader.is_finished());
    		assert_eq!(Error::UnexpectedEnd, reader.read::<u8>().unwrap_err());
    	}

    	#[test]
    	fn test_reader_iterator() {
    		let buffer = vec![
    			1u8, 0,
    			2, 0,
    			3, 0,
    			4, 0,
    		];

    		let result = deserialize_iterator(&buffer as &[u8]).collect::<Result<Vec<u16>, _>>().unwrap();
    		assert_eq!(result, vec![1u16, 2, 3, 4]);
    	}

    	#[test]
    	fn test_stream_append() {
    		let mut stream = Stream::default();

    		stream
    			.append(&1u8)
    			.append(&2u16)
    			.append(&3u32)
    			.append(&4u64);

    		let expected = vec![
    			1u8,
    			2, 0,
    			3, 0, 0, 0,
    			4, 0, 0, 0, 0, 0, 0, 0,
    		].into();

    		assert_eq!(stream.out(), expected);
    	}

    	#[test]
    	fn test_bytes_deserialize() {
    		let raw: Bytes = "020145".into();
    		let expected: Bytes = "0145".into();
    		assert_eq!(expected, deserialize(raw.as_ref()).unwrap());
    	}

    	#[test]
    	fn test_bytes_serialize() {
    		let expected: Bytes = "020145".into();
    		let bytes: Bytes = "0145".into();
    		assert_eq!(expected, serialize(&bytes));
    	}

    	#[test]
    	fn test_string_serialize() {
    		let expected: Bytes = "0776657273696f6e".into();
    		let s: String = "version".into();
    		assert_eq!(serialize(&s), expected);
    		let expected: Bytes = "00".into();
    		let s: String = "".into();
    		assert_eq!(serialize(&s), expected);
    	}

    	#[test]
    	fn test_string_deserialize() {
    		let raw: Bytes = "0776657273696f6e".into();
    		let expected: String = "version".into();
    		assert_eq!(expected, deserialize::<_, String>(raw.as_ref()).unwrap());
    		let raw: Bytes = "00".into();
    		let expected: String = "".into();
    		assert_eq!(expected, deserialize::<_, String>(raw.as_ref()).unwrap());
    	}

    	#[test]
    	fn test_steam_append_slice() {
    		let mut slice = [0u8; 4];
    		slice[0] = 0x64;
    		let mut stream = Stream::default();
    		stream.append_slice(&slice);
    		assert_eq!(stream.out(), "64000000".into());
    	}
    }

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