Size

Enum Size 

Source
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

VariantSizeRange
Char1 byte0 to 255
Short2 bytes0 to 65,535
Long4 bytes0 to 4,294,967,295
LongLong8 bytes0 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

Source

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);
Source

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]);
Source

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 exactly self.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());
Source

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 least self.size() bytes)
§Returns

A tuple of (value, remainder) where:

  • value is the unpacked integer
  • remainder is 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]);

Trait Implementations§

Source§

impl Clone for Size

Source§

fn clone(&self) -> Size

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Size

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Size

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Size

Source§

fn eq(&self, other: &Size) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Size

Source§

impl Eq for Size

Source§

impl StructuralPartialEq for Size

Auto Trait Implementations§

§

impl Freeze for Size

§

impl RefUnwindSafe for Size

§

impl Send for Size

§

impl Sync for Size

§

impl Unpin for Size

§

impl UnwindSafe for Size

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.