zink/primitives/
address.rs1use crate::{
2 ffi,
3 primitives::{Bytes20, Bytes32},
4 storage::{StorageValue, TransientStorageValue},
5 Asm,
6};
7
8#[repr(C)]
10#[derive(Clone, Copy)]
11pub struct Address(Bytes20);
12
13impl Address {
14 pub const fn empty() -> Self {
16 Address(Bytes20::empty())
17 }
18
19 #[inline(always)]
21 pub fn caller() -> Self {
22 unsafe { ffi::evm::caller() }
23 }
24
25 #[allow(clippy::should_implement_trait)]
29 #[inline(always)]
30 pub fn eq(self, other: Self) -> bool {
31 self.0.eq(other.0)
32 }
33
34 pub fn to_bytes32(&self) -> Bytes32 {
36 #[cfg(target_family = "wasm")]
37 {
38 Bytes32(self.0 .0) }
40 #[cfg(not(target_family = "wasm"))]
41 {
42 let mut bytes = [0u8; 32];
43 bytes[12..32].copy_from_slice(&self.0 .0); Bytes32(bytes)
45 }
46 }
47
48 #[cfg(not(target_family = "wasm"))]
49 pub fn bytes32(&self) -> [u8; 32] {
50 self.to_bytes32().0 }
52}
53
54impl Asm for Address {
55 fn push(self) {
56 unsafe { ffi::bytes::push_bytes20(self.0) }
57 }
58
59 #[cfg(not(target_family = "wasm"))]
60 fn bytes32(&self) -> [u8; 32] {
61 self.bytes32()
62 }
63}
64
65impl StorageValue for Address {
66 fn sload() -> Self {
67 Self(unsafe { ffi::bytes::sload_bytes20() })
68 }
69}
70
71impl From<Bytes20> for Address {
72 fn from(value: Bytes20) -> Self {
73 Address(value)
74 }
75}
76
77#[cfg(not(target_family = "wasm"))]
78impl From<[u8; 20]> for Address {
79 fn from(value: [u8; 20]) -> Self {
80 Address(Bytes20(value))
81 }
82}
83
84impl TransientStorageValue for Address {
85 fn tload() -> Self {
86 Address(unsafe { ffi::bytes::tload_bytes20() })
87 }
88}