From 0b373450dae1faac6390af3a17dc0957fe65a07d Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 21 Oct 2023 02:15:23 +0700 Subject: [PATCH] chore: implement try into outpoint --- dash/src/blockdata/transaction/outpoint.rs | 28 ++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/dash/src/blockdata/transaction/outpoint.rs b/dash/src/blockdata/transaction/outpoint.rs index 4134d060aa..0197cd10c0 100644 --- a/dash/src/blockdata/transaction/outpoint.rs +++ b/dash/src/blockdata/transaction/outpoint.rs @@ -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"), @@ -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> for OutPoint { + type Error = io::Error; + + fn try_into(self) -> Result, 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] @@ -336,4 +360,4 @@ mod tests { // assert_eq!(out_point.vout, 0); // assert_eq!(out_point.txid, tx.txid()); // } -} \ No newline at end of file +}