-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1510 from multiversx/annotation-ref
Annotation system refactor
- Loading branch information
Showing
27 changed files
with
332 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
framework/base/src/types/interaction/annotated/annotated_impl_managed_address.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
framework/base/src/types/interaction/annotated/annotated_impl_managed_buffer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.