pub enum Size {
Char,
Short,
Long,
LongLong,
}Expand description
Integer size types for packing and unpacking binary data.
The Tor protocol uses fixed-width unsigned integers in network byte order (big-endian). This enum provides methods for encoding and decoding these integers according to the struct pack format.
§Variants
| Variant | Size | Range |
|---|---|---|
Char | 1 byte | 0 to 255 |
Short | 2 bytes | 0 to 65,535 |
Long | 4 bytes | 0 to 4,294,967,295 |
LongLong | 8 bytes | 0 to 18,446,744,073,709,551,615 |
§Example
use stem_rs::client::datatype::Size;
// Pack a 16-bit port number
let port: u64 = 9001;
let packed = Size::Short.pack(port);
assert_eq!(packed, vec![0x23, 0x29]);
// Unpack it back
let unpacked = Size::Short.unpack(&packed).unwrap();
assert_eq!(unpacked, 9001);
// Pop from a larger buffer
let buffer = vec![0x23, 0x29, 0xFF, 0xFF];
let (value, remainder) = Size::Short.pop(&buffer).unwrap();
assert_eq!(value, 9001);
assert_eq!(remainder, &[0xFF, 0xFF]);Variants§
Char
Unsigned 8-bit integer (1 byte).
Short
Unsigned 16-bit integer (2 bytes, big-endian).
Long
Unsigned 32-bit integer (4 bytes, big-endian).
LongLong
Unsigned 64-bit integer (8 bytes, big-endian).
Implementations§
Source§impl Size
impl Size
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the size in bytes for this integer type.
§Example
use stem_rs::client::datatype::Size;
assert_eq!(Size::Char.size(), 1);
assert_eq!(Size::Short.size(), 2);
assert_eq!(Size::Long.size(), 4);
assert_eq!(Size::LongLong.size(), 8);Sourcepub fn pack(&self, value: u64) -> Vec<u8> ⓘ
pub fn pack(&self, value: u64) -> Vec<u8> ⓘ
Packs an integer value into big-endian bytes.
§Arguments
value- The integer value to pack (truncated to fit the size)
§Returns
A Vec<u8> containing the packed bytes in network byte order.
§Example
use stem_rs::client::datatype::Size;
assert_eq!(Size::Char.pack(0x12), vec![0x12]);
assert_eq!(Size::Short.pack(0x1234), vec![0x12, 0x34]);
assert_eq!(Size::Long.pack(0x12345678), vec![0x12, 0x34, 0x56, 0x78]);Sourcepub fn unpack(&self, data: &[u8]) -> Result<u64, Error>
pub fn unpack(&self, data: &[u8]) -> Result<u64, Error>
Unpacks big-endian bytes into an integer value.
§Arguments
data- The bytes to unpack (must be exactlyself.size()bytes)
§Errors
Returns Error::Protocol if data.len() does not match self.size().
§Example
use stem_rs::client::datatype::Size;
assert_eq!(Size::Char.unpack(&[0x12]).unwrap(), 0x12);
assert_eq!(Size::Short.unpack(&[0x12, 0x34]).unwrap(), 0x1234);
// Wrong size returns an error
assert!(Size::Short.unpack(&[0x12]).is_err());Sourcepub fn pop<'a>(&self, data: &'a [u8]) -> Result<(u64, &'a [u8]), Error>
pub fn pop<'a>(&self, data: &'a [u8]) -> Result<(u64, &'a [u8]), Error>
Unpacks an integer from the start of a byte slice, returning the remainder.
This is useful for parsing sequential fields from a binary buffer.
§Arguments
data- The byte slice to read from (must have at leastself.size()bytes)
§Returns
A tuple of (value, remainder) where:
valueis the unpacked integerremainderis the unconsumed portion of the input
§Errors
Returns Error::Protocol if data.len() is less than self.size().
§Example
use stem_rs::client::datatype::Size;
let data = vec![0x00, 0x12, 0xFF, 0xFF];
let (value, rest) = Size::Short.pop(&data).unwrap();
assert_eq!(value, 18);
assert_eq!(rest, &[0xFF, 0xFF]);