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
use core::fmt::Debug;

pub trait HandleTypeInfo {
    type ManagedBufferHandle: HandleConstraints;
    type BigIntHandle: HandleConstraints;
    type BigFloatHandle: HandleConstraints;
    type EllipticCurveHandle: HandleConstraints;
}

use elrond_codec::TryStaticCast;

use crate::{
    api::{ErrorApi, ErrorApiImpl},
    types::ManagedVecItem,
};

pub type RawHandle = i32;

pub trait HandleConstraints:
    ManagedVecItem + TryStaticCast + Debug + Clone + From<RawHandle> + PartialEq + PartialEq<RawHandle>
{
    fn new(handle: RawHandle) -> Self;
    fn to_be_bytes(&self) -> [u8; 4];
    fn get_raw_handle(&self) -> RawHandle;

    fn cast_or_signal_error<E: ErrorApi, U: TryStaticCast>(self) -> U {
        if let Some(other) = self.try_cast() {
            other
        } else {
            E::error_api_impl().signal_error(b"Cast type mismatch")
        }
    }
}

pub fn use_raw_handle<H>(handle: RawHandle) -> H
where
    H: HandleConstraints,
{
    H::new(handle)
}

impl HandleConstraints for i32 {
    fn new(handle: RawHandle) -> Self {
        handle
    }

    fn to_be_bytes(&self) -> [u8; 4] {
        i32::to_be_bytes(*self)
    }

    fn get_raw_handle(&self) -> RawHandle {
        *self
    }
}

pub fn handle_to_be_bytes<H: HandleConstraints>(handle: H) -> [u8; 4] {
    HandleConstraints::to_be_bytes(&handle)
}