From 3fca1307112d2265bb615615b24d8198e02548d3 Mon Sep 17 00:00:00 2001 From: lorban Date: Fri, 21 Jun 2024 17:24:59 +0200 Subject: [PATCH] apply revision apply jbesraa revision https://github.com/stratum-mining/stratum/pull/949/files/388ad8998dca212d3fefc0f89af70ffe4d16a2d1 --- .../src/primitives/sequences/option.rs | 48 ++++++++++++------- .../src/primitives/sequences/seq0255.rs | 42 +++++++++------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs index 5e378f2474..46a6c959d7 100644 --- a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs +++ b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/option.rs @@ -4,6 +4,7 @@ use super::{ }; use crate::primitives::{FixedSize, GetSize}; use alloc::vec::Vec; +use debug_assert; use serde::{ ser::{self, SerializeSeq, SerializeTuple}, Deserialize, Deserializer, Serialize, @@ -69,7 +70,7 @@ impl<'s, T: Clone + Serialize + TryFromBSlice<'s>> Serialize for Sv2Option<'s, T } (None, Some(data)) => { if serializer.is_human_readable() { - let data_ = self.data.clone().unwrap(); + let data_ = data.clone(); let mut seq = serializer.serialize_seq(Some(data_.len()))?; for item in data_ { seq.serialize_element(&item)?; @@ -83,7 +84,13 @@ impl<'s, T: Clone + Serialize + TryFromBSlice<'s>> Serialize for Sv2Option<'s, T seq.end() } } - _ => panic!(), + _ => { + debug_assert!( + false, + "sv2option can never have boh fields of the same type" + ); + panic!() + } } } } @@ -231,22 +238,29 @@ impl<'a, T: Clone + FixedSize + Serialize + TryFromBSlice<'a>> GetSize for Sv2Op } impl<'s> Sv2Option<'s, U256<'s>> { pub fn into_static(self) -> Sv2Option<'static, U256<'static>> { - if let Some(inner) = self.data { - let inner = inner.clone(); - let data = inner.into_iter().map(|i| i.into_static()).collect(); - Sv2Option { - seq: None, - data: Some(data), + match (self.data, self.seq) { + (None, Some(seq)) => { + let data = seq.parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Sv2Option { + seq: None, + data: Some(data), + } } - } else { - // this is an already valid seq should be safe to call the unwraps. - // also this library shouldn't be used for priduction envs so is ok do thigs like this - // one - let data = self.seq.unwrap().parse().unwrap(); - let data = data.into_iter().map(|i| i.into_static()).collect(); - Sv2Option { - seq: None, - data: Some(data), + (Some(inner), None) => { + let inner = inner.clone(); + let data = inner.into_iter().map(|i| i.into_static()).collect(); + Sv2Option { + seq: None, + data: Some(data), + } + } + _ => { + debug_assert!( + false, + "sv2option can never have boh fields of the same type" + ); + panic!() } } } diff --git a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs index 0e40191979..2c679b792d 100644 --- a/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs +++ b/protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/seq0255.rs @@ -73,7 +73,7 @@ impl<'s, T: Clone + Serialize + TryFromBSlice<'s>> Serialize for Seq0255<'s, T> } (None, Some(data)) => { if serializer.is_human_readable() { - let data_ = self.data.clone().unwrap(); + let data_ = data.clone(); let mut seq = serializer.serialize_seq(Some(data_.len()))?; for item in data_ { seq.serialize_element(&item)?; @@ -235,22 +235,32 @@ impl<'a, T: Clone + FixedSize + Serialize + TryFromBSlice<'a>> GetSize for Seq02 } impl<'s> Seq0255<'s, U256<'s>> { pub fn into_static(self) -> Seq0255<'static, U256<'static>> { - if let Some(inner) = self.data { - let inner = inner.clone(); - let data = inner.into_iter().map(|i| i.into_static()).collect(); - Seq0255 { - seq: None, - data: Some(data), + match (self.data, self.seq) { + (None, Some(seq)) => { + // this is an already valid seq should be safe to call the unwraps. + // also this library shouldn't be used for priduction envs so is ok do thigs like this + // one + let data = seq.parse().unwrap(); + let data = data.into_iter().map(|i| i.into_static()).collect(); + Seq0255 { + seq: None, + data: Some(data), + } } - } else { - // this is an already valid seq should be safe to call the unwraps. - // also this library shouldn't be used for priduction envs so is ok do thigs like this - // one - let data = self.seq.unwrap().parse().unwrap(); - let data = data.into_iter().map(|i| i.into_static()).collect(); - Seq0255 { - seq: None, - data: Some(data), + (Some(inner), None) => { + let inner = inner.clone(); + let data = inner.into_iter().map(|i| i.into_static()).collect(); + Seq0255 { + seq: None, + data: Some(data), + } + } + _ => { + debug_assert!( + false, + "sv2option can never have boh fields of the same type" + ); + panic!() } } }