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

    // Required methods
    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;

    // Provided methods
    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§

source

type Error: From<SerError> + From<IOError> + Error

An associated error type

Required Methods§

source

fn serialized_length(&self) -> usize

Returns the byte-length of the serialized data structure.

source

fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>where R: Read, Self: Sized,

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());
source

fn write_to<W>( &self, writer: &mut W ) -> Result<usize, <Self as ByteFormat>::Error>where W: Write,

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§

source

fn read_seq_from<R>( reader: &mut R, mode: ReadSeqMode ) -> Result<Vec<Self>, Self::Error>where R: Read, Self: Sized,

Read a sequence of objects from the reader. The mode argument specifies how many objects to read.

source

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>,

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)

source

fn deserialize_hex(s: &str) -> Result<Self, Self::Error>where Self: Sized,

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

source

fn deserialize_base64(s: &str) -> Result<Self, Self::Error>where Self: Sized,

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

source

fn serialize_hex(&self) -> String

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

source

fn serialize_base64(&self) -> String

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

Implementations on Foreign Types§

source§

impl ByteFormat for u8

§

type Error = SerError

source§

fn serialized_length(&self) -> usize

source§

fn read_seq_from<R>(reader: &mut R, mode: ReadSeqMode) -> SerResult<Vec<u8>>where R: Read, Self: Sized,

source§

fn read_from<R>(reader: &mut R) -> SerResult<Self>where R: Read, Self: Sized,

source§

fn write_to<W>(&self, writer: &mut W) -> SerResult<usize>where W: Write,

Implementors§