multiversx_sc/types/interaction/
annotated.rs

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