Skip to content

Commit

Permalink
Merge branch 'rc/v0.54' into mdrop
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 28, 2024
2 parents 94e6981 + d37a5bf commit 96f3944
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait ManagedVecFeatures {
&self,
mv: ManagedVec<BigUint>,
index: usize,
item: &BigUint,
item: BigUint,
) -> ManagedVec<BigUint> {
let mut result = mv;
if result.set(index, item).is_ok() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ fn test_managed_vec_set() {
mv2.push(BigUint::from(1u32));
mv2.push(BigUint::from(5u32));
mv2.push(BigUint::from(3u32));
assert_eq!(bf.managed_vec_set(mv1, 1, &BigUint::from(5u64)), mv2);
assert_eq!(bf.managed_vec_set(mv1, 1, BigUint::from(5u64)), mv2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPaymentMultiValue<M> {
}

#[inline]
fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
self.obj.to_byte_writer(writer)
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
self.obj.into_byte_writer(writer)
}
}

Expand Down
12 changes: 6 additions & 6 deletions framework/base/src/types/managed/wrapped/esdt_token_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ where
})
}

fn managed_vec_item_to_slice<T>(arr: &mut [u8], index: &mut usize, item: &T)
fn managed_vec_item_to_slice<T>(arr: &mut [u8], index: &mut usize, item: T)
where
T: ManagedVecItem,
{
ManagedVecItem::to_byte_writer(item, |bytes| {
ManagedVecItem::into_byte_writer(item, |bytes| {
let size = T::payload_size();
arr[*index..*index + size].copy_from_slice(bytes);
*index += size;
Expand Down Expand Up @@ -211,13 +211,13 @@ impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPayment<M> {
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
let mut arr: [u8; 16] = [0u8; 16];
let mut index = 0;

managed_vec_item_to_slice(&mut arr, &mut index, &self.token_identifier);
managed_vec_item_to_slice(&mut arr, &mut index, &self.token_nonce);
managed_vec_item_to_slice(&mut arr, &mut index, &self.amount);
managed_vec_item_to_slice(&mut arr, &mut index, self.token_identifier);
managed_vec_item_to_slice(&mut arr, &mut index, self.token_nonce);
managed_vec_item_to_slice(&mut arr, &mut index, self.amount);

writer(&arr[..])
}
Expand Down
4 changes: 2 additions & 2 deletions framework/base/src/types/managed/wrapped/managed_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ where
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
<T::OwnHandle as ManagedVecItem>::to_byte_writer(&self.handle.clone(), writer)
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
<T::OwnHandle as ManagedVecItem>::into_byte_writer(self.handle, writer)
}
}

Expand Down
8 changes: 4 additions & 4 deletions framework/base/src/types/managed/wrapped/managed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ where
}
}

pub fn set(&mut self, index: usize, item: &T) -> Result<(), InvalidSliceError> {
pub fn set(&mut self, index: usize, item: T) -> Result<(), InvalidSliceError> {
let byte_index = index * T::payload_size();
item.to_byte_writer(|slice| self.buffer.set_slice(byte_index, slice))
item.into_byte_writer(|slice| self.buffer.set_slice(byte_index, slice))
}

/// Returns a new `ManagedVec`, containing the [start_index, end_index) range of elements.
Expand All @@ -205,7 +205,7 @@ where
}

pub fn push(&mut self, item: T) {
item.to_byte_writer(|bytes| {
item.into_byte_writer(|bytes| {
self.buffer.append_bytes(bytes);
});
}
Expand Down Expand Up @@ -251,7 +251,7 @@ where
}

pub fn overwrite_with_single_item(&mut self, item: T) {
item.to_byte_writer(|bytes| {
item.into_byte_writer(|bytes| {
self.buffer.overwrite(bytes);
});
}
Expand Down
47 changes: 28 additions & 19 deletions framework/base/src/types/managed/wrapped/managed_vec_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ pub trait ManagedVecItem: 'static {
reader: Reader,
) -> Self::Ref<'a>;

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R;
/// Converts the object into bytes.
///
/// The output is processed by the `writer` lambda.
/// The writer is provided by the caller.
/// The callee will use it to pass on the bytes.
///
/// The method is used when instering (push, overwrite) into a ManagedVec.
///
/// Note that a destructor should not be called at this moment, since the ManagedVec will take ownership of the item.
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R;
}

macro_rules! impl_int {
Expand All @@ -73,7 +82,7 @@ macro_rules! impl_int {
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
let bytes = self.to_be_bytes();
writer(&bytes)
}
Expand Down Expand Up @@ -104,8 +113,8 @@ impl ManagedVecItem for usize {
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
let bytes = (*self as u32).to_be_bytes();
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
let bytes = (self as u32).to_be_bytes();
writer(&bytes)
}
}
Expand All @@ -125,11 +134,11 @@ impl ManagedVecItem for bool {
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
// true -> 1u8
// false -> 0u8
let u8_value = u8::from(*self);
<u8 as ManagedVecItem>::to_byte_writer(&u8_value, writer)
let u8_value = u8::from(self);
<u8 as ManagedVecItem>::into_byte_writer(u8_value, writer)
}
}

Expand Down Expand Up @@ -161,12 +170,12 @@ where
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
let mut payload = Self::PAYLOAD::new_buffer();
let slice = payload.payload_slice_mut();
if let Some(t) = self {
slice[0] = 1;
T::to_byte_writer(t, |bytes| {
T::into_byte_writer(t, |bytes| {
slice[1..].copy_from_slice(bytes);
});
}
Expand All @@ -193,8 +202,8 @@ macro_rules! impl_managed_type {
ManagedRef::wrap_handle(handle)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
<$ty<M> as ManagedType<M>>::OwnHandle::to_byte_writer(&self.get_handle(), writer)
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
<$ty<M> as ManagedType<M>>::OwnHandle::into_byte_writer(self.get_handle(), writer)
}
}
};
Expand Down Expand Up @@ -227,9 +236,9 @@ where
ManagedRef::wrap_handle(handle)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
<<Self as ManagedType<M>>::OwnHandle as ManagedVecItem>::to_byte_writer(
&self.get_handle(),
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
<<Self as ManagedType<M>>::OwnHandle as ManagedVecItem>::into_byte_writer(
self.get_handle(),
writer,
)
}
Expand All @@ -256,8 +265,8 @@ where
ManagedRef::wrap_handle(handle)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
<M::ManagedBufferHandle as ManagedVecItem>::to_byte_writer(&self.get_handle(), writer)
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
<M::ManagedBufferHandle as ManagedVecItem>::into_byte_writer(self.get_handle(), writer)
}
}

