Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManagedVecRef #1885

Merged
merged 6 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/examples/lottery-esdt/src/lottery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub trait Lottery {
if let Some(whitelist) = opt_whitelist.as_option() {
let mut mapper = self.lottery_whitelist(&lottery_name);
for addr in &*whitelist {
mapper.insert(addr);
mapper.insert(addr.clone());
}
}

Expand Down
3 changes: 2 additions & 1 deletion contracts/examples/multisig/src/multisig_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub trait MultisigStateModule {

fn add_multiple_board_members(&self, new_board_members: ManagedVec<ManagedAddress>) -> usize {
let mut duplicates = false;
let new_board_members_len = new_board_members.len();
self.user_mapper().get_or_create_users(
new_board_members.into_iter(),
|user_id, new_user| {
Expand All @@ -49,7 +50,7 @@ pub trait MultisigStateModule {
require!(!duplicates, "duplicate board member");

let num_board_members_mapper = self.num_board_members();
let new_num_board_members = num_board_members_mapper.get() + new_board_members.len();
let new_num_board_members = num_board_members_mapper.get() + new_board_members_len;
num_board_members_mapper.set(new_num_board_members);

new_num_board_members
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/order-book/pair/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub const FREE_ORDER_FROM_STORAGE_MIN_PENALTIES: u64 = 6;

#[type_abi]
#[derive(
ManagedVecItem, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone,
ManagedVecItem, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone, Copy,
)]
pub enum OrderType {
Buy,
Expand Down
10 changes: 5 additions & 5 deletions contracts/examples/order-book/pair/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait EventsModule {
fn emit_order_event(&self, order: Order<Self::Api>) {
let caller = self.blockchain().get_caller();
let epoch = self.blockchain().get_block_epoch();
let order_type = order.order_type.clone();
let order_type = order.order_type;

self.order_event(caller, epoch, order_type, order);
}
Expand All @@ -31,7 +31,7 @@ pub trait EventsModule {
for order in orders.iter() {
let order_type = order.order_type;
let order_id = order.id;
let order_creator = order.creator;
let order_creator = &order.creator;

self.match_order_event(&caller, epoch, order_type, order_id, order_creator);
}
Expand All @@ -44,7 +44,7 @@ pub trait EventsModule {
for order in orders.iter() {
let order_type = order.order_type;
let order_id = order.id;
let order_creator = order.creator;
let order_creator = &order.creator;

self.free_order_event(&caller, epoch, order_type, order_id, order_creator);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ pub trait EventsModule {
#[indexed] epoch: u64,
#[indexed] order_type: OrderType,
#[indexed] order_id: u64,
#[indexed] order_creator: ManagedAddress,
#[indexed] order_creator: &ManagedAddress,
);

#[event("free_order")]
Expand All @@ -85,6 +85,6 @@ pub trait EventsModule {
#[indexed] epoch: u64,
#[indexed] order_type: OrderType,
#[indexed] order_id: u64,
#[indexed] order_creator: ManagedAddress,
#[indexed] order_creator: &ManagedAddress,
);
}
2 changes: 1 addition & 1 deletion contracts/examples/order-book/pair/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub trait OrdersModule:
let mut orders_vec = MultiValueManagedVec::new();
for order in orders.iter() {
if order.order_type == order_type {
orders_vec.push(order);
orders_vec.push(order.clone());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Bracket {
}

#[type_abi]
#[derive(ManagedVecItem, NestedEncode, NestedDecode)]
#[derive(ManagedVecItem, NestedEncode, NestedDecode, Clone)]
pub struct ComputedBracket<M: ManagedTypeApi> {
pub end_index: u64,
pub nft_reward_percent: BigUint<M>,
Expand Down Expand Up @@ -74,7 +74,7 @@ pub trait RewardsDistribution:
.unwrap_or_else(|| self.new_raffle());
let mut rng = RandomnessSource::default();

let mut bracket = raffle.computed_brackets.get(0);
let mut bracket = raffle.computed_brackets.get(0).clone();

let run_result = self.run_while_it_has_gas(DEFAULT_MIN_GAS_TO_SAVE_PROGRESS, || {
let ticket = self.shuffle_and_pick_single_ticket(
Expand Down Expand Up @@ -138,7 +138,7 @@ pub trait RewardsDistribution:
) {
while ticket > bracket.end_index {
computed_brackets.remove(0);
*bracket = computed_brackets.get(0);
*bracket = computed_brackets.get(0).clone();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait DistributionModule {
}
self.tx()
.to(&distribution.address)
.raw_call(distribution.endpoint)
.raw_call(distribution.endpoint.clone())
.egld_or_single_esdt(token_id, token_nonce, &payment_amount)
.gas(distribution.gas_limit)
.transfer_execute();
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/seed-nft-minter/src/seed_nft_minter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait SeedNftMinter:
marketplaces: ManagedVec<ManagedAddress>,
distribution: ManagedVec<Distribution<Self::Api>>,
) {
self.marketplaces().extend(&marketplaces);
self.marketplaces().extend(marketplaces);
self.init_distribution(distribution);
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/feature-tests/basic-features/src/echo_managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ pub trait EchoManagedTypes {
MultiValue3<ManagedAddress, usize, MultiValueEncodedCounted<MultiValue2<usize, usize>>>,
> {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ pub trait FirstContract {
arg_buffer.push_arg(esdt_token_identifier);
arg_buffer.push_arg(amount);
arg_buffer.push_arg(func_name);
for arg in args.into_iter() {
arg_buffer.push_arg_raw(arg);
for arg in args {
arg_buffer.push_arg_raw(arg.clone());
}

self.tx()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait ForwarderRaw:
));
}
} else {
for payment in payments.into_iter() {
for payment in payments.iter() {
let _ = self.callback_payments().push(&(
EgldOrEsdtTokenIdentifier::esdt(payment.token_identifier),
payment.token_nonce,
Expand Down
2 changes: 1 addition & 1 deletion contracts/feature-tests/composability/vault/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub trait Vault {

let mut new_tokens = ManagedVec::new();

for payment in payments.into_iter() {
for payment in payments.iter() {
// burn old tokens
self.send().esdt_local_burn(
&payment.token_identifier,
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/src/governance/governance_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct ProposalFees<M: ManagedTypeApi> {
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem)]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)]
pub struct FeeEntry<M: ManagedTypeApi> {
pub depositor_addr: ManagedAddress<M>,
pub tokens: EsdtTokenPayment<M>,
Expand Down
4 changes: 2 additions & 2 deletions contracts/modules/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub trait GovernanceModule:
let mut i = 0;
while i < proposal.fees.entries.len() {
if proposal.fees.entries.get(i).depositor_addr == caller {
fees_to_send.push(proposal.fees.entries.get(i));
fees_to_send.push(proposal.fees.entries.get(i).clone());
proposal.fees.entries.remove(i);
} else {
i += 1;
Expand Down Expand Up @@ -417,7 +417,7 @@ pub trait GovernanceModule:
let payments = self.proposals().get(proposal_id).fees;

for fee_entry in payments.entries.iter() {
let payment = fee_entry.tokens;
let payment = &fee_entry.tokens;
self.tx()
.to(&fee_entry.depositor_addr)
.single_esdt(
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait StakingModule {
user_whitelist: &ManagedVec<ManagedAddress>,
) {
for user in user_whitelist {
let _ = self.user_whitelist().insert(user);
let _ = self.user_whitelist().insert(user.clone());
}

let nr_board_members = self.user_whitelist().len();
Expand Down
7 changes: 2 additions & 5 deletions framework/base/src/api/managed_types/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ pub trait HandleTypeInfo {

use crate::codec::TryStaticCast;

use crate::{
api::{ErrorApi, ErrorApiImpl},
types::ManagedVecItem,
};
use crate::api::{ErrorApi, ErrorApiImpl};

pub type RawHandle = i32;

pub trait HandleConstraints:
ManagedVecItem + TryStaticCast + Debug + Clone + From<RawHandle> + PartialEq + PartialEq<RawHandle>
TryStaticCast + Debug + Clone + From<RawHandle> + PartialEq + PartialEq<RawHandle>
{
fn new(handle: RawHandle) -> Self;
fn to_be_bytes(&self) -> [u8; 4];
Expand Down
2 changes: 1 addition & 1 deletion framework/base/src/types/heap/arg_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
{
fn from(managed_arg_buffer: &ManagedArgBuffer<M>) -> Self {
let mut result = Self::new();
for m_arg in managed_arg_buffer.data.into_iter() {
for m_arg in &managed_arg_buffer.data {
result.push_argument_bytes(m_arg.to_boxed_bytes().as_slice());
}
result
Expand Down
2 changes: 1 addition & 1 deletion framework/base/src/types/interaction/managed_arg_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
#[cfg(feature = "alloc")]
pub fn to_raw_args_vec(&self) -> Vec<Vec<u8>> {
let mut v = Vec::new();
for item in self.data.into_iter() {
for item in &self.data {
v.push(item.to_boxed_bytes().into_vec());
}
v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
multi_types::MultiValue3, DecodeErrorHandler, EncodeErrorHandler, TopDecodeMulti,
TopDecodeMultiInput, TopDecodeMultiLength, TopEncodeMulti, TopEncodeMultiOutput,
},
types::ManagedVecRef,
};

use crate::{
Expand Down Expand Up @@ -42,15 +43,14 @@ impl<M: ManagedTypeApi> EsdtTokenPaymentMultiValue<M> {
impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPaymentMultiValue<M> {
type PAYLOAD = <EsdtTokenPayment<M> as ManagedVecItem>::PAYLOAD;
const SKIPS_RESERIALIZATION: bool = EsdtTokenPayment::<M>::SKIPS_RESERIALIZATION;
type Ref<'a> = Self;
type Ref<'a> = ManagedVecRef<'a, Self>;

fn read_from_payload(payload: &Self::PAYLOAD) -> Self {
EsdtTokenPayment::read_from_payload(payload).into()
}

unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> {
// TODO: managed ref
Self::read_from_payload(payload)
ManagedVecRef::new(Self::read_from_payload(payload))
}

fn save_to_payload(self, payload: &mut Self::PAYLOAD) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,15 @@ where
T: ManagedVecItem + TopEncode + 'static,
{
#[inline]
#[rustfmt::skip]
fn from(v: ManagedVec<M, T>) -> Self {
try_cast_execute_or_else(
v,
MultiValueEncoded::from_raw_vec,
|v| MultiValueEncoded::from(&v),
MultiValueEncoded::from_iter,
)
}
}

impl<M, T> From<&ManagedVec<M, T>> for MultiValueEncoded<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + TopEncode,
{
#[inline]
fn from(v: &ManagedVec<M, T>) -> Self {
let mut result = MultiValueEncoded::new();
for item in v.into_iter() {
result.push(item);
}
result
}
}

impl<M, T> MultiValueEncoded<M, T>
where
M: ManagedTypeApi,
Expand Down Expand Up @@ -181,7 +165,7 @@ where
pub fn to_vec(&self) -> ManagedVec<M, T> {
let mut result = ManagedVec::new();
let serializer = ManagedSerializer::<M>::new();
for item in self.raw_buffers.into_iter() {
for item in &self.raw_buffers {
result.push(serializer.top_decode_from_managed_buffer(&item));
}
result
Expand All @@ -198,7 +182,7 @@ where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
{
for elem in self.raw_buffers.into_iter() {
for elem in &self.raw_buffers {
elem.multi_encode_or_handle_err(output, h)?;
}
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ where
pub fn to_vec(&self) -> ManagedVec<M, T> {
let mut result = ManagedVec::new();
let serializer = ManagedSerializer::<M>::new();
for item in self.raw_buffers.into_iter() {
for item in &self.raw_buffers {
result.push(serializer.top_decode_from_managed_buffer(&item));
}
result
Expand All @@ -156,7 +156,7 @@ where
let raw_count = self.raw_buffers.len();
let count = raw_count / T::LEN;
count.multi_encode_or_handle_err(output, h)?;
for elem in self.raw_buffers.into_iter() {
for elem in &self.raw_buffers {
elem.multi_encode_or_handle_err(output, h)?;
}
Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::borrow::Borrow;

use multiversx_sc_codec::multi_types::MultiValueVec;

use crate::{
Expand All @@ -7,7 +9,7 @@ use crate::{
DecodeErrorHandler, EncodeErrorHandler, TopDecodeMulti, TopDecodeMultiInput,
TopEncodeMulti, TopEncodeMultiOutput, Vec,
},
types::ManagedType,
types::{ManagedType, ManagedVecOwnedIterator},
};

use crate::types::{ManagedVec, ManagedVecItem, ManagedVecRefIterator};
Expand Down Expand Up @@ -142,6 +144,20 @@ where
}
}

impl<M, T> IntoIterator for MultiValueManagedVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
type Item = T;

type IntoIter = ManagedVecOwnedIterator<M, T>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<'a, M, T> IntoIterator for &'a MultiValueManagedVec<M, T>
where
M: ManagedTypeApi,
Expand Down Expand Up @@ -181,8 +197,8 @@ where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
{
for elem in self.0.into_iter() {
elem.multi_encode_or_handle_err(output, h)?;
for elem in &self.0 {
elem.borrow().multi_encode_or_handle_err(output, h)?;
}
Ok(())
}
Expand Down
Loading
Loading