Skip to main content

CompactSizeEncoder

Struct CompactSizeEncoder 

Source
pub struct CompactSizeEncoder { /* private fields */ }
Expand description

Encoder for a compact size encoded integer.

Implementations§

Source§

impl CompactSizeEncoder

Source

pub fn new(value: usize) -> Self

Constructs a new CompactSizeEncoder for a length prefix.

The usize type is the natural Rust type for lengths and collection sizes, which is the dominant use case for compact size encoding in the Bitcoin protocol. Prefer this constructor whenever you are encoding the length of a collection or a byte slice.

Compact size encodings are defined only over the u64 range. On exotic platforms where usize is wider than 64 bits the value will be saturated to u64::MAX, but in practice any in-memory length that could actually be passed here is well within the u64 range.

If you need to encode an arbitrary u64 integer that is not a length prefix, use Self::new_u64 instead.

Source

pub fn new_u64(value: u64) -> Self

Constructs a new CompactSizeEncoder for an arbitrary u64 integer.

Prefer Self::new unless you are encoding a non-length integer.

A small number of fields in the Bitcoin protocol are compact-size-encoded integers that are not collection lengths (e.g. service flags). Use this constructor for those cases, where the natural type of the value is u64 rather than usize.

Source

pub const fn encoded_size(value: usize) -> usize

Returns the number of bytes used to encode this CompactSize value.

§Returns
  • 1 for 0..=0xFC
  • 3 for 0xFD..=(2^16-1)
  • 5 for 0x10000..=(2^32-1)
  • 9 otherwise.

Trait Implementations§

Source§

impl Clone for CompactSizeEncoder

Source§

fn clone(&self) -> CompactSizeEncoder

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 CompactSizeEncoder

Source§

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

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

impl Encoder for CompactSizeEncoder

Source§

fn current_chunk(&self) -> &[u8]

Yields the current encoded byteslice. Read more
Source§

fn advance(&mut self) -> bool

Moves the encoder to its next state. Read more
Source§

impl ExactSizeEncoder for CompactSizeEncoder

Source§

fn len(&self) -> usize

The number of bytes remaining that the encoder will yield.
Source§

fn is_empty(&self) -> bool

Returns whether the encoder would yield an empty response.

Auto Trait Implementations§

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> 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.