      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

    use std::io;
    use ser::{
    	Serializable, Stream,
    	Deserializable, Reader, Error as ReaderError,
    };
    use common::NetAddress;
    use {Payload, MessageResult};

    #[derive(Debug, PartialEq)]
    pub enum Addr {
    	V0(V0),
    	V31402(V31402),
    }

    impl Addr {
    	pub fn new(addresses: Vec<AddressEntry>) -> Self {
    		Addr::V31402(V31402 {
    			addresses: addresses,
    		})
    	}
    }

    impl Payload for Addr {
    	fn version() -> u32 {
    		0
    	}

    	fn command() -> &'static str {
    		"addr"
    	}

    	fn deserialize_payload<T>(reader: &mut Reader<T>, version: u32) -> MessageResult<Self> where T: io::Read {
    		let result = if version < 31402 {
    			reader.read().map(Addr::V0)
    		} else {
    			reader.read().map(Addr::V31402)
    		};

    		result.map_err(Into::into)
    	}

    	fn serialize_payload(&self, stream: &mut Stream, version: u32) -> MessageResult<()> {
    		match *self {
    			Addr::V0(ref addr) => addr.serialize(stream),
    			Addr::V31402(ref addr) => {
    				if version < 31402 {
    					let view = V31402AsV0::new(addr);
    					view.serialize(stream);
    				} else {
    					addr.serialize(stream);
    				}
    			}
    		}
    		Ok(())
    	}
    }

    #[derive(Debug, PartialEq)]
    pub struct AddressEntry {
    	pub timestamp: u32,
    	pub address: NetAddress,
    }

    impl Serializable for AddressEntry {
    	fn serialize(&self, stream: &mut Stream) {
    		stream
    			.append(&self.timestamp)
    			.append(&self.address);
    	}
    }

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

    		Ok(entry)
    	}
    }

    #[derive(Debug, PartialEq)]
    pub struct V31402 {
    	pub addresses: Vec<AddressEntry>,
    }

    impl Serializable for V31402 {
    	fn serialize(&self, stream: &mut Stream) {
    		stream.append_list(&self.addresses);
    	}
    }

    impl Deserializable for V31402 {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
    		let result = V31402 {
    			addresses: try!(reader.read_list_max(1000)),
    		};

    		Ok(result)
    	}
    }

    #[derive(Debug, PartialEq)]
    pub struct V0 {
    	pub addresses: Vec<NetAddress>,
    }

    impl Serializable for V0 {
    	fn serialize(&self, stream: &mut Stream) {
    		stream.append_list(&self.addresses);
    	}
    }

    impl Deserializable for V0 {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
    		let result = V0 {
    			addresses: try!(reader.read_list_max(1000)),
    		};

    		Ok(result)
    	}
    }

    struct V31402AsV0<'a> {
    	v: &'a V31402,
    }

    impl<'a> V31402AsV0<'a> {
    	fn new(v: &'a V31402) -> Self {
    		V31402AsV0 {
    			v: v,
    		}
    	}
    }

    impl<'a> Serializable for V31402AsV0<'a> {
    	fn serialize(&self, stream: &mut Stream) {
    		let vec_ref: Vec<&'a NetAddress> = self.v.addresses.iter().map(|x| &x.address).collect();
    		stream.append_list::<NetAddress, &'a NetAddress>(&vec_ref);
    	}
    }

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

    	#[test]
    	fn test_addr_serialize() {
    		let expected: Bytes = "01e215104d010000000000000000000000000000000000ffff0a000001208d".into();
    		let addr = V31402 {
    			addresses: vec![
    				AddressEntry {
    					timestamp: 0x4d1015e2,
    					address: "010000000000000000000000000000000000ffff0a000001208d".into(),
    				},
    			],
    		};

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

    	#[test]
    	fn test_addr_deserialize() {
    		let raw: Bytes = "01e215104d010000000000000000000000000000000000ffff0a000001208d".into();
    		let expected = V31402 {
    			addresses: vec![
    				AddressEntry {
    					timestamp: 0x4d1015e2,
    					address: "010000000000000000000000000000000000ffff0a000001208d".into(),
    				},
    			],
    		};

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

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