ab_contracts_common/
address.rs1use crate::ShardIndex;
2use ab_contracts_io_type::metadata::IoTypeMetadataKind;
3use ab_contracts_io_type::trivial_type::TrivialType;
4use core::cmp::Ordering;
5use core::mem::MaybeUninit;
6use core::{fmt, ptr};
7
8#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
12#[repr(C)]
13pub struct Address(u64, u64);
14
15unsafe impl TrivialType for Address {
16 const METADATA: &[u8] = &[IoTypeMetadataKind::Address as u8];
17}
18
19const _: () = {
21 let (type_details, _metadata) = IoTypeMetadataKind::type_details(Address::METADATA)
22 .expect("Statically correct metadata; qed");
23 assert!(size_of::<Address>() == type_details.recommended_capacity as usize);
24 assert!(align_of::<Address>() == type_details.alignment.get() as usize);
25};
26
27impl fmt::Debug for Address {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 f.debug_tuple("Address").field(&self.into_u128()).finish()
30 }
31}
32
33impl fmt::Display for Address {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 self.into_u128().fmt(f)
37 }
38}
39
40impl PartialEq<&Address> for Address {
41 #[inline(always)]
42 fn eq(&self, other: &&Address) -> bool {
43 self.0 == other.0
44 }
45}
46
47impl PartialEq<Address> for &Address {
48 #[inline(always)]
49 fn eq(&self, other: &Address) -> bool {
50 self.0 == other.0
51 }
52}
53
54impl Ord for Address {
55 #[inline(always)]
56 fn cmp(&self, other: &Address) -> Ordering {
57 self.into_u128().cmp(&other.into_u128())
58 }
59}
60
61impl PartialOrd for Address {
62 #[inline(always)]
63 fn partial_cmp(&self, other: &Address) -> Option<Ordering> {
64 Some(self.cmp(other))
65 }
66}
67
68impl From<u128> for Address {
69 #[inline(always)]
70 fn from(value: u128) -> Self {
71 Self::from_u128(value)
72 }
73}
74
75impl From<Address> for u128 {
76 #[inline(always)]
77 fn from(value: Address) -> Self {
78 value.into_u128()
79 }
80}
81
82impl Address {
85 pub const NULL: Self = Self::from_u128(0);
88 pub const SYSTEM_CODE: Self = Self::from_u128(1);
90 pub const SYSTEM_BLOCK: Self = Self::from_u128(2);
92 pub const SYSTEM_STATE: Self = Self::from_u128(3);
94 pub const SYSTEM_NATIVE_TOKEN: Self = Self::from_u128(4);
96 pub const SYSTEM_SIMPLE_WALLET_BASE: Self = Self::from_u128(10);
98
99 #[inline(always)]
101 const fn into_u128(self) -> u128 {
102 unsafe { ptr::from_ref(&self).cast::<u128>().read_unaligned() }
104 }
105
106 #[inline(always)]
108 const fn from_u128(n: u128) -> Self {
109 let mut result = MaybeUninit::<Self>::uninit();
110 unsafe {
112 result.as_mut_ptr().cast::<u128>().write_unaligned(n);
113 result.assume_init()
114 }
115 }
116
117 #[inline(always)]
119 pub const fn system_address_allocator(shard_index: ShardIndex) -> Self {
120 Self::from_u128(shard_index.to_u32() as u128 * ShardIndex::MAX_ADDRESSES_PER_SHARD.get())
124 }
125}