Trait coins_core::ser::ByteFormat
source · 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§
Required Methods§
sourcefn serialized_length(&self) -> usize
fn serialized_length(&self) -> usize
Returns the byte-length of the serialized data structure.
sourcefn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>where
R: Read,
Self: Sized,
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());
sourcefn write_to<W>(
&self,
writer: &mut W
) -> Result<usize, <Self as ByteFormat>::Error>where
W: Write,
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§
sourcefn read_seq_from<R>(
reader: &mut R,
mode: ReadSeqMode
) -> Result<Vec<Self>, Self::Error>where
R: Read,
Self: Sized,
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.
sourcefn 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 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)
sourcefn deserialize_hex(s: &str) -> Result<Self, Self::Error>where
Self: Sized,
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.
sourcefn deserialize_base64(s: &str) -> Result<Self, Self::Error>where
Self: Sized,
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
sourcefn serialize_hex(&self) -> String
fn serialize_hex(&self) -> String
Serializes self
to a vector, returns the hex-encoded vector
sourcefn serialize_base64(&self) -> String
fn serialize_base64(&self) -> String
Serialize self
to a base64 string, using standard RFC4648 non-url safe characters