Expand description
§Rust Bitcoin Consensus Encoding
Traits and utilities for encoding and decoding Bitcoin data types in a consensus-consistent way, using a sans-I/O architecture.
Rather than reading from or writing to std::io::Read/std::io::Write traits directly, the
codec types work with byte slices. This keeps codec logic I/O-agnostic, so the same
implementation works in no_std environments, sync I/O, async I/O, and hash engines without
duplicating logic or surfacing I/O errors in non-I/O contexts (e.g. when hashing an encoding).
Consensus encoding is the canonical byte representation of Bitcoin data types used across the peer-to-peer network and transaction serialization. This crate only supports deterministic encoding and will never support types like floats whose encoding is non-deterministic or platform-dependent.
§Encoding
Types implement Encode to produce an Encoder, which yields encoded bytes in chunks
via Encoder::current_chunk and Encoder::advance. The caller drives the process by
pulling chunks until advance returns EncoderStatus::Finished.
§Decoding
Types implement Decode to produce a Decoder, which consumes bytes via
Decoder::push_bytes until it signals completion by returning Ok(DecoderStatus::Ready). The
caller then calls Decoder::end to obtain the decoded value.
Unlike encoding, decoding is fallible. Both push_bytes and end return Result. I/O errors
are handled by the caller, keeping the codec logic I/O-agnostic.
§Drivers
This crate provides free functions which drive codecs for common I/O interfaces. On the decoding side we provide:
decode_from_read: Decode from a stdlib buffered reader.decode_from_read_unbuffered: Decode from a stdlib unbuffered reader (4k buffer on stack).decode_from_read_unbuffered_with: As above with custom sized stack-allocated buffer.decode_from_slice: Decode from a byte slice (errors if slice is not completely consumed).decode_from_slice_unbounded: Slice can contain additional data after decoding completes.
And on the encoding side we provide:
encode_to_writer: Encode to a stdlib writer.drain_to_writer: Drain an encoder to a stdlib writer.encode_to_vec: Encode to the heap.drain_to_vec: Drain an encoder to the heap.
§Feature Flags
std- Enables std lib I/O driver functions andstd::error::Errorimpls (impliesalloc).alloc- Enablesencode_to_vec,Vec-based decoders, and allocation-based helpers.
Re-exports§
pub use self::error::LengthPrefixExceedsMaxError;pub use self::error::ReadError;pub use self::error::ByteVecDecoderError;pub use self::error::VecDecoderError;pub use self::error::CompactSizeDecoderError;pub use self::error::DecodeError;pub use self::error::Decoder2Error;pub use self::error::Decoder3Error;pub use self::error::Decoder4Error;pub use self::error::Decoder6Error;pub use self::error::UnconsumedError;pub use self::error::UnexpectedEofError;
Modules§
- error
- Error types for the whole crate.
Macros§
- encoder_
newtype - Implements a newtype around an encoder.
- encoder_
newtype_ exact - Implements a newtype around an exact-size encoder.
Structs§
- Array
Decoder - A decoder that expects exactly N bytes and returns them as an array.
- Array
Encoder - An encoder for a single array.
- Array
RefEncoder - An encoder for a reference to an array.
- Byte
VecDecoder - A decoder that decodes a byte vector.
- Bytes
Encoder - An encoder for a single byte slice.
- Compact
Size Decoder - Decodes a compact size encoded integer as a length prefix.
- Compact
Size Encoder - Encoder for a compact size encoded integer.
- Compact
Size U64Decoder - Decodes a compact size encoded integer as a raw
u64. - Decoder2
- A decoder which wraps two inner decoders and returns the output of both.
- Decoder3
- A decoder which decodes three objects, one after the other.
- Decoder4
- A decoder which decodes four objects, one after the other.
- Decoder6
- A decoder which decodes six objects, one after the other.
- Encoder2
- An encoder which encodes two objects, one after the other.
- Encoder3
- An encoder which encodes three objects, one after the other.
- Encoder4
- An encoder which encodes four objects, one after the other.
- Encoder6
- An encoder which encodes six objects, one after the other.
- Encoder
Byte Iter - Yields bytes from any
Encoderinstance. - Slice
Encoder - An encoder for a list of encodable types.
- VecDecoder
- A decoder that decodes a vector of
Ts.
Enums§
- Decoder
Status - Indicates whether a decoder needs more data or is ready to finalize.
- Encoder
Status - Indicates whether the encoder still has bytes available or it is finished.
Traits§
- Decode
- A Bitcoin object which can be consensus-decoded using a push decoder.
- Decoder
- A push decoder for a consensus-decodable object.
- Encode
- A Bitcoin object which can be consensus-encoded.
- Encoder
- An encoder for a consensus-encodable object.
- Exact
Size Encoder - An encoder with a known size.
Functions§
- check_
decode - Checks that the given bytes decode to the expected value, panicking if they don’t.
- check_
decoder - Checks that the given
decoderproduces the expected value, panicking if it doesn’t. - check_
encode - Checks that the given
valueencodes toexpected, panicking if it doesn’t. - check_
encoder - Checks that the given
encoderyieldsexpected, panicking if it doesn’t. - decode_
from_ read - Decodes an object from a buffered reader.
- decode_
from_ read_ unbuffered - Decodes an object from an unbuffered reader using a fixed-size buffer.
- decode_
from_ read_ unbuffered_ with - Decodes an object from an unbuffered reader using a custom-sized buffer.
- decode_
from_ slice - Decodes an object from a byte slice.
- decode_
from_ slice_ unbounded - Decodes an object from an unbounded byte slice.
- drain_
to_ vec - Drains the output of an
Encoderinto a vector. - drain_
to_ writer - Drains the output of an
Encoderto a standard I/O writer. - encode_
to_ vec - Encodes an object into a vector.
- encode_
to_ writer - Encodes an object to a standard I/O writer.