multiversx_sc/types/interaction/
annotated.rs

1mod annotated_impl_big_uint;
2mod annotated_impl_managed_address;
3mod annotated_impl_managed_buffer;
4mod annotated_impl_token_identifier;
5mod annotated_impl_u64;
6
7use crate::{
8    api::ManagedTypeApi,
9    formatter::FormatBuffer,
10    types::{ManagedBuffer, ManagedBufferCachedBuilder},
11};
12
13use super::TxEnv;
14
15/// Describes a value can also have a custom representation in a mandos scenario.
16///
17/// It is based on managed types in order to be embedded into parametric tests too.
18pub trait AnnotatedValue<Env, T>: Sized
19where
20    Env: TxEnv,
21{
22    fn annotation(&self, env: &Env) -> ManagedBuffer<Env::Api>;
23
24    /// Produces the value from a reference of the annotated type. Might involve a `.clone()` in some cases.
25    fn to_value(&self, env: &Env) -> T;
26
27    /// Consumes annotated value to produce actual value.
28    ///
29    /// Override whenever it helps to avoid an unnecessary clone.
30    fn into_value(self, env: &Env) -> T {
31        self.to_value(env)
32    }
33
34    /// Can be used when working with references only.
35    ///
36    /// Override whenever it helps to avoid an unnecessary clone.
37    fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
38    where
39        F: FnOnce(&T) -> R,
40    {
41        f(&self.to_value(env))
42    }
43}
44
45/// Useful for u64 display in several places.
46pub(super) fn display_u64<Api>(n: u64) -> ManagedBuffer<Api>
47where
48    Api: ManagedTypeApi,
49{
50    let mut result = ManagedBufferCachedBuilder::new_from_slice(&[]);
51    result.append_display(&n);
52    result.into_managed_buffer()
53}