Skip to content

Commit

Permalink
Merge pull request #1510 from multiversx/annotation-ref
Browse files Browse the repository at this point in the history
Annotation system refactor
  • Loading branch information
andrei-marinica authored Mar 27, 2024
2 parents 8f96dde + dc9c49b commit a8394b9
Show file tree
Hide file tree
Showing 27 changed files with 332 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub trait ProxyTestFirst {
.to(other_contract)
.raw_upgrade()
.argument(&456)
.egld(payment.clone_value())
.egld(payment)
.upgrade_contract(&code, CodeMetadata::UPGRADEABLE);
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/src/esdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub trait EsdtModule {
let egld_returned = self.call_value().egld_value();
self.tx()
.to(&initial_caller)
.egld(&*egld_returned)
.egld(egld_returned)
.transfer_if_not_empty();
},
}
Expand Down
127 changes: 24 additions & 103 deletions framework/base/src/types/interaction/annotated.rs
Original file line number Diff line number Diff line change
@@ -1,116 +1,37 @@
use crate::types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer};
mod annotated_impl_big_uint;
mod annotated_impl_managed_address;
mod annotated_impl_managed_buffer;

use crate::{
proxy_imports::ManagedRef,
types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer},
};

use super::TxEnv;

pub trait AnnotatedValue<Env, T>
pub trait AnnotatedValue<Env, T>: Sized
where
Env: TxEnv,
{
fn annotation(&self, env: &Env) -> ManagedBuffer<Env::Api>;

fn into_value(self, env: &Env) -> T;
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for ManagedAddress<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.hex_expr()
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
self
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &ManagedAddress<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.hex_expr()
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
self.clone()
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for Address
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
ManagedAddress::from(self).hex_expr()
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
self.into()
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &Address
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
ManagedAddress::from(*self).hex_expr()
}
/// Produces the value from a reference of the annotated type. Might involve a `.clone()` in some cases.
fn to_value(&self, env: &Env) -> T;

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
self.into()
}
}

impl<Env> AnnotatedValue<Env, ManagedBuffer<Env::Api>> for ManagedBuffer<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.hex_expr()
}

fn into_value(self, _env: &Env) -> ManagedBuffer<Env::Api> {
self
}
}

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for BigUint<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.to_display()
}

fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
self
}
}

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for &BigUint<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.to_display()
}

fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
self.clone()
}
}

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for u64
where
Env: TxEnv,
{
fn annotation(&self, env: &Env) -> ManagedBuffer<Env::Api> {
self.into_value(env).to_display()
/// Consumes annotated value to produce actual value.
///
/// Override whenever it helps to avoid an unnecessary clone.
fn into_value(self, env: &Env) -> T {
self.to_value(env)
}

fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
BigUint::<Env::Api>::from(self)
/// Can be used when working with references only.
///
/// Override whenever it helps to avoid an unnecessary clone.
fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&T) -> R,
{
f(&self.to_value(env))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use crate::{
proxy_imports::ManagedRef,
types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer},
};

use super::{AnnotatedValue, TxEnv};

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for BigUint<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.to_display()
}

fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
self.clone()
}

fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
self
}

fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
{
f(self)
}
}

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for &BigUint<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.to_display()
}

fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
(*self).clone()
}

fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
self.clone()
}

fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
{
f(self)
}
}

impl<'a, Env> AnnotatedValue<Env, BigUint<Env::Api>> for ManagedRef<'a, Env::Api, BigUint<Env::Api>>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.to_display()
}

fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
(*self).clone_value()
}

fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
self.clone_value()
}

fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
{
f(self)
}
}

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for u64
where
Env: TxEnv,
{
fn annotation(&self, env: &Env) -> ManagedBuffer<Env::Api> {
self.to_value(env).to_display()
}

fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
BigUint::from(*self)
}
}

impl<Env> AnnotatedValue<Env, BigUint<Env::Api>> for ()
where
Env: TxEnv,
{
fn annotation(&self, env: &Env) -> ManagedBuffer<Env::Api> {
ManagedBuffer::from("0")
}

fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
BigUint::zero()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{
proxy_imports::ManagedRef,
types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer},
};

use super::{AnnotatedValue, TxEnv};

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for ManagedAddress<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.hex_expr()
}

fn to_value(&self, env: &Env) -> ManagedAddress<Env::Api> {
self.clone()
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
self
}

fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&ManagedAddress<Env::Api>) -> R,
{
f(self)
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &ManagedAddress<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.hex_expr()
}

fn to_value(&self, env: &Env) -> ManagedAddress<Env::Api> {
(*self).clone()
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
self.clone()
}

fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&ManagedAddress<Env::Api>) -> R,
{
f(self)
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for Address
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
ManagedAddress::from(self).hex_expr()
}

fn to_value(&self, env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from(self)
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &Address
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
ManagedAddress::from(*self).hex_expr()
}

fn to_value(&self, env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from(*self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::{
proxy_imports::ManagedRef,
types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer},
};

use super::{AnnotatedValue, TxEnv};

impl<Env> AnnotatedValue<Env, ManagedBuffer<Env::Api>> for ManagedBuffer<Env::Api>
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.hex_expr()
}

fn to_value(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.clone()
}

fn into_value(self, _env: &Env) -> ManagedBuffer<Env::Api> {
self
}

fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&ManagedBuffer<Env::Api>) -> R,
{
f(self)
}
}
Loading

0 comments on commit a8394b9

Please sign in to comment.