Add a new `zcash_serialize_bytes` utility function (#2150)
This commit is contained in:
parent
adcd13bdd3
commit
c0326677a4
|
@ -40,6 +40,22 @@ impl<T: ZcashSerialize> ZcashSerialize for Vec<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serialize a byte vector as a compactsize number of items, then the items.
|
||||||
|
///
|
||||||
|
/// # Correctness
|
||||||
|
///
|
||||||
|
/// Most Zcash types have specific rules about serialization of `Vec<u8>`s.
|
||||||
|
/// Check the spec and consensus rules before using this function.
|
||||||
|
///
|
||||||
|
/// See `zcash_serialize_bytes_external_count` for more details, and usage information.
|
||||||
|
//
|
||||||
|
// we specifically want to serialize `Vec`s here, rather than generic slices
|
||||||
|
#[allow(clippy::ptr_arg)]
|
||||||
|
pub fn zcash_serialize_bytes<W: io::Write>(vec: &Vec<u8>, mut writer: W) -> Result<(), io::Error> {
|
||||||
|
writer.write_compactsize(vec.len() as u64)?;
|
||||||
|
zcash_serialize_bytes_external_count(vec, writer)
|
||||||
|
}
|
||||||
|
|
||||||
/// Serialize an `AtLeastOne` vector as a compactsize number of items, then the
|
/// Serialize an `AtLeastOne` vector as a compactsize number of items, then the
|
||||||
/// items. This is the most common format in Zcash.
|
/// items. This is the most common format in Zcash.
|
||||||
impl<T: ZcashSerialize> ZcashSerialize for AtLeastOne<T> {
|
impl<T: ZcashSerialize> ZcashSerialize for AtLeastOne<T> {
|
||||||
|
@ -101,10 +117,9 @@ pub fn zcash_serialize_bytes_external_count<W: io::Write>(
|
||||||
|
|
||||||
/// Write a Bitcoin-encoded UTF-8 `&str`.
|
/// Write a Bitcoin-encoded UTF-8 `&str`.
|
||||||
impl ZcashSerialize for &str {
|
impl ZcashSerialize for &str {
|
||||||
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
|
fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
|
||||||
let str_bytes = self.as_bytes();
|
let str_bytes = self.as_bytes().to_vec();
|
||||||
writer.write_compactsize(str_bytes.len() as u64)?;
|
zcash_serialize_bytes(&str_bytes, writer)
|
||||||
writer.write_all(str_bytes)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue