Trait handshakes::prelude::ByteFormat [−][src]
pub trait ByteFormat { type Error: From<SerError> + From<Error> + Error; fn serialized_length(&self) -> usize; fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>
where
R: Read; fn write_to<W>(&self, writer: &mut W) -> Result<usize, Self::Error>
where
W: Write; fn read_seq_from<R>(
reader: &mut R,
mode: ReadSeqMode
) -> Result<Vec<Self, Global>, Self::Error>
where
R: Read, { ... } fn write_seq_to<'a, W, E, Iter, Item>(
writer: &mut W,
iter: Iter
) -> Result<usize, Self::Error>
where
Item: 'a + ByteFormat<Error = E>,
W: Write,
Iter: IntoIterator<Item = &'a Item>,
E: Into<Self::Error> + From<SerError> + From<Error> + Error, { ... } fn deserialize_hex(s: &str) -> Result<Self, Self::Error> { ... } fn deserialize_base64(s: &str) -> Result<Self, Self::Error> { ... } 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.
Associated Types
Required methods
fn serialized_length(&self) -> usize
fn serialized_length(&self) -> usize
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
fn read_seq_from<R>(
reader: &mut R,
mode: ReadSeqMode
) -> Result<Vec<Self, Global>, Self::Error> where
R: Read,
fn read_seq_from<R>(
reader: &mut R,
mode: ReadSeqMode
) -> Result<Vec<Self, Global>, Self::Error> where
R: Read,
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)
fn deserialize_hex(s: &str) -> Result<Self, Self::Error>
fn deserialize_hex(s: &str) -> Result<Self, Self::Error>
Decodes a hex string to a Vec<u8>
, deserializes an instance of Self
from that vector.
fn deserialize_base64(s: &str) -> Result<Self, Self::Error>
fn deserialize_base64(s: &str) -> Result<Self, Self::Error>
Serialize self
to a base64 string, using standard RFC4648 non-url safe characters
fn serialize_hex(&self) -> String
fn serialize_hex(&self) -> String
Serializes self
to a vector, returns the hex-encoded vector
fn serialize_base64(&self) -> String
fn serialize_base64(&self) -> String
Serialize self
to a base64 string, using standard RFC4648 non-url safe characters
Implementations on Foreign Types
type Error = Bip32Error
pub fn read_from<T>(
_reader: &mut T
) -> Result<KeyDerivation, <KeyDerivation as ByteFormat>::Error> where
T: Read,
KeyDerivation: Sized,
pub fn write_to<T>(
&self,
writer: &mut T
) -> Result<usize, <KeyDerivation as ByteFormat>::Error> where
T: Write,
type Error = Bip32Error
pub fn read_from<R>(
reader: &mut R
) -> Result<KeyFingerprint, <KeyFingerprint as ByteFormat>::Error> where
R: Read,
KeyFingerprint: Sized,
pub fn write_to<W>(
&self,
writer: &mut W
) -> Result<usize, <KeyFingerprint as ByteFormat>::Error> where
W: Write,