Expand All @@ -278,7 +287,7 @@ impl ManagedVecItem for EsdtTokenType {
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
writer(&[self.as_u8()])
}
}
Expand All @@ -298,7 +307,7 @@ impl ManagedVecItem for EsdtLocalRole {
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
<u16 as ManagedVecItem>::to_byte_writer(&self.as_u16(), writer)
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
<u16 as ManagedVecItem>::into_byte_writer(self.as_u16(), writer)
}
}
10 changes: 6 additions & 4 deletions framework/base/src/types/managed/wrapped/managed_vec_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};
use core::{
marker::PhantomData,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
};

Expand All @@ -16,7 +17,7 @@ where
_phantom_t: PhantomData<&'a mut T>, // needed for the lifetime, even though T is present
managed_vec_handle: M::ManagedBufferHandle,
item_index: usize,
item: T,
item: ManuallyDrop<T>,
}

impl<'a, M, T> ManagedVecRef<'a, M, T>
Expand All @@ -37,7 +38,7 @@ where
_phantom_t: PhantomData,
managed_vec_handle,
item_index,
item,
item: ManuallyDrop::new(item),
}
}
}
Expand All @@ -48,8 +49,9 @@ where
T: ManagedVecItem,
{
fn drop(&mut self) {
let _ = Self::wrap_as_managed_vec(self.managed_vec_handle.clone())
.set(self.item_index, &self.item);
let item = unsafe { ManuallyDrop::take(&mut self.item) };
let _ =
Self::wrap_as_managed_vec(self.managed_vec_handle.clone()).set(self.item_index, item);
}
}

