From 809b71a17ee9af5760f1d3ad83bfe6244e7a0f41 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 29 Nov 2024 17:49:15 +0200 Subject: [PATCH] ManagedVecItem - ref for tuples --- .../basic-features/src/echo_managed.rs | 4 ++-- .../multi_value/multi_value_managed_vec.rs | 16 +++++++++++++++- .../types/managed/wrapped/managed_vec_item.rs | 10 ++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/contracts/feature-tests/basic-features/src/echo_managed.rs b/contracts/feature-tests/basic-features/src/echo_managed.rs index ffabfa5b16..98154b2d9f 100644 --- a/contracts/feature-tests/basic-features/src/echo_managed.rs +++ b/contracts/feature-tests/basic-features/src/echo_managed.rs @@ -159,11 +159,11 @@ pub trait EchoManagedTypes { MultiValue3>>, > { let mut result = MultiValueEncoded::new(); - for triple in address_number_pairs.into_iter() { + for triple in address_number_pairs { let (address, x, counted_list) = triple.into_tuple(); let mut counted_lazy = MultiValueEncodedCounted::new(); let v = counted_list.into_vec(); - for pair in &v { + for pair in v { counted_lazy.push(pair); } result.push((address, x, counted_lazy).into()); diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs index e342d88cf1..aa34600120 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs @@ -9,7 +9,7 @@ use crate::{ DecodeErrorHandler, EncodeErrorHandler, TopDecodeMulti, TopDecodeMultiInput, TopEncodeMulti, TopEncodeMultiOutput, Vec, }, - types::ManagedType, + types::{ManagedType, ManagedVecOwnedIterator}, }; use crate::types::{ManagedVec, ManagedVecItem, ManagedVecRefIterator}; @@ -144,6 +144,20 @@ where } } +impl IntoIterator for MultiValueManagedVec +where + M: ManagedTypeApi, + T: ManagedVecItem, +{ + type Item = T; + + type IntoIter = ManagedVecOwnedIterator; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + impl<'a, M, T> IntoIterator for &'a MultiValueManagedVec where M: ManagedTypeApi, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index d14ae0067f..62e62ad4dd 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -324,7 +324,7 @@ where { type PAYLOAD = <(T1, (T2, ())) as ManagedVecItemNestedTuple>::PAYLOAD; const SKIPS_RESERIALIZATION: bool = T1::SKIPS_RESERIALIZATION && T2::SKIPS_RESERIALIZATION; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -338,8 +338,7 @@ where } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: tuple of refs - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { @@ -362,7 +361,7 @@ where { type PAYLOAD = <(T1, (T2, (T3, ()))) as ManagedVecItemNestedTuple>::PAYLOAD; const SKIPS_RESERIALIZATION: bool = T1::SKIPS_RESERIALIZATION && T2::SKIPS_RESERIALIZATION; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -377,8 +376,7 @@ where } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: tuple of refs - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) {