pub struct CompactSizeEncoder { /* private fields */ }Expand description
Encoder for a compact size encoded integer.
Implementations§
Source§impl CompactSizeEncoder
impl CompactSizeEncoder
Sourcepub fn new(value: usize) -> Self
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.
Sourcepub fn new_u64(value: u64) -> Self
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.
Sourcepub const fn encoded_size(value: usize) -> usize
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
impl Clone for CompactSizeEncoder
Source§fn clone(&self) -> CompactSizeEncoder
fn clone(&self) -> CompactSizeEncoder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more