From c93f295cd5f8750b4a732875cb0627d541cef78e Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 27 Jun 2023 13:25:38 +0100 Subject: [PATCH 1/2] feat(storage): do not require `Default` for `derive(Compact)` --- crates/storage/codecs/derive/src/compact/enums.rs | 4 ++-- crates/storage/codecs/derive/src/compact/structs.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/storage/codecs/derive/src/compact/enums.rs b/crates/storage/codecs/derive/src/compact/enums.rs index 978d98d86c56..a408c59305b3 100644 --- a/crates/storage/codecs/derive/src/compact/enums.rs +++ b/crates/storage/codecs/derive/src/compact/enums.rs @@ -68,8 +68,8 @@ impl<'a> EnumHandler<'a> { // Unamed type self.enum_lines.push(quote! { #current_variant_index => { - let mut inner = #field_type::default(); - (inner, buf) = #field_type::#from_compact_ident(buf, buf.len()); + let (inner, new_buf) = #field_type::#from_compact_ident(buf, buf.len()); + buf = new_buf; #ident::#variant_name(inner) } }); diff --git a/crates/storage/codecs/derive/src/compact/structs.rs b/crates/storage/codecs/derive/src/compact/structs.rs index 9bf56950ed8d..aec1d7c66e44 100644 --- a/crates/storage/codecs/derive/src/compact/structs.rs +++ b/crates/storage/codecs/derive/src/compact/structs.rs @@ -137,21 +137,21 @@ impl<'a> StructHandler<'a> { }) } else { let ident_type = format_ident!("{ftype}"); - self.lines.push(quote! { - let mut #name = #ident_type::default(); - }); if !is_flag_type(ftype) { // It's a type that handles its own length requirements. (h256, Custom, ...) self.lines.push(quote! { - (#name, buf) = #ident_type::#from_compact_ident(buf, buf.len()); + let (#name, new_buf) = #ident_type::#from_compact_ident(buf, buf.len()); }) } else if *is_compact { self.lines.push(quote! { - (#name, buf) = #ident_type::#from_compact_ident(buf, flags.#len() as usize); + let (#name, new_buf) = #ident_type::#from_compact_ident(buf, flags.#len() as usize); }); } else { todo!() } + self.lines.push(quote! { + buf = new_buf; + }); } } } From f7916a5961642272d8706954eec80bf1f99520d8 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 27 Jun 2023 13:30:19 +0100 Subject: [PATCH 2/2] fix test --- .../storage/codecs/derive/src/compact/mod.rs | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/crates/storage/codecs/derive/src/compact/mod.rs b/crates/storage/codecs/derive/src/compact/mod.rs index 3f67fb721bb4..e8daacfa2358 100644 --- a/crates/storage/codecs/derive/src/compact/mod.rs +++ b/crates/storage/codecs/derive/src/compact/mod.rs @@ -277,25 +277,24 @@ mod tests { } fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) { let (flags, mut buf) = TestStructFlags::from(buf); - let mut f_u64 = u64::default(); - (f_u64, buf) = u64::from_compact(buf, flags.f_u64_len() as usize); - let mut f_u256 = U256::default(); - (f_u256, buf) = U256::from_compact(buf, flags.f_u256_len() as usize); - let mut f_bool_t = bool::default(); - (f_bool_t, buf) = bool::from_compact(buf, flags.f_bool_t_len() as usize); - let mut f_bool_f = bool::default(); - (f_bool_f, buf) = bool::from_compact(buf, flags.f_bool_f_len() as usize); - let mut f_option_none = Option::default(); - (f_option_none, buf) = Option::from_compact(buf, flags.f_option_none_len() as usize); - let mut f_option_some = Option::default(); - (f_option_some, buf) = Option::specialized_from_compact(buf, flags.f_option_some_len() as usize); - let mut f_option_some_u64 = Option::default(); - (f_option_some_u64, buf) = - Option::from_compact(buf, flags.f_option_some_u64_len() as usize); - let mut f_vec_empty = Vec::default(); - (f_vec_empty, buf) = Vec::from_compact(buf, buf.len()); - let mut f_vec_some = Vec::default(); - (f_vec_some, buf) = Vec::specialized_from_compact(buf, buf.len()); + let (f_u64, new_buf) = u64::from_compact(buf, flags.f_u64_len() as usize); + buf = new_buf; + let (f_u256, new_buf) = U256::from_compact(buf, flags.f_u256_len() as usize); + buf = new_buf; + let (f_bool_t, new_buf) = bool::from_compact(buf, flags.f_bool_t_len() as usize); + buf = new_buf; + let (f_bool_f, new_buf) = bool::from_compact(buf, flags.f_bool_f_len() as usize); + buf = new_buf; + let (f_option_none, new_buf) = Option::from_compact(buf, flags.f_option_none_len() as usize); + buf = new_buf; + let (f_option_some, new_buf) = Option::specialized_from_compact(buf, flags.f_option_some_len() as usize); + buf = new_buf; + let (f_option_some_u64, new_buf) = Option::from_compact(buf, flags.f_option_some_u64_len() as usize); + buf = new_buf; + let (f_vec_empty, new_buf) = Vec::from_compact(buf, buf.len()); + buf = new_buf; + let (f_vec_some, new_buf) = Vec::specialized_from_compact(buf, buf.len()); + buf = new_buf; let obj = TestStruct { f_u64: f_u64, f_u256: f_u256,