multiversx_sc_scenario/api/impl_vh/
debug_handle_vh.rs1use std::sync::Arc;
2
3use multiversx_chain_vm::host::context::TxContext;
4use multiversx_sc::{
5 api::{HandleConstraints, RawHandle},
6 codec::TryStaticCast,
7};
8
9use crate::executor::debug::ContractDebugStack;
10
11#[derive(Clone)]
12pub struct DebugHandle {
13 pub(crate) context: Arc<TxContext>,
16 raw_handle: RawHandle,
17}
18
19impl DebugHandle {
20 pub fn is_on_current_context(&self) -> bool {
21 Arc::ptr_eq(
22 &self.context,
23 &ContractDebugStack::static_peek().tx_context_ref.into_ref(),
24 )
25 }
26
27 pub fn is_on_same_context(&self, other: &DebugHandle) -> bool {
28 Arc::ptr_eq(&self.context, &other.context)
29 }
30
31 pub fn assert_current_context(&self) {
32 assert!(
33 self.is_on_current_context(),
34 "Managed value not used in original context"
35 );
36 }
37}
38
39impl core::fmt::Debug for DebugHandle {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 RawHandle::fmt(&self.raw_handle, f)
42 }
43}
44
45impl HandleConstraints for DebugHandle {
46 fn new(handle: multiversx_sc::api::RawHandle) -> Self {
47 Self {
48 context: ContractDebugStack::static_peek().tx_context_ref.into_ref(),
49 raw_handle: handle,
50 }
51 }
52
53 fn to_be_bytes(&self) -> [u8; 4] {
54 self.assert_current_context();
55 self.raw_handle.to_be_bytes()
56 }
57
58 fn get_raw_handle(&self) -> RawHandle {
59 self.assert_current_context();
60 self.raw_handle
61 }
62
63 fn get_raw_handle_unchecked(&self) -> RawHandle {
64 self.raw_handle
65 }
66}
67
68impl PartialEq<RawHandle> for DebugHandle {
69 fn eq(&self, other: &RawHandle) -> bool {
70 &self.raw_handle == other
71 }
72}
73
74impl PartialEq<DebugHandle> for DebugHandle {
75 fn eq(&self, other: &DebugHandle) -> bool {
76 Arc::ptr_eq(&self.context, &other.context) && self.raw_handle == other.raw_handle
77 }
78}
79
80impl From<i32> for DebugHandle {
81 fn from(handle: i32) -> Self {
82 DebugHandle::new(handle)
83 }
84}
85
86impl TryStaticCast for DebugHandle {}