-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathuniversal_contract_obj.rs
More file actions
71 lines (61 loc) · 1.75 KB
/
universal_contract_obj.rs
File metadata and controls
71 lines (61 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use core::{cell::UnsafeCell, marker::PhantomData};
use crate::api::{const_handles, RawHandle, VMApi};
use super::{CallValueWrapper, ContractBase};
pub struct ContractObjData {
pub call_value_egld_handle: RawHandle,
pub call_value_multi_esdt_handle: RawHandle,
}
impl Default for ContractObjData {
fn default() -> Self {
ContractObjData {
call_value_egld_handle: const_handles::UNINITIALIZED_HANDLE,
call_value_multi_esdt_handle: const_handles::UNINITIALIZED_HANDLE,
}
}
}
/// A unique empty structure that automatically implements all smart contract traits.
///
/// The smart contract macros will automatically also generate trait implementations for this type. These include:
/// - the contract trait
/// - the `AutoImpl` trait
/// - the `EndpointWrappers` trait
///
/// When generating WASM, this contract implementation is used.
/// This makes sure no monomorphization-induced code duplication occurs in relation to modules.
pub struct UniversalContractObj<A>
where
A: VMApi,
{
_phantom: PhantomData<A>,
pub data: UnsafeCell<ContractObjData>,
}
unsafe impl<A> Sync for UniversalContractObj<A> where A: VMApi {}
unsafe impl<A> Send for UniversalContractObj<A> where A: VMApi {}
impl<A> UniversalContractObj<A>
where
A: VMApi,
{
pub fn new() -> Self {
Self {
_phantom: PhantomData,
data: UnsafeCell::new(ContractObjData::default()),
}
}
}
impl<A> Default for UniversalContractObj<A>
where
A: VMApi,
{
fn default() -> Self {
Self::new()
}
}
impl<A> ContractBase for UniversalContractObj<A>
where
A: VMApi,
{
type Api = A;
fn call_value(&self) -> CallValueWrapper<'_, Self::Api> {
CallValueWrapper::new(&self.data)
}
}