Skip to main content

calimero_sys/types/
pointer.rs

1use core::marker::PhantomData;
2use core::ptr;
3
4#[cfg(target_arch = "wasm32")]
5mod guest;
6
7#[cfg(not(target_arch = "wasm32"))]
8mod host;
9
10#[repr(C)]
11#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub struct PtrSizedInt {
13    value: u64,
14}
15
16impl PtrSizedInt {
17    pub const MAX: Self = Self { value: u64::MAX };
18
19    #[inline]
20    pub const fn new(value: usize) -> Self {
21        Self {
22            value: value as u64,
23        }
24    }
25
26    #[inline]
27    pub const fn as_usize(self) -> usize {
28        self.value as usize
29    }
30}
31
32impl From<usize> for PtrSizedInt {
33    #[inline]
34    fn from(value: usize) -> Self {
35        Self::new(value)
36    }
37}
38
39#[repr(C)]
40#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
41pub struct Pointer<T> {
42    value: PtrSizedInt,
43    _phantom: PhantomData<T>,
44}
45
46impl<T> Pointer<T> {
47    #[inline]
48    pub fn null() -> Self {
49        Self::new(ptr::null())
50    }
51}
52
53impl<T> From<*const T> for Pointer<T> {
54    #[inline]
55    fn from(ptr: *const T) -> Self {
56        Self::new(ptr)
57    }
58}
59
60impl<T> From<*mut T> for Pointer<T> {
61    #[inline]
62    fn from(ptr: *mut T) -> Self {
63        Self::new(ptr)
64    }
65}