From 4c0df3134efa73ae7087052ddeb1b266e9fdb24a Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 18 Nov 2022 10:13:54 +0200 Subject: [PATCH 1/2] cargo fmt --- .../src/impl_for_types/impl_phantom.rs | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/elrond-codec/src/impl_for_types/impl_phantom.rs b/elrond-codec/src/impl_for_types/impl_phantom.rs index 5b23aef4dc..46438c5ec0 100644 --- a/elrond-codec/src/impl_for_types/impl_phantom.rs +++ b/elrond-codec/src/impl_for_types/impl_phantom.rs @@ -9,7 +9,7 @@ use crate::{ /// /// Note: the unit type `()` would have naturally fit this role, but we decided to make the unit type multi-value only. -impl TopEncode for PhantomData { +impl TopEncode for PhantomData { #[inline] fn top_encode_or_handle_err(&self, output: O, _h: H) -> Result<(), H::HandledErr> where @@ -21,7 +21,7 @@ impl TopEncode for PhantomData { } } -impl TopDecode for PhantomData { +impl TopDecode for PhantomData { fn top_decode_or_handle_err(input: I, h: H) -> Result where I: TopDecodeInput, @@ -35,7 +35,7 @@ impl TopDecode for PhantomData { } } -impl NestedEncode for PhantomData { +impl NestedEncode for PhantomData { #[inline] fn dep_encode_or_handle_err(&self, _dest: &mut O, _h: H) -> Result<(), H::HandledErr> where @@ -46,7 +46,7 @@ impl NestedEncode for PhantomData { } } -impl NestedDecode for PhantomData { +impl NestedDecode for PhantomData { #[inline] fn dep_decode_or_handle_err(_input: &mut I, _h: H) -> Result where @@ -57,13 +57,12 @@ impl NestedDecode for PhantomData { } } - #[cfg(test)] pub mod tests { use crate as elrond_codec; - use elrond_codec_derive::{TopDecode, TopEncode, NestedDecode, NestedEncode}; use crate::test_util::{check_dep_encode_decode, check_top_encode_decode}; use core::marker::PhantomData; + use elrond_codec_derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}; #[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, PartialEq, Eq, Clone, Debug)] pub struct TestStructWithPhantom { @@ -84,11 +83,25 @@ pub mod tests { #[test] fn test_dep_struc() { - check_dep_encode_decode(TestStructWithPhantom::{x: 42, y:42, _phantom: PhantomData::}, &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42]); + check_dep_encode_decode( + TestStructWithPhantom:: { + x: 42, + y: 42, + _phantom: PhantomData::, + }, + &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42], + ); } #[test] fn test_top_struc() { - check_top_encode_decode(TestStructWithPhantom::{x: 42, y:42, _phantom: PhantomData::}, &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42]); + check_top_encode_decode( + TestStructWithPhantom:: { + x: 42, + y: 42, + _phantom: PhantomData::, + }, + &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42], + ); } } From e9dfab5ed8f2bbe32ddc1112032335817ac7e312 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 18 Nov 2022 10:17:30 +0200 Subject: [PATCH 2/2] IntoMultiValue trait, imports macro fix --- .../examples/nft-minter/src/nft_module.rs | 2 -- .../composability/vault/src/vault.rs | 7 +----- elrond-codec/src/multi/into_multi_value.rs | 10 +++++++++ elrond-codec/src/multi/mod.rs | 2 ++ .../bonding_curve/utils/owner_endpoints.rs | 1 - .../src/bonding_curve/utils/structs.rs | 1 - .../src/bonding_curve/utils/user_endpoints.rs | 1 - elrond-wasm-modules/src/esdt.rs | 2 -- elrond-wasm-modules/src/ongoing_operation.rs | 2 -- elrond-wasm/src/macros.rs | 6 ++++- .../managed/wrapped/esdt_token_payment.rs | 16 +++++++++----- .../src/types/managed/wrapped/managed_vec.rs | 22 ++++++++++++++++--- 12 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 elrond-codec/src/multi/into_multi_value.rs diff --git a/contracts/examples/nft-minter/src/nft_module.rs b/contracts/examples/nft-minter/src/nft_module.rs index f05f1626b0..8083f4f0fd 100644 --- a/contracts/examples/nft-minter/src/nft_module.rs +++ b/contracts/examples/nft-minter/src/nft_module.rs @@ -1,8 +1,6 @@ elrond_wasm::imports!(); elrond_wasm::derive_imports!(); -use elrond_wasm::elrond_codec::TopEncode; - const NFT_AMOUNT: u32 = 1; const ROYALTIES_MAX: u32 = 10_000; diff --git a/contracts/feature-tests/composability/vault/src/vault.rs b/contracts/feature-tests/composability/vault/src/vault.rs index 20130c12d9..3374256b1b 100644 --- a/contracts/feature-tests/composability/vault/src/vault.rs +++ b/contracts/feature-tests/composability/vault/src/vault.rs @@ -38,12 +38,7 @@ pub trait Vault { } fn esdt_transfers_multi(&self) -> MultiValueEncoded { - let esdt_transfers = self.call_value().all_esdt_transfers(); - let mut esdt_transfers_multi = MultiValueEncoded::new(); - for esdt_transfer in esdt_transfers.into_iter() { - esdt_transfers_multi.push(esdt_transfer.into_multi_value()); - } - esdt_transfers_multi + self.call_value().all_esdt_transfers().into_multi_value() } #[payable("*")] diff --git a/elrond-codec/src/multi/into_multi_value.rs b/elrond-codec/src/multi/into_multi_value.rs new file mode 100644 index 0000000000..4af6b9d9af --- /dev/null +++ b/elrond-codec/src/multi/into_multi_value.rs @@ -0,0 +1,10 @@ +use super::{TopDecodeMulti, TopEncodeMulti}; + +/// Defines conversion of a type to its multi-value representation. +/// +/// Consumes input. +pub trait IntoMultiValue { + type MultiValue: TopEncodeMulti + TopDecodeMulti; + + fn into_multi_value(self) -> Self::MultiValue; +} diff --git a/elrond-codec/src/multi/mod.rs b/elrond-codec/src/multi/mod.rs index 5e5b682666..d5ab287143 100644 --- a/elrond-codec/src/multi/mod.rs +++ b/elrond-codec/src/multi/mod.rs @@ -1,8 +1,10 @@ +mod into_multi_value; mod top_de_multi; mod top_de_multi_input; mod top_en_multi; mod top_en_multi_output; +pub use into_multi_value::*; pub use top_de_multi::*; pub use top_de_multi_input::*; pub use top_en_multi::*; diff --git a/elrond-wasm-modules/src/bonding_curve/utils/owner_endpoints.rs b/elrond-wasm-modules/src/bonding_curve/utils/owner_endpoints.rs index 53de35c413..be0ad5c212 100644 --- a/elrond-wasm-modules/src/bonding_curve/utils/owner_endpoints.rs +++ b/elrond-wasm-modules/src/bonding_curve/utils/owner_endpoints.rs @@ -12,7 +12,6 @@ use crate::bonding_curve::{ }; use super::structs::CurveArguments; -use elrond_wasm::{abi::TypeAbi, elrond_codec::TopEncode}; #[elrond_wasm::module] pub trait OwnerEndpointsModule: storage::StorageModule + events::EventsModule { diff --git a/elrond-wasm-modules/src/bonding_curve/utils/structs.rs b/elrond-wasm-modules/src/bonding_curve/utils/structs.rs index 6a9f10942e..3567757fd4 100644 --- a/elrond-wasm-modules/src/bonding_curve/utils/structs.rs +++ b/elrond-wasm-modules/src/bonding_curve/utils/structs.rs @@ -1,5 +1,4 @@ use crate::bonding_curve::curves::curve_function::CurveFunction; -use elrond_wasm::{abi::TypeAbi, elrond_codec::TopEncode}; elrond_wasm::imports!(); elrond_wasm::derive_imports!(); diff --git a/elrond-wasm-modules/src/bonding_curve/utils/user_endpoints.rs b/elrond-wasm-modules/src/bonding_curve/utils/user_endpoints.rs index 88a5df0635..37c3e5f329 100644 --- a/elrond-wasm-modules/src/bonding_curve/utils/user_endpoints.rs +++ b/elrond-wasm-modules/src/bonding_curve/utils/user_endpoints.rs @@ -7,7 +7,6 @@ use crate::bonding_curve::{ curves::curve_function::CurveFunction, utils::{events, storage, structs::BondingCurve}, }; -use elrond_wasm::{abi::TypeAbi, elrond_codec::TopEncode}; #[elrond_wasm::module] pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { diff --git a/elrond-wasm-modules/src/esdt.rs b/elrond-wasm-modules/src/esdt.rs index ed17be1a7e..c0bfd19e07 100644 --- a/elrond-wasm-modules/src/esdt.rs +++ b/elrond-wasm-modules/src/esdt.rs @@ -1,5 +1,3 @@ -use elrond_wasm::elrond_codec::TopEncode; - elrond_wasm::imports!(); /// Standard smart contract module for managing a single ESDT. diff --git a/elrond-wasm-modules/src/ongoing_operation.rs b/elrond-wasm-modules/src/ongoing_operation.rs index a37ab70b50..67c963c88a 100644 --- a/elrond-wasm-modules/src/ongoing_operation.rs +++ b/elrond-wasm-modules/src/ongoing_operation.rs @@ -1,7 +1,5 @@ elrond_wasm::imports!(); -use elrond_wasm::elrond_codec::TopEncode; - pub const DEFAULT_MIN_GAS_TO_SAVE_PROGRESS: u64 = 1_000_000; pub type LoopOp = bool; diff --git a/elrond-wasm/src/macros.rs b/elrond-wasm/src/macros.rs index b502e0b97f..a589fb380a 100644 --- a/elrond-wasm/src/macros.rs +++ b/elrond-wasm/src/macros.rs @@ -11,6 +11,7 @@ macro_rules! imports { SubAssign, }; use elrond_wasm::{ + abi::TypeAbi, api::{ BigFloatApi, BigIntApi, BlockchainApi, BlockchainApiImpl, CallValueApi, CallValueApiImpl, CryptoApi, CryptoApiImpl, EllipticCurveApi, ErrorApi, @@ -19,7 +20,10 @@ macro_rules! imports { }, arrayvec::ArrayVec, contract_base::{ContractBase, ProxyObjBase}, - elrond_codec::{multi_types::*, DecodeError, NestedDecode, NestedEncode, TopDecode}, + elrond_codec::{ + multi_types::*, DecodeError, IntoMultiValue, NestedDecode, NestedEncode, TopDecode, + TopEncode, + }, err_msg, esdt::*, io::*, diff --git a/elrond-wasm/src/types/managed/wrapped/esdt_token_payment.rs b/elrond-wasm/src/types/managed/wrapped/esdt_token_payment.rs index f9a03ba98a..05aed71024 100644 --- a/elrond-wasm/src/types/managed/wrapped/esdt_token_payment.rs +++ b/elrond-wasm/src/types/managed/wrapped/esdt_token_payment.rs @@ -7,7 +7,7 @@ use crate as elrond_wasm; // needed by the codec and TypeAbi generated code use crate::derive::TypeAbi; use elrond_codec::{ elrond_codec_derive::{NestedEncode, TopEncode}, - NestedDecode, TopDecode, + IntoMultiValue, NestedDecode, TopDecode, }; #[derive(TopEncode, NestedEncode, TypeAbi, Clone, PartialEq, Eq, Debug)] @@ -44,11 +44,6 @@ impl EsdtTokenPayment { pub fn into_tuple(self) -> (TokenIdentifier, u64, BigUint) { (self.token_identifier, self.token_nonce, self.amount) } - - #[inline] - pub fn into_multi_value(self) -> EsdtTokenPaymentMultiValue { - self.into() - } } impl TopDecode for EsdtTokenPayment { @@ -162,6 +157,15 @@ where }); } +impl IntoMultiValue for EsdtTokenPayment { + type MultiValue = EsdtTokenPaymentMultiValue; + + #[inline] + fn into_multi_value(self) -> Self::MultiValue { + self.into() + } +} + impl ManagedVecItem for EsdtTokenPayment { const PAYLOAD_SIZE: usize = 16; const SKIPS_RESERIALIZATION: bool = false; diff --git a/elrond-wasm/src/types/managed/wrapped/managed_vec.rs b/elrond-wasm/src/types/managed/wrapped/managed_vec.rs index d669b32bc5..88cf306d6e 100644 --- a/elrond-wasm/src/types/managed/wrapped/managed_vec.rs +++ b/elrond-wasm/src/types/managed/wrapped/managed_vec.rs @@ -4,14 +4,14 @@ use crate::{ types::{ heap::{ArgBuffer, BoxedBytes}, ManagedBuffer, ManagedBufferNestedDecodeInput, ManagedType, ManagedVecItem, ManagedVecRef, - ManagedVecRefIterator, MultiValueManagedVec, + ManagedVecRefIterator, MultiValueEncoded, MultiValueManagedVec, }, }; use alloc::vec::Vec; use core::{borrow::Borrow, iter::FromIterator, marker::PhantomData}; use elrond_codec::{ - DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, - NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeMultiOutput, + DecodeErrorHandler, EncodeErrorHandler, IntoMultiValue, NestedDecode, NestedDecodeInput, + NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeMultiOutput, TopEncodeOutput, }; @@ -452,6 +452,22 @@ where } } +impl IntoMultiValue for ManagedVec +where + M: ManagedTypeApi, + T: ManagedVecItem + IntoMultiValue, +{ + type MultiValue = MultiValueEncoded; + + fn into_multi_value(self) -> Self::MultiValue { + let mut result = MultiValueEncoded::new(); + for item in self.into_iter() { + result.push(item.into_multi_value()); + } + result + } +} + impl TypeAbi for ManagedVec where M: ManagedTypeApi,