drt_sc_scenario/api/impl_vh/
debug_handle_vh.rs1use drt_chain_vm::tx_mock::{TxContext, TxContextStack};
2use drt_sc::{
3 api::{use_raw_handle, HandleConstraints, RawHandle},
4 codec::TryStaticCast,
5 types::ManagedVecItem,
6};
7use std::sync::Arc;
8
9#[derive(Clone)]
10pub struct DebugHandle {
11 pub(crate) context: Arc<TxContext>,
12 raw_handle: RawHandle,
13}
14
15impl DebugHandle {
16 pub fn is_on_current_context(&self) -> bool {
17 Arc::ptr_eq(&self.context, &TxContextStack::static_peek())
18 }
19
20 pub fn is_on_same_context(&self, other: &DebugHandle) -> bool {
21 Arc::ptr_eq(&self.context, &other.context)
22 }
23
24 pub fn assert_current_context(&self) {
25 assert!(
26 self.is_on_current_context(),
27 "Managed value not used in original context"
28 );
29 }
30}
31
32impl core::fmt::Debug for DebugHandle {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 RawHandle::fmt(&self.raw_handle, f)
35 }
36}
37
38impl HandleConstraints for DebugHandle {
39 fn new(handle: drt_sc::api::RawHandle) -> Self {
40 Self {
41 context: TxContextStack::static_peek(),
42 raw_handle: handle,
43 }
44 }
45
46 fn to_be_bytes(&self) -> [u8; 4] {
47 self.assert_current_context();
48 self.raw_handle.to_be_bytes()
49 }
50
51 fn get_raw_handle(&self) -> RawHandle {
52 self.assert_current_context();
53 self.raw_handle
54 }
55
56 fn get_raw_handle_unchecked(&self) -> RawHandle {
57 self.raw_handle
58 }
59}
60
61impl PartialEq<RawHandle> for DebugHandle {
62 fn eq(&self, other: &RawHandle) -> bool {
63 &self.raw_handle == other
64 }
65}
66
67impl PartialEq<DebugHandle> for DebugHandle {
68 fn eq(&self, other: &DebugHandle) -> bool {
69 Arc::ptr_eq(&self.context, &other.context) && self.raw_handle == other.raw_handle
70 }
71}
72
73impl From<i32> for DebugHandle {
74 fn from(handle: i32) -> Self {
75 DebugHandle::new(handle)
76 }
77}
78
79impl ManagedVecItem for DebugHandle {
80 type PAYLOAD = <RawHandle as ManagedVecItem>::PAYLOAD;
81
82 const SKIPS_RESERIALIZATION: bool = <RawHandle as ManagedVecItem>::SKIPS_RESERIALIZATION;
83
84 type Ref<'a> = Self;
85
86 fn from_byte_reader<Reader: FnMut(&mut [u8])>(reader: Reader) -> Self {
87 use_raw_handle(RawHandle::from_byte_reader(reader))
88 }
89
90 unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(
91 reader: Reader,
92 ) -> Self::Ref<'a> {
93 use_raw_handle(RawHandle::from_byte_reader(reader))
94 }
95
96 fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, writer: Writer) -> R {
97 RawHandle::to_byte_writer(&self.get_raw_handle(), writer)
98 }
99}
100
101impl TryStaticCast for DebugHandle {}