novax_data/types/
managed.rs

1multiversx_sc::imports!();
2multiversx_sc::derive_imports!();
3use multiversx_sc_codec::TopEncodeMulti;
4
5/// Provides a bridge from common Rust types to complex smart contract types
6/// managed by the MultiversX virtual machine.
7///
8/// This trait facilitates the conversion from native Rust types to managed types.
9/// Unlike `NativeConvertible`, this bridge establishes a many-to-many relationship,
10/// allowing multiple Rust types to be converted to multiple managed types.
11///
12/// Implementing this trait requires specifying the managed type parameter `M` and
13/// providing an implementation for the `to_managed` method, which will carry out
14/// the actual conversion.
15///
16/// # Type Parameters
17/// - `M`: The managed type to which the native Rust type will be converted,
18///        constrained by the `TopEncodeMulti` trait.
19///
20/// # Methods
21/// - `to_managed`: Performs the conversion from the native Rust type to the specified
22///                 managed type.
23///
24/// # Examples
25///
26/// - `String` can be converted to `ManagedBuffer`, `TokenIdentifier`, or `ManagedAddress`.
27/// - `Vec` can be converted to `ManagedVec`.
28///
29/// ```rust
30/// # use novax_data::ManagedConvertible;
31/// # use multiversx_sc::types::ManagedBuffer;
32/// # use multiversx_sc_scenario::api::StaticApi;
33///
34/// struct YourStruct(String);
35///
36/// impl ManagedConvertible<ManagedBuffer<StaticApi>> for YourStruct {
37///     fn to_managed(&self) -> ManagedBuffer<StaticApi> {
38///         ManagedBuffer::from(&*self.0)
39///     }
40/// }
41/// ```
42pub trait ManagedConvertible<M: TopEncodeMulti> {
43    /// Converts the native Rust type to the specified managed type.
44    fn to_managed(&self) -> M;
45}
46
47
48macro_rules! managed_convertible_impl_self {
49    ($($type_name:ident )+) => {
50        $(
51            impl ManagedConvertible<$type_name> for $type_name {
52                fn to_managed(&self) -> $type_name {
53                    self.clone()
54                }
55            }
56        )+
57    }
58}
59
60pub(crate) use managed_convertible_impl_self;