      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

    use std::io;
    use hash::H256;
    use ser::{Serializable, Stream, Deserializable, Reader, Error as ReaderError};

    #[derive(Debug, PartialEq, Clone, Copy)]
    #[repr(u32)]
    pub enum InventoryType {
    	Error = 0,
    	MessageTx = 1,
    	MessageBlock = 2,
    	MessageFilteredBlock = 3,
    	MessageCompactBlock = 4,
    	MessageWitnessTx = 0x40000001,
    	MessageWitnessBlock = 0x40000002,
    	MessageWitnessFilteredBlock = 0x40000003,
    }

    impl InventoryType {
    	pub fn from_u32(v: u32) -> Option<Self> {
    		match v {
    			0 => Some(InventoryType::Error),
    			1 => Some(InventoryType::MessageTx),
    			2 => Some(InventoryType::MessageBlock),
    			3 => Some(InventoryType::MessageFilteredBlock),
    			4 => Some(InventoryType::MessageCompactBlock),
    			0x40000001 => Some(InventoryType::MessageWitnessTx),
    			0x40000002 => Some(InventoryType::MessageWitnessBlock),
    			0x40000003 => Some(InventoryType::MessageWitnessFilteredBlock),
    			_ => None
    		}
    	}
    }

    impl From<InventoryType> for u32 {
    	fn from(i: InventoryType) -> Self {
    		i as u32
    	}
    }

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

    impl Deserializable for InventoryType {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
    		let t: u32 = try!(reader.read());
    		InventoryType::from_u32(t).ok_or(ReaderError::MalformedData)
    	}
    }

    #[derive(Debug, PartialEq, Clone)]
    pub struct InventoryVector {
    	pub inv_type: InventoryType,
    	pub hash: H256,
    }

    impl InventoryVector {
    	pub fn tx(hash: H256) -> Self {
    		InventoryVector {
    			inv_type: InventoryType::MessageTx,
    			hash: hash,
    		}
    	}

    	pub fn witness_tx(hash: H256) -> Self {
    		InventoryVector {
    			inv_type: InventoryType::MessageWitnessTx,
    			hash: hash,
    		}
    	}

    	pub fn block(hash: H256) -> Self {
    		InventoryVector {
    			inv_type: InventoryType::MessageBlock,
    			hash: hash,
    		}
    	}

    	pub fn witness_block(hash: H256) -> Self {
    		InventoryVector {
    			inv_type: InventoryType::MessageWitnessBlock,
    			hash: hash,
    		}
    	}
    }

    impl Serializable for InventoryVector {
    	fn serialize(&self, stream: &mut Stream) {
    		stream
    			.append(&self.inv_type)
    			.append(&self.hash);
    	}
    }

    impl Deserializable for InventoryVector {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
    		let vec = InventoryVector {
    			inv_type: try!(reader.read()),
    			hash: try!(reader.read()),
    		};

    		Ok(vec)
    	}
    }

    #[cfg(test)]
    mod tests {
    	use bytes::Bytes;
    	use ser::{serialize, deserialize};
    	use super::{InventoryVector, InventoryType};

    	#[test]
    	fn test_inventory_serialize() {
    		let expected = "020000000400000000000000000000000000000000000000000000000000000000000000".into();

    		let inventory = InventoryVector {
    			inv_type: InventoryType::MessageBlock,
    			hash: 4u8.into(),
    		};

    		assert_eq!(serialize(&inventory), expected);
    	}

    	#[test]
    	fn test_inventory_deserialize() {
    		let raw: Bytes = "020000000400000000000000000000000000000000000000000000000000000000000000".into();

    		let expected = InventoryVector {
    			inv_type: InventoryType::MessageBlock,
    			hash: 4u8.into(),
    		};

    		assert_eq!(expected, deserialize(raw.as_ref()).unwrap());
    	}

    	#[test]
    	fn test_inventory_type_conversion() {
    		assert_eq!(0u32, InventoryType::Error.into());
    		assert_eq!(1u32, InventoryType::MessageTx.into());
    		assert_eq!(2u32, InventoryType::MessageBlock.into());
    		assert_eq!(3u32, InventoryType::MessageFilteredBlock.into());
    		assert_eq!(4u32, InventoryType::MessageCompactBlock.into());
    		assert_eq!(0x40000001u32, InventoryType::MessageWitnessTx.into());
    		assert_eq!(0x40000002u32, InventoryType::MessageWitnessBlock.into());
    		assert_eq!(0x40000003u32, InventoryType::MessageWitnessFilteredBlock.into());

    		assert_eq!(InventoryType::from_u32(0).unwrap(), InventoryType::Error);
    		assert_eq!(InventoryType::from_u32(1).unwrap(), InventoryType::MessageTx);
    		assert_eq!(InventoryType::from_u32(2).unwrap(), InventoryType::MessageBlock);
    		assert_eq!(InventoryType::from_u32(3).unwrap(), InventoryType::MessageFilteredBlock);
    		assert_eq!(InventoryType::from_u32(4).unwrap(), InventoryType::MessageCompactBlock);
    		assert_eq!(InventoryType::from_u32(0x40000001).unwrap(), InventoryType::MessageWitnessTx);
    		assert_eq!(InventoryType::from_u32(0x40000002).unwrap(), InventoryType::MessageWitnessBlock);
    		assert_eq!(InventoryType::from_u32(0x40000003).unwrap(), InventoryType::MessageWitnessFilteredBlock);
    	}
    }

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