hopr_lib

Trait BytesEncodable

Source
pub trait BytesEncodable<const N: usize, E = GeneralError>: Into<[u8; N]> + for<'a> TryFrom<&'a [u8], Error = E> {
    const SIZE: usize = N;

    // Provided methods
    fn into_encoded(self) -> [u8; N] { ... }
    fn into_boxed(self) -> Box<[u8]> { ... }
}
Expand description

Represents a type that can be encoded to/decoded from a fixed sized byte array of size N. This requires processing and memory allocation to represent the type in binary encoding.

Differences between BytesEncodable and BytesRepresentable:

  • BytesRepresentable is already internally carrying the encoded representation of the type, so no additional encoding or allocation is required to represent the type as a byte array.
  • BytesEncodable requires additional transformation and allocation to represent the type as a fixed size byte array.
  • BytesEncodable is the strict superset of BytesRepresentable: meaning the former can be possibly implemented for a type that already implements the latter, but it is not possible vice versa.

Provided Associated Constants§

Source

const SIZE: usize = N

Size of the encoded byte array. Defaults to N and should not be overridden.

Provided Methods§

Source

fn into_encoded(self) -> [u8; N]

Convenience function to represent the A shorthand for let v: [u8; N] = self.into().

Source

fn into_boxed(self) -> Box<[u8]>

Convenience function to encode the type into a Box.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl BytesEncodable<hopr_crypto_packet::::por::{impl#4}::{constant#0}> for ProofOfRelayString

Source§

impl BytesEncodable<hopr_crypto_types::::vrf::{impl#6}::{constant#0}> for VrfParameters

Source§

impl BytesEncodable<hopr_transport_network::::messaging::{impl#4}::{constant#0}> for PingMessage

Implementors§

Source§

impl BytesEncodable<hopr_internal_types::::tickets::{impl#9}::{constant#0}> for Ticket