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

TypeAbi Unmanaged field + ReturnsResultUnmanaged result handler #1604

Merged
merged 3 commits into from
May 7, 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/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl AdderInteract {
.to(self.state.current_adder_address())
.typed(adder_proxy::AdderProxy)
.sum()
.returns(ReturnsResultAs::<RustBigUint>::new())
.returns(ReturnsResultUnmanaged)
.prepare_async()
.run()
.await;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use multiversx_sc_scenario::imports::*;
use num_bigint::BigUint;

use scenario_tester::*;

Expand Down Expand Up @@ -66,9 +65,9 @@ fn st_blackbox() {
.to(ST_ADDRESS)
.typed(scenario_tester_proxy::ScenarioTesterProxy)
.sum()
.returns(ReturnsResultAs::<BigUint>::new())
.returns(ReturnsResultUnmanaged)
.run();
assert_eq!(value, BigUint::from(5u32));
assert_eq!(value, RustBigUint::from(5u32));

world
.tx()
Expand Down
3 changes: 3 additions & 0 deletions framework/base/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ mod type_abi_impl_codec_multi;
mod type_description;
mod type_description_container;

#[cfg(feature = "num-bigint")]
mod type_abi_impl_big_int;

pub use build_info_abi::*;
pub use contract_abi::*;
pub use endpoint_abi::*;
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/abi/type_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use alloc::{format, string::ToString, vec::Vec};
///
/// Will be automatically implemented for struct ad enum types via the `#[type_abi]` annotation.
pub trait TypeAbi: TypeAbiFrom<Self> {
type Unmanaged: TypeAbiFrom<Self>;

fn type_names() -> TypeNames {
TypeNames {
abi: Self::type_name(),
Expand Down
30 changes: 30 additions & 0 deletions framework/base/src/abi/type_abi_impl_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use alloc::{
impl TypeAbiFrom<()> for () {}

impl TypeAbi for () {
type Unmanaged = Self;

/// No another exception from the 1-type-1-output-abi rule:
/// the unit type produces no output.
fn output_abis(_output_names: &[&'static str]) -> OutputAbis {
Expand All @@ -20,6 +22,8 @@ impl TypeAbi for () {
impl<T, U> TypeAbiFrom<&U> for &T where T: TypeAbiFrom<U> {}

impl<T: TypeAbi> TypeAbi for &T {
type Unmanaged = Self;

fn type_name() -> TypeName {
T::type_name()
}
Expand All @@ -36,6 +40,8 @@ impl<T: TypeAbi> TypeAbi for &T {
impl<T, U> TypeAbiFrom<Box<U>> for Box<T> where T: TypeAbiFrom<U> {}

impl<T: TypeAbi> TypeAbi for Box<T> {
type Unmanaged = Self;

fn type_name() -> TypeName {
T::type_name()
}
Expand All @@ -52,6 +58,8 @@ impl<T: TypeAbi> TypeAbi for Box<T> {
impl<T, U> TypeAbiFrom<&[T]> for &[U] where T: TypeAbiFrom<U> {}

impl<T: TypeAbi> TypeAbi for &[T] {
type Unmanaged = Self;

fn type_name() -> TypeName {
let t_name = T::type_name();
if t_name == "u8" {
Expand All @@ -76,6 +84,8 @@ impl<T: TypeAbi> TypeAbi for &[T] {
impl<T, U> TypeAbiFrom<Vec<T>> for Vec<U> where T: TypeAbiFrom<U> {}

impl<T: TypeAbi> TypeAbi for Vec<T> {
type Unmanaged = Self;

fn type_name() -> TypeName {
<&[T]>::type_name()
}
Expand All @@ -92,6 +102,8 @@ impl<T: TypeAbi> TypeAbi for Vec<T> {
impl<T: TypeAbi, const CAP: usize> TypeAbiFrom<ArrayVec<T, CAP>> for ArrayVec<T, CAP> {}

impl<T: TypeAbi, const CAP: usize> TypeAbi for ArrayVec<T, CAP> {
type Unmanaged = Self;

fn type_name() -> TypeName {
<&[T]>::type_name()
}
Expand All @@ -108,6 +120,8 @@ impl<T: TypeAbi, const CAP: usize> TypeAbi for ArrayVec<T, CAP> {
impl<T> TypeAbiFrom<Box<[T]>> for Box<[T]> {}

impl<T: TypeAbi> TypeAbi for Box<[T]> {
type Unmanaged = Self;

fn type_name() -> TypeName {
<&[T]>::type_name()
}
Expand All @@ -127,6 +141,8 @@ impl TypeAbiFrom<&str> for String {}
impl TypeAbiFrom<Box<str>> for String {}

impl TypeAbi for String {
type Unmanaged = Self;

fn type_name() -> TypeName {
"utf-8 string".into()
}
Expand All @@ -135,6 +151,8 @@ impl TypeAbi for String {
impl TypeAbiFrom<&'static str> for &'static str {}

impl TypeAbi for &'static str {
type Unmanaged = Self;

fn type_name() -> TypeName {
String::type_name()
}
Expand All @@ -149,6 +167,8 @@ impl TypeAbiFrom<&str> for Box<str> {}
impl TypeAbiFrom<String> for Box<str> {}

impl TypeAbi for Box<str> {
type Unmanaged = Self;

fn type_name() -> TypeName {
String::type_name()
}
Expand All @@ -164,6 +184,8 @@ macro_rules! type_abi_name_only {
impl TypeAbiFrom<&$ty> for $ty {}

impl TypeAbi for $ty {
type Unmanaged = Self;

fn type_name() -> TypeName {
TypeName::from($name)
}
Expand Down Expand Up @@ -225,6 +247,8 @@ impl TypeAbiFrom<i8> for i16 {}
impl<T, U> TypeAbiFrom<Option<T>> for Option<U> where T: TypeAbiFrom<U> {}

impl<T: TypeAbi> TypeAbi for Option<T> {
type Unmanaged = Self;

fn type_name() -> TypeName {
format!("Option<{}>", T::type_name())
}
Expand All @@ -241,6 +265,8 @@ impl<T: TypeAbi> TypeAbi for Option<T> {
impl<T, U, E> TypeAbiFrom<Result<T, E>> for Result<U, E> where T: TypeAbiFrom<U> {}

impl<T: TypeAbi, E> TypeAbi for Result<T, E> {
type Unmanaged = Self;

fn type_name() -> TypeName {
T::type_name()
}
Expand Down Expand Up @@ -275,6 +301,8 @@ macro_rules! tuple_impls {
where
$($name: TypeAbi,)+
{
type Unmanaged = Self;

fn type_name() -> TypeName {
let mut repr = TypeName::from("tuple<");
$(
Expand Down Expand Up @@ -331,6 +359,8 @@ tuple_impls! {
impl<T, U, const N: usize> TypeAbiFrom<[T; N]> for [U; N] where T: TypeAbiFrom<U> {}

impl<T: TypeAbi, const N: usize> TypeAbi for [T; N] {
type Unmanaged = Self;

fn type_name() -> TypeName {
let mut repr = TypeName::from("array");
repr.push_str(N.to_string().as_str());
Expand Down
33 changes: 33 additions & 0 deletions framework/base/src/abi/type_abi_impl_big_int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::codec::num_bigint::{BigInt, BigUint};

use super::{TypeAbi, TypeAbiFrom, TypeName};

impl TypeAbiFrom<Self> for BigUint {}
impl TypeAbiFrom<&Self> for BigUint {}

impl TypeAbi for BigUint {
type Unmanaged = Self;

fn type_name() -> TypeName {
TypeName::from("BigUint")
}

fn type_name_rust() -> TypeName {
TypeName::from("num_bigint::BigUint")
}
}

impl TypeAbiFrom<Self> for BigInt {}
impl TypeAbiFrom<&Self> for BigInt {}

impl TypeAbi for BigInt {
type Unmanaged = Self;

fn type_name() -> TypeName {
TypeName::from("BigInt")
}

fn type_name_rust() -> TypeName {
TypeName::from("num_bigint::BigInt")
}
}
8 changes: 8 additions & 0 deletions framework/base/src/abi/type_abi_impl_codec_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ where

#[cfg(feature = "alloc")]
impl<T: TypeAbi> TypeAbi for crate::codec::multi_types::MultiValueVec<T> {
type Unmanaged = Self;

fn type_name() -> TypeName {
super::type_name_variadic::<T>()
}
Expand All @@ -35,6 +37,8 @@ impl<T: TypeAbi> TypeAbi for crate::codec::multi_types::MultiValueVec<T> {
impl<T> TypeAbiFrom<T> for IgnoreValue {}

impl TypeAbi for IgnoreValue {
type Unmanaged = Self;

fn type_name() -> TypeName {
TypeName::from("ignore")
}
Expand All @@ -51,6 +55,8 @@ impl TypeAbi for IgnoreValue {
impl<T, U> TypeAbiFrom<OptionalValue<T>> for OptionalValue<U> where T: TypeAbiFrom<U> {}

impl<T: TypeAbi> TypeAbi for OptionalValue<T> {
type Unmanaged = Self;

fn type_name() -> TypeName {
super::type_name_optional::<T>()
}
Expand Down Expand Up @@ -80,6 +86,8 @@ macro_rules! multi_arg_impls {
where
$($name: TypeAbi,)+
{
type Unmanaged = Self;

fn type_name() -> TypeName {
let mut repr = TypeName::from("multi");
repr.push('<');
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/bi_di_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ where
+ PartialEq
+ TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
MultiValueEncoded::<SA, MultiValue2<K, V>>::type_name()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/linked_list_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ where
SA: StorageMapperApi,
T: TopEncode + TopDecode + NestedEncode + NestedDecode + Clone + TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<T>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/map_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ where
K: TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi + 'static,
V: TopEncode + TopDecode + TypeAbi + 'static,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
MultiValueEncoded::<SA, MultiValue2<K, V>>::type_name()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/queue_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ where
SA: StorageMapperApi,
T: TopEncode + TopDecode + TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<T>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/set_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ where
SA: StorageMapperApi,
T: TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<T>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/single_value_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ where
SA: StorageMapperApi,
T: TopEncode + TopDecode + TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
T::type_name()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ impl<SA> TypeAbi for FungibleTokenMapper<SA>
where
SA: StorageMapperApi + CallTypeApi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
TokenIdentifier::<SA>::type_name()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ impl<SA> TypeAbi for NonFungibleTokenMapper<SA>
where
SA: StorageMapperApi + CallTypeApi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
TokenIdentifier::<SA>::type_name()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/unique_id_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ impl<SA> TypeAbi for UniqueIdMapper<SA, CurrentStorage>
where
SA: StorageMapperApi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<usize>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/unordered_set_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ where
SA: StorageMapperApi,
T: TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<T>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/user_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ impl<SA> TypeAbi for UserMapper<SA, CurrentStorage>
where
SA: StorageMapperApi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<ManagedAddress<SA>>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/storage/mappers/vec_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ where
SA: StorageMapperApi,
T: TopEncode + TopDecode + TypeAbi,
{
type Unmanaged = Self;

fn type_name() -> TypeName {
crate::abi::type_name_variadic::<T>()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/types/crypto/message_hash_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ impl From<u8> for MessageHashType {
impl TypeAbiFrom<Self> for MessageHashType {}

impl TypeAbi for MessageHashType {
type Unmanaged = Self;

fn type_name() -> TypeName {
"MessageHashType".into()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/types/flags/code_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ impl TopDecode for CodeMetadata {
impl TypeAbiFrom<Self> for CodeMetadata {}

impl TypeAbi for CodeMetadata {
type Unmanaged = Self;

fn type_name() -> TypeName {
"CodeMetadata".into()
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/types/heap/async_call_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ where
impl<T: TypeAbi> TypeAbiFrom<Self> for AsyncCallResult<T> {}

impl<T: TypeAbi> TypeAbi for AsyncCallResult<T> {
type Unmanaged = Self;

fn type_name() -> TypeName {
let mut repr = TypeName::from("AsyncCallResult<");
repr.push_str(T::type_name().as_str());
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/types/heap/boxed_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ impl TopDecode for BoxedBytes {
impl TypeAbiFrom<Self> for BoxedBytes {}

impl TypeAbi for BoxedBytes {
type Unmanaged = Self;

fn type_name() -> TypeName {
"bytes".into()
}
Expand Down
Loading
Loading