      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

    //! Variable-length integer commonly used in the Bitcoin [P2P protocol](https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers)

    use std::{fmt, io};
    use {
    	Serializable, Stream,
    	Deserializable, Reader, Error as ReaderError
    };

    /// A type of variable-length integer commonly used in the Bitcoin P2P protocol and Bitcoin serialized data structures.
    #[derive(Default, Debug, Clone, Copy, PartialEq)]
    pub struct CompactInteger(u64);

    impl fmt::Display for CompactInteger {
    	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    		self.0.fmt(f)
    	}
    }

    impl From<CompactInteger> for usize {
    	fn from(i: CompactInteger) -> Self {
    		i.0 as usize
    	}
    }

    impl From<CompactInteger> for u64 {
    	fn from(i: CompactInteger) -> Self {
    		i.0
    	}
    }

    impl From<u8> for CompactInteger {
    	fn from(i: u8) -> Self {
    		CompactInteger(i as u64)
    	}
    }

    impl From<u16> for CompactInteger {
    	fn from(i: u16) -> Self {
    		CompactInteger(i as u64)
    	}
    }

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

    impl From<usize> for CompactInteger {
    	fn from(i: usize) -> Self {
    		CompactInteger(i as u64)
    	}
    }

    impl From<u64> for CompactInteger {
    	fn from(i: u64) -> Self {
    		CompactInteger(i)
    	}
    }

    impl Serializable for CompactInteger {
    	fn serialize(&self, stream: &mut Stream) {
    		match self.0 {
    			0...0xfc => {
    				stream.append(&(self.0 as u8));
    			},
    			0xfd...0xffff => {
    				stream
    					.append(&0xfdu8)
    					.append(&(self.0 as u16));
    			},
    			0x10000...0xffff_ffff => {
    				stream
    					.append(&0xfeu8)
    					.append(&(self.0 as u32));
    			},
    			_ => {
    				stream
    					.append(&0xffu8)
    					.append(&self.0);
    			}
    		}
    	}

    	fn serialized_size(&self) -> usize {
    		match self.0 {
    			0...0xfc => 1,
    			0xfd...0xffff => 3,
    			0x10000...0xffff_ffff => 5,
    			_ => 9,
    		}
    	}
    }

    impl Deserializable for CompactInteger {
    	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
    		let result = match try!(reader.read::<u8>()) {
    			i @ 0...0xfc => i.into(),
    			0xfd => try!(reader.read::<u16>()).into(),
    			0xfe => try!(reader.read::<u32>()).into(),
    			_ => try!(reader.read::<u64>()).into(),
    		};

    		Ok(result)
    	}
    }

    #[cfg(test)]
    mod tests {
    	use {Reader, Error as ReaderError, Stream};
    	use super::CompactInteger;

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

    		stream
    			.append(&CompactInteger::from(0u64))
    			.append(&CompactInteger::from(0xfcu64))
    			.append(&CompactInteger::from(0xfdu64))
    			.append(&CompactInteger::from(0xffffu64))
    			.append(&CompactInteger::from(0x10000u64))
    			.append(&CompactInteger::from(0xffff_ffffu64))
    			.append(&CompactInteger::from(0x1_0000_0000u64));

    		let expected = vec![
    			0,
    			0xfc,
    			0xfd, 0xfd, 0x00,
    			0xfd, 0xff, 0xff,
    			0xfe, 0x00, 0x00, 0x01, 0x00,
    			0xfe, 0xff, 0xff, 0xff, 0xff,
    			0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    		].into();

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

    	#[test]
    	fn test_compact_integer_reader() {
    		let buffer = vec![
    			0,
    			0xfc,
    			0xfd, 0xfd, 0x00,
    			0xfd, 0xff, 0xff,
    			0xfe, 0x00, 0x00, 0x01, 0x00,
    			0xfe, 0xff, 0xff, 0xff, 0xff,
    			0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    		];

    		let mut reader = Reader::new(&buffer);
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0u64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0xfcu64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0xfdu64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0xffffu64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0x10000u64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0xffff_ffffu64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap(), 0x1_0000_0000u64.into());
    		assert_eq!(reader.read::<CompactInteger>().unwrap_err(), ReaderError::UnexpectedEnd);
    	}
    }

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