-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge in changes of #59 and simplify Header::hash
Further reduce boilerplate code by leveraging generics
- Loading branch information
Showing
6 changed files
with
71 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// A wrapper whose sole purpose is to extend the original | ||
/// prost::Message trait with a few helper functions in order to reduce boiler-plate code | ||
/// (and without modifying the prost-amino dependency). | ||
pub struct AminoMessage<M>(M); // Note: we have to introduce a new type in order to add methods like bytes_vec or impl Into<Vec<u8>> | ||
|
||
impl<M> AminoMessage<M> | ||
where | ||
M: prost_amino::Message, | ||
{ | ||
/// Returns a wrapper for the given prost-amino message | ||
pub fn new(m: M) -> AminoMessage<M> { | ||
AminoMessage(m) | ||
} | ||
/// Directly amino encode a prost-amino message into a freshly created Vec<u8>. | ||
/// This can be useful when passing those bytes directly to a hasher, or, | ||
/// to reduce boiler plate code when working with the encoded bytes. | ||
/// | ||
/// Warning: Only use this method, if you are in control what will be encoded. | ||
/// If there is an encoding error, this method will panic. | ||
pub fn bytes_vec(m: M) -> Vec<u8> { | ||
AminoMessage(m).into() | ||
} | ||
} | ||
|
||
impl<M> Into<Vec<u8>> for AminoMessage<M> | ||
where | ||
M: prost_amino::Message, | ||
{ | ||
/// Convert a wrapped prost-amino message directly into a byte vector which contains the amino | ||
/// encoding. | ||
/// | ||
/// Warning: Only use this method, if you are in control what will be encoded. If there is an | ||
/// encoding error, this method will panic. | ||
fn into(self) -> Vec<u8> { | ||
let mut res = Vec::with_capacity(self.0.encoded_len()); | ||
self.0.encode(&mut res).unwrap(); | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters