Skip to main content

Encode

Trait Encode 

Source
pub trait Encode {
    type Encoder<'e>: Encoder
       where Self: 'e;

    // Required method
    fn encoder(&self) -> Self::Encoder<'_>;
}
Expand description

A Bitcoin object which can be consensus-encoded.

To encode something, use the Self::encoder method to obtain a Self::Encoder, which will behave like an iterator yielding byte slices.

§Examples

use bitcoin_consensus_encoding::{encoder_newtype, encode_to_vec, Encode, ArrayEncoder};

struct Foo([u8; 4]);

encoder_newtype! {
    pub struct FooEncoder<'e>(ArrayEncoder<4>);
}

impl Encode for Foo {
    type Encoder<'e> = FooEncoder<'e> where Self: 'e;

    fn encoder(&self) -> Self::Encoder<'_> {
        FooEncoder::new(ArrayEncoder::without_length_prefix(self.0))
    }
}

let foo = Foo([0xde, 0xad, 0xbe, 0xef]);
assert_eq!(encode_to_vec(&foo), vec![0xde, 0xad, 0xbe, 0xef]);

Required Associated Types§

Source

type Encoder<'e>: Encoder where Self: 'e

The encoder associated with this type. Conceptually, the encoder is like an iterator which yields byte slices.

Required Methods§

Source

fn encoder(&self) -> Self::Encoder<'_>

Constructs a “default encoder” for the type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§