Expand Down
6 changes: 3 additions & 3 deletions framework/derive/src/managed_vec_item_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn generate_to_byte_writer_snippets(fields: &syn::Fields) -> Vec<proc_macro2::To
let field_ident = &field.ident;
let type_name = &field.ty;
quote! {
multiversx_sc::types::ManagedVecItem::to_byte_writer(&self.#field_ident, |bytes| {
multiversx_sc::types::ManagedVecItem::into_byte_writer(self.#field_ident, |bytes| {
let next_index = index + <#type_name as multiversx_sc::types::ManagedVecItem>::payload_size();
payload_slice[index .. next_index].copy_from_slice(bytes);
index = next_index;
Expand Down Expand Up @@ -137,7 +137,7 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
let mut arr: [u8; 1] = [0u8; 1];
arr[0] = match self {
#(#writer_match_arms)*
Expand Down Expand Up @@ -180,7 +180,7 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token
Self::from_byte_reader(reader)
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, mut writer: Writer) -> R {
#payload_buffer_snippet
let mut index = 0;

Expand Down
4 changes: 2 additions & 2 deletions framework/scenario/src/api/impl_vh/debug_handle_vh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl ManagedVecItem for DebugHandle {
use_raw_handle(RawHandle::from_byte_reader(reader))
}

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
RawHandle::to_byte_writer(&self.get_raw_handle(), writer)
fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
RawHandle::into_byte_writer(self.get_raw_handle(), writer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ fn managed_struct_to_bytes_writer() {
let handle_bytes = s.big_uint.get_handle().to_be_bytes();
let expected = [0xff, 0xff, 0xff, handle_bytes[3], 0x00, 0x01, 0x23, 0x45];

<ManagedStructWithBigUint<StaticApi> as multiversx_sc::types::ManagedVecItem>::to_byte_writer(
&s,
<ManagedStructWithBigUint<StaticApi> as multiversx_sc::types::ManagedVecItem>::into_byte_writer(
s,
|bytes| {
payload_slice.copy_from_slice(bytes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ fn struct_to_bytes_writer() {
handle3[1], handle3[2], handle3[3], handle4[0], handle4[1], handle4[2], handle4[3],
];

<ManagedStructWithToken<StaticApi> as multiversx_sc::types::ManagedVecItem>::to_byte_writer(
&s,
<ManagedStructWithToken<StaticApi> as multiversx_sc::types::ManagedVecItem>::into_byte_writer(
s,
|bytes| {
payload_slice.copy_from_slice(bytes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fn enum_static() {

#[test]
fn enum_to_bytes_writer() {
<SimpleEnum as multiversx_sc::types::ManagedVecItem>::to_byte_writer(
&SimpleEnum::Variant1,
<SimpleEnum as multiversx_sc::types::ManagedVecItem>::into_byte_writer(
SimpleEnum::Variant1,
|bytes| {
assert_eq!(bytes.len(), 1);
assert_eq!(bytes[0], 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn struct_1_to_bytes_writer() {
let mut payload = <Struct1 as multiversx_sc::types::ManagedVecItem>::PAYLOAD::new_buffer();
let payload_slice = payload.payload_slice_mut();

<Struct1 as multiversx_sc::types::ManagedVecItem>::to_byte_writer(&s, |bytes| {
<Struct1 as multiversx_sc::types::ManagedVecItem>::into_byte_writer(s, |bytes| {
payload_slice.copy_from_slice(bytes);
assert_eq!(
payload_slice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn struct_to_bytes_writer() {
/* arr */ 0x61, 0x11, 0x62, 0x22,
];

<Struct2 as multiversx_sc::types::ManagedVecItem>::to_byte_writer(&s, |bytes| {
<Struct2 as multiversx_sc::types::ManagedVecItem>::into_byte_writer(s, |bytes| {
assert_eq!(bytes, &expected_payload[..]);
});
}
Expand Down

0 comments on commit 96f3944

Please sign in to comment.