From aa254105616bfd69543366e055a852e005dd5a59 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Wed, 17 Apr 2024 13:46:06 +0200 Subject: [PATCH] Remove implementation of SiaEncodable for Address and Algorithm --- src/address.rs | 15 +-------------- src/specifier.rs | 7 ++++--- src/transactions.rs | 11 ++++++----- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/address.rs b/src/address.rs index 8aa43cb..c787e53 100644 --- a/src/address.rs +++ b/src/address.rs @@ -126,12 +126,6 @@ impl fmt::Display for Address { } } -impl SiaEncodable for Address { - fn encode(&self, buf: &mut Vec) { - buf.extend_from_slice(&encoding::to_bytes(&self).unwrap()); - } -} - #[derive(Debug, PartialEq, Clone, Copy)] pub enum Algorithm { ED25519, @@ -150,7 +144,6 @@ impl Serialize for Algorithm { where S: serde::Serializer, { - let spec: Specifier = match self { Algorithm::ED25519 => Specifier::from("ed25519"), }; @@ -158,12 +151,6 @@ impl Serialize for Algorithm { } } -impl SiaEncodable for Algorithm { - fn encode(&self, buf: &mut Vec) { - buf.extend_from_slice(&encoding::to_bytes(&self).unwrap()); - } -} - /// A generic public key that can be used to spend a utxo or revise a file /// contract /// @@ -214,7 +201,7 @@ impl fmt::Display for UnlockKey { impl SiaEncodable for UnlockKey { fn encode(&self, buf: &mut Vec) { - self.algorithm.encode(buf); + encoding::to_writer(buf, &self.algorithm).unwrap(); buf.extend_from_slice(&32_u64.to_le_bytes()); buf.extend_from_slice(self.public_key.as_ref()); } diff --git a/src/specifier.rs b/src/specifier.rs index 81c1ade..829d204 100644 --- a/src/specifier.rs +++ b/src/specifier.rs @@ -5,14 +5,15 @@ const SPECIFIER_SIZE: usize = 16; #[derive(Debug, PartialEq, Serialize)] pub struct Specifier([u8; SPECIFIER_SIZE]); +// implement the `From` trait for the `Specifier` struct to allow for creating a +// Specifier from any type that can be converted to a slice of bytes such as +// 'String', '&str' or byte arrays. impl> From for Specifier { fn from(src: T) -> Self { let src = src.as_ref(); assert!(src.len() <= SPECIFIER_SIZE, "specifier too long"); let mut spec = Specifier([0; SPECIFIER_SIZE]); - for (src, dst) in src.iter().zip(spec.0.iter_mut()) { - *dst = *src; - } + spec.0[..src.len()].copy_from_slice(src); spec } } diff --git a/src/transactions.rs b/src/transactions.rs index 45f079e..1852417 100644 --- a/src/transactions.rs +++ b/src/transactions.rs @@ -1,5 +1,6 @@ use core::fmt; +use crate::encoding; use crate::Currency; use crate::Signature; use crate::{Address, UnlockConditions}; @@ -75,7 +76,7 @@ pub struct SiacoinOutput { impl SiaEncodable for SiacoinOutput { fn encode(&self, buf: &mut Vec) { self.value.encode(buf); - self.address.encode(buf); + encoding::to_writer(buf, &self.address).unwrap(); } } @@ -126,7 +127,7 @@ impl SiaEncodable for SiafundInput { fn encode(&self, buf: &mut Vec) { self.parent_id.encode(buf); self.unlock_conditions.encode(buf); - self.claim_address.encode(buf); + encoding::to_writer(buf, &self.claim_address).unwrap(); } } @@ -140,7 +141,7 @@ pub struct SiafundOutput { impl SiaEncodable for SiafundOutput { fn encode(&self, buf: &mut Vec) { self.value.encode(buf); - self.address.encode(buf); + encoding::to_writer(buf, &self.address).unwrap(); self.claim_start.encode(buf); } } @@ -203,7 +204,7 @@ impl SiaEncodable for FileContract { for output in &self.missed_proof_outputs { output.encode(buf); } - self.unlock_hash.encode(buf); + encoding::to_writer(buf, &self.unlock_hash).unwrap(); buf.extend_from_slice(&self.revision_number.to_le_bytes()); } } @@ -239,7 +240,7 @@ impl SiaEncodable for FileContractRevision { for output in &self.missed_proof_outputs { output.encode(buf); } - self.unlock_hash.encode(buf); + encoding::to_writer(buf, &self.unlock_hash).unwrap(); } }