forked from Philogy/transient-goodies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransientPrimitives.sol
99 lines (81 loc) · 2.55 KB
/
TransientPrimitives.sol
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/// @author philogy <https://github.com/philogy>
struct tuint256 {
uint256 __placeholder;
}
struct tint256 {
uint256 __placeholder;
}
struct tbytes32 {
uint256 __placeholder;
}
struct taddress {
uint256 __placeholder;
}
using TransientPrimitivesLib for tuint256 global;
using TransientPrimitivesLib for tint256 global;
using TransientPrimitivesLib for tbytes32 global;
using TransientPrimitivesLib for taddress global;
library TransientPrimitivesLib {
function get(tuint256 storage ptr) internal view returns (uint256 value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(ptr.slot)
}
}
function get(tint256 storage ptr) internal view returns (int256 value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(ptr.slot)
}
}
function get(tbytes32 storage ptr) internal view returns (bytes32 value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(ptr.slot)
}
}
function get(taddress storage ptr) internal view returns (address value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(ptr.slot)
}
}
function set(tuint256 storage ptr, uint256 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(ptr.slot, value)
}
}
function inc(tuint256 storage ptr, uint256 change) internal returns (uint256 newValue) {
ptr.set(newValue = ptr.get() + change);
}
function dec(tuint256 storage ptr, uint256 change) internal returns (uint256 newValue) {
ptr.set(newValue = ptr.get() - change);
}
function set(tint256 storage ptr, int256 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(ptr.slot, value)
}
}
function inc(tint256 storage ptr, int256 change) internal returns (int256 newValue) {
ptr.set(newValue = ptr.get() + change);
}
function dec(tint256 storage ptr, int256 change) internal returns (int256 newValue) {
ptr.set(newValue = ptr.get() - change);
}
function set(tbytes32 storage ptr, bytes32 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(ptr.slot, value)
}
}
function set(taddress storage ptr, address value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(ptr.slot, value)
}
}
}