Skip to content

Commit

Permalink
Merge pull request multiversx#858 from ElrondNetwork/into-multi
Browse files Browse the repository at this point in the history
IntoMultiValue trait
  • Loading branch information
andrei-marinica authored Nov 18, 2022
2 parents 2a53315 + 8f9243f commit 4337847
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 33 deletions.
2 changes: 0 additions & 2 deletions contracts/examples/nft-minter/src/nft_module.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
7 changes: 1 addition & 6 deletions contracts/feature-tests/composability/vault/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ pub trait Vault {
}

fn esdt_transfers_multi(&self) -> MultiValueEncoded<EsdtTokenPaymentMultiValue> {
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("*")]
Expand Down
29 changes: 21 additions & 8 deletions elrond-codec/src/impl_for_types/impl_phantom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> TopEncode for PhantomData<T> {
impl<T> TopEncode for PhantomData<T> {
#[inline]
fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
where
Expand All @@ -21,7 +21,7 @@ impl <T> TopEncode for PhantomData<T> {
}
}

impl <T> TopDecode for PhantomData<T> {
impl<T> TopDecode for PhantomData<T> {
fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
where
I: TopDecodeInput,
Expand All @@ -35,7 +35,7 @@ impl <T> TopDecode for PhantomData<T> {
}
}

impl <T> NestedEncode for PhantomData<T> {
impl<T> NestedEncode for PhantomData<T> {
#[inline]
fn dep_encode_or_handle_err<O, H>(&self, _dest: &mut O, _h: H) -> Result<(), H::HandledErr>
where
Expand All @@ -46,7 +46,7 @@ impl <T> NestedEncode for PhantomData<T> {
}
}

impl <T> NestedDecode for PhantomData<T> {
impl<T> NestedDecode for PhantomData<T> {
#[inline]
fn dep_decode_or_handle_err<I, H>(_input: &mut I, _h: H) -> Result<Self, H::HandledErr>
where
Expand All @@ -57,13 +57,12 @@ impl <T> NestedDecode for PhantomData<T> {
}
}


#[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<M> {
Expand All @@ -84,11 +83,25 @@ pub mod tests {

#[test]
fn test_dep_struc() {
check_dep_encode_decode(TestStructWithPhantom::<u64>{x: 42, y:42, _phantom: PhantomData::<u64>}, &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42]);
check_dep_encode_decode(
TestStructWithPhantom::<u64> {
x: 42,
y: 42,
_phantom: PhantomData::<u64>,
},
&[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42],
);
}

#[test]
fn test_top_struc() {
check_top_encode_decode(TestStructWithPhantom::<u64>{x: 42, y:42, _phantom: PhantomData::<u64>}, &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42]);
check_top_encode_decode(
TestStructWithPhantom::<u64> {
x: 42,
y: 42,
_phantom: PhantomData::<u64>,
},
&[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42],
);
}
}
10 changes: 10 additions & 0 deletions elrond-codec/src/multi/into_multi_value.rs
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions elrond-codec/src/multi/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion elrond-wasm-modules/src/bonding_curve/utils/structs.rs
Original file line number Diff line number Diff line change
@@ -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!();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions elrond-wasm-modules/src/esdt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use elrond_wasm::elrond_codec::TopEncode;

elrond_wasm::imports!();

/// Standard smart contract module for managing a single ESDT.
Expand Down
2 changes: 0 additions & 2 deletions elrond-wasm-modules/src/ongoing_operation.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 5 additions & 1 deletion elrond-wasm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ macro_rules! imports {
SubAssign,
};
use elrond_wasm::{
abi::TypeAbi,
api::{
BigFloatApi, BigIntApi, BlockchainApi, BlockchainApiImpl, CallValueApi,
CallValueApiImpl, CryptoApi, CryptoApiImpl, EllipticCurveApi, ErrorApi,
Expand All @@ -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::*,
Expand Down
16 changes: 10 additions & 6 deletions elrond-wasm/src/types/managed/wrapped/esdt_token_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -44,11 +44,6 @@ impl<M: ManagedTypeApi> EsdtTokenPayment<M> {
pub fn into_tuple(self) -> (TokenIdentifier<M>, u64, BigUint<M>) {
(self.token_identifier, self.token_nonce, self.amount)
}

#[inline]
pub fn into_multi_value(self) -> EsdtTokenPaymentMultiValue<M> {
self.into()
}
}

impl<M: ManagedTypeApi> TopDecode for EsdtTokenPayment<M> {
Expand Down Expand Up @@ -162,6 +157,15 @@ where
});
}

impl<M: ManagedTypeApi> IntoMultiValue for EsdtTokenPayment<M> {
type MultiValue = EsdtTokenPaymentMultiValue<M>;

#[inline]
fn into_multi_value(self) -> Self::MultiValue {
self.into()
}
}

impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPayment<M> {
const PAYLOAD_SIZE: usize = 16;
const SKIPS_RESERIALIZATION: bool = false;
Expand Down
22 changes: 19 additions & 3 deletions elrond-wasm/src/types/managed/wrapped/managed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -452,6 +452,22 @@ where
}
}

impl<M, T> IntoMultiValue for ManagedVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + IntoMultiValue,
{
type MultiValue = MultiValueEncoded<M, T::MultiValue>;

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<M, T> TypeAbi for ManagedVec<M, T>
where
M: ManagedTypeApi,
Expand Down

0 comments on commit 4337847

Please sign in to comment.