From 1b5bb89ec06be7c8d5e4c391245363c230de6a51 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 ++++++++++++------- 1 file changed, 31 insertions(+), 17 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!() } } }