Struct avalanche_types::packer::Packer
source · pub struct Packer { /* private fields */ }
Expand description
Packer packs and unpacks the underlying bytes array. ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer ref. https://doc.rust-lang.org/1.7.0/book/mutability.html ref. https://doc.rust-lang.org/std/cell/struct.Cell.html
Implementations§
source§impl Packer
impl Packer
sourcepub fn pack_vertex(&self, vtx: &mut Vertex) -> Result<()>
pub fn pack_vertex(&self, vtx: &mut Vertex) -> Result<()>
Encodes vertex fields with codec version and packer.
ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex#Build
sourcepub fn unpack_vertex(&self) -> Result<Vertex>
pub fn unpack_vertex(&self) -> Result<Vertex>
Unpacks the vertex.
ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex#Build
source§impl Packer
impl Packer
sourcepub fn pack_ip(&self, ip_addr: IpAddr, port: u16) -> Result<()>
pub fn pack_ip(&self, ip_addr: IpAddr, port: u16) -> Result<()>
Writes the “IP” value at the offset in 16-byte representation and increments the offset afterwards. ref. “avalanchego/utils/wrappers.Packer.PackIP” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackIP ref. https://doc.rust-lang.org/std/net/enum.IpAddr.html ref. https://doc.rust-lang.org/std/net/struct.Ipv4Addr.html ref. https://doc.rust-lang.org/std/net/struct.Ipv6Addr.html
sourcepub fn unpack_ip(&self) -> Result<(IpAddr, u16)>
pub fn unpack_ip(&self) -> Result<(IpAddr, u16)>
Unpacks the “IP” in the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackIP” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackIP
sourcepub fn pack_ips(&self, ips: &[(IpAddr, u16)]) -> Result<()>
pub fn pack_ips(&self, ips: &[(IpAddr, u16)]) -> Result<()>
Writes the list of “IP” values at the offset in 16-byte representation and increments the offset afterwards. ref. “avalanchego/utils/wrappers.Packer.PackIPs” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackIPs
sourcepub fn unpack_ips(&self) -> Result<Vec<(IpAddr, u16)>>
pub fn unpack_ips(&self) -> Result<Vec<(IpAddr, u16)>>
Unpacks the list of “IP“s in the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackIPs” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackIPs
source§impl Packer
impl Packer
pub fn new(max_size: usize, initial_cap: usize) -> Self
sourcepub fn new_with_header(max_size: usize, initial_cap: usize) -> Self
pub fn new_with_header(max_size: usize, initial_cap: usize) -> Self
Creates a new Packer with 32-bit message length header.
sourcepub fn load_bytes_for_pack(max_size: usize, b: &[u8]) -> Self
pub fn load_bytes_for_pack(max_size: usize, b: &[u8]) -> Self
Create a new packer from the existing bytes. Resets the offset to the end of the existing bytes.
sourcepub fn load_bytes_for_unpack(max_size: usize, b: &[u8]) -> Self
pub fn load_bytes_for_unpack(max_size: usize, b: &[u8]) -> Self
Create a new packer from the existing bytes. Resets the offset to the beginning of the existing bytes.
sourcepub fn take_bytes(&self) -> Bytes
pub fn take_bytes(&self) -> Bytes
Returns the current bytes array as an immutable bytes array. If the packer header is set to “true”, the first 4-byte represents the message length in the big-endian order. The returned bytes length will be 4-byte + message.
Be cautious! Once bytes are taken out, the “bytes” field is set to default (empty). To continue to write to bytes, remember to put it back with “set_bytes” because “bytes.take” leaves the field as “Default::default()”. TODO: make sure this does shallow copy!
sourcepub fn set_bytes(&self, b: &[u8])
pub fn set_bytes(&self, b: &[u8])
Sets the current bytes array as an immutable bytes array. Useful to reuse packer after calling “take_bytes”, which makes the “bytes” field default (empty).
sourcepub fn get_offset(&self) -> usize
pub fn get_offset(&self) -> usize
Returns the “offset” value.
sourcepub fn expand(&self, n: usize) -> Result<()>
pub fn expand(&self, n: usize) -> Result<()>
Ensures the remaining capacity of the bytes array so it can write “n” bytes to the array. ref. “avalanchego/utils/wrappers.Packer.Expand” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.Expand
sourcepub fn pack_byte(&self, v: u8) -> Result<()>
pub fn pack_byte(&self, v: u8) -> Result<()>
Writes the “u8” value at the offset and increments the offset afterwards. ref. “avalanchego/utils/wrappers.Packer.PackByte” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackByte
sourcepub fn unpack_byte(&self) -> Result<u8>
pub fn unpack_byte(&self) -> Result<u8>
Unpacks the byte in the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackByte” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackByte
sourcepub fn pack_u16(&self, v: u16) -> Result<()>
pub fn pack_u16(&self, v: u16) -> Result<()>
Writes the “u16” value at the offset and increments the offset afterwards. ref. “avalanchego/utils/wrappers.Packer.PackShort” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackShort
sourcepub fn unpack_u16(&self) -> Result<u16>
pub fn unpack_u16(&self) -> Result<u16>
Unpacks the u16 from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackShort” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackShort
sourcepub fn pack_u32(&self, v: u32) -> Result<()>
pub fn pack_u32(&self, v: u32) -> Result<()>
Writes the “u32” value at the offset and increments the offset afterwards. This is also used for encoding the type IDs from codec. ref. “avalanchego/utils/wrappers.Packer.PackInt” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackInt
sourcepub fn unpack_u32(&self) -> Result<u32>
pub fn unpack_u32(&self) -> Result<u32>
Unpacks the u32 from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackInt” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackInt
sourcepub fn pack_u64(&self, v: u64) -> Result<()>
pub fn pack_u64(&self, v: u64) -> Result<()>
Writes the “u64” value at the offset and increments the offset afterwards. ref. “avalanchego/utils/wrappers.Packer.PackLong” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackLong
sourcepub fn unpack_u64(&self) -> Result<u64>
pub fn unpack_u64(&self) -> Result<u64>
Unpacks the u64 from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackLong” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackLong
sourcepub fn pack_bool(&self, v: bool) -> Result<()>
pub fn pack_bool(&self, v: bool) -> Result<()>
Writes the “bool” value at the offset and increments the offset afterwards. ref. “avalanchego/utils/wrappers.Packer.PackBool” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackBool
sourcepub fn unpack_bool(&self) -> Result<bool>
pub fn unpack_bool(&self) -> Result<bool>
Unpacks the bool in the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackBool” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackBool
sourcepub fn pack_bytes(&self, v: &[u8]) -> Result<()>
pub fn pack_bytes(&self, v: &[u8]) -> Result<()>
Writes the “u8” fixed-size array from the offset and increments the offset as much. ref. “avalanchego/utils/wrappers.Packer.PackFixedBytes” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackFixedBytes
sourcepub fn unpack_bytes(&self, n: usize) -> Result<Vec<u8>>
pub fn unpack_bytes(&self, n: usize) -> Result<Vec<u8>>
Unpacks the “u8” fixed-size array from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackFixedBytes” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackFixedBytes
sourcepub fn pack_bytes_with_header(&self, v: &[u8]) -> Result<()>
pub fn pack_bytes_with_header(&self, v: &[u8]) -> Result<()>
Writes the “u8” slice from the offset and increments the offset as much. The first 4-byte is used for encoding length header. ref. “avalanchego/utils/wrappers.Packer.PackBytes” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackBytes
sourcepub fn unpack_bytes_with_header(&self) -> Result<Vec<u8>>
pub fn unpack_bytes_with_header(&self) -> Result<Vec<u8>>
Unpacks the “u8” slice from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackBytes” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackBytes
sourcepub fn pack_2d_bytes(&self, v: Vec<Vec<u8>>) -> Result<()>
pub fn pack_2d_bytes(&self, v: Vec<Vec<u8>>) -> Result<()>
Writes the two-dimensional “u8” slice from the offset and increments the offset as much. ref. “avalanchego/utils/wrappers.Packer.PackFixedByteSlices” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackFixedByteSlices
sourcepub fn unpack_2d_bytes(&self, n: usize) -> Result<Vec<Vec<u8>>>
pub fn unpack_2d_bytes(&self, n: usize) -> Result<Vec<Vec<u8>>>
Unpacks the two-dimensional “u8” slice from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.UnpackFixedByteSlices” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackFixedByteSlices
sourcepub fn pack_2d_bytes_with_header(&self, v: Vec<Vec<u8>>) -> Result<()>
pub fn pack_2d_bytes_with_header(&self, v: Vec<Vec<u8>>) -> Result<()>
Writes the two-dimensional “u8” slice from the offset and increments the offset as much. ref. “avalanchego/utils/wrappers.Packer.Pack2DByteSlice” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.Pack2DByteSlice
sourcepub fn unpack_2d_bytes_with_header(&self) -> Result<Vec<Vec<u8>>>
pub fn unpack_2d_bytes_with_header(&self) -> Result<Vec<Vec<u8>>>
Unpacks the two-dimensional “u8” slice from the “offset” position, and advances the cursor and offset. ref. “avalanchego/utils/wrappers.Packer.Unpack2DByteSlice” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.Unpack2DByteSlice
sourcepub fn pack_str(&self, v: &str) -> Result<()>
pub fn pack_str(&self, v: &str) -> Result<()>
Writes str from the offset and increments the offset as much. ref. “avalanchego/utils/wrappers.Packer.PackStr” ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.PackStr
sourcepub fn unpack_str(&self) -> Result<String>
pub fn unpack_str(&self) -> Result<String>
Unpacks str from the offset. ref. “avalanchego/utils/wrappers.Packer.UnpackStr”
TODO: Go “UnpackStr” does deep-copy of bytes to “string” cast Can we bypass deep-copy by passing around bytes? ref. https://github.com/golang/go/issues/25484
ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/utils/wrappers#Packer.UnpackStr
Auto Trait Implementations§
impl !RefUnwindSafe for Packer
impl Send for Packer
impl !Sync for Packer
impl Unpin for Packer
impl UnwindSafe for Packer
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request