pub trait ByteFormat {
    type Error: From<SerError> + From<IOError> + Error;

    fn serialized_length(&self) -> usize;
    fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>
    where
        R: Read,
        Self: Sized
; fn write_to<W>(
        &self,
        writer: &mut W
    ) -> Result<usize, <Self as ByteFormat>::Error>
    where
        W: Write
; fn read_seq_from<R>(
        reader: &mut R,
        mode: ReadSeqMode
    ) -> Result<Vec<Self>, Self::Error>
    where
        R: Read,
        Self: Sized
, { ... } fn write_seq_to<'a, W, E, Iter, Item>(
        writer: &mut W,
        iter: Iter
    ) -> Result<usize, <Self as ByteFormat>::Error>
    where
        W: Write,
        E: Into<Self::Error> + From<SerError> + From<IOError> + Error,
        Item: 'a + ByteFormat<Error = E>,
        Iter: IntoIterator<Item = &'a Item>
, { ... } fn deserialize_hex(s: &str) -> Result<Self, Self::Error>
    where
        Self: Sized
, { ... } fn deserialize_base64(s: &str) -> Result<Self, Self::Error>
    where
        Self: Sized
, { ... } fn serialize_hex(&self) -> String { ... } fn serialize_base64(&self) -> String { ... } }
Expand description

A simple trait for deserializing from std::io::Read and serializing to std::io::Write.

ByteFormat is used extensively in Sighash calculation, txid calculations, and transaction serialization and deserialization.

Required Associated Types

An associated error type

Required Methods

Returns the byte-length of the serialized data structure.

Deserializes an instance of Self from a std::io::Read. The limit argument is used only when deserializing collections, and specifies a maximum number of instances of the underlying type to read.

use std::io::Read;
use coins_core::{hashes::Hash256Digest, ser::*};

let mut a = [0u8; 32];
let result = Hash256Digest::read_from(&mut a.as_ref()).unwrap();

assert_eq!(result, Hash256Digest::default());

Serializes self to a std::io::Write. Following Write trait conventions, its Ok type must be a usize denoting the number of bytes written.

use std::io::Write;
use coins_core::{hashes::Hash256Digest, ser::*};

let mut buf: Vec<u8> = vec![];
let written = Hash256Digest::default().write_to(&mut buf).unwrap();

assert_eq!(
   buf,
   vec![0u8; 32]
);

Provided Methods

Read a sequence of exactly limit objects from the reader.

Write a sequence of ByteFormat objects to a writer. The iter argument may be any object that implements IntoIterator<Item = &Item>. This means we can seamlessly use vectors, slices, etc.

use std::io::Write;
use coins_core::{hashes::Hash256Digest, ser::*};

let mut buf: Vec<u8> = vec![];
let mut digests = vec![Hash256Digest::default(), Hash256Digest::default()];

// Works with iterators
let written = Hash256Digest::write_seq_to(&mut buf, digests.iter()).expect("Write succesful");

assert_eq!(
   buf,
   vec![0u8; 64]
);

// And with vectors
let written = Hash256Digest::write_seq_to(&mut buf, &digests).expect("Write succesful");
assert_eq!(
   buf,
   vec![0u8; 128]
);

This should be invoked as Item::write_seq_to(writer, iter)

Decodes a hex string to a Vec<u8>, deserializes an instance of Self from that vector.

Serialize self to a base64 string, using standard RFC4648 non-url safe characters

Serializes self to a vector, returns the hex-encoded vector

Serialize self to a base64 string, using standard RFC4648 non-url safe characters

Implementations on Foreign Types

Implementors