Skip to content

Commit

Permalink
chore: implement try into outpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Oct 20, 2023
1 parent d7beaf2 commit 0b37345
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions dash/src/blockdata/transaction/outpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ crate::serde_utils::serde_struct_human_string_impl!(OutPoint, "an OutPoint", txi

impl From<[u8; 36]> for OutPoint {
fn from(buffer: [u8; 36]) -> Self {
let (mut left, right) = buffer.split_at(32);
let (left, right) = buffer.split_at(32);
let index: [u8; 4] = right.try_into().expect("OutPoint vout is not 4 bytes");
Self {
txid: deserialize(left).expect("OutPoint txid is not 32 bytes"),
Expand Down Expand Up @@ -117,6 +117,30 @@ impl Decodable for OutPoint {
}
}

impl TryInto<[u8; 32]> for OutPoint {
type Error = io::Error;

fn try_into(self) -> Result<[u8; 32], Self::Error> {
let mut buffer = [0u8; 32];

self.consensus_encode(&mut buffer)?;

Ok(buffer.into_inner())
}
}

impl TryInto<Vec<u8>> for OutPoint {
type Error = io::Error;

fn try_into(self) -> Result<Vec<u8>, Self::Error> {
let mut buffer = Vec::new();

self.consensus_encode(&mut buffer)?;

Ok(buffer)
}
}

/// An error in parsing an OutPoint.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -336,4 +360,4 @@ mod tests {
// assert_eq!(out_point.vout, 0);
// assert_eq!(out_point.txid, tx.txid());
// }
}
}

0 comments on commit 0b37345

Please sign in to comment.