33
44pragma solidity ^ 0.8.24 ;
55
6+ import {TypedSlot} from "./types/TypedSlot.sol " ;
7+
68/**
79 * @dev Library for reading and writing primitive types to specific storage slots. This is a variant of {StorageSlot}
810 * that supports transient storage.
@@ -12,6 +14,7 @@ pragma solidity ^0.8.24;
1214 * Example usage:
1315 * ```solidity
1416 * contract ReentrancyGuard {
17+ * using TypedSlot for bytes32;
1518 * using TransientSlot for *;
1619 *
1720 * bytes32 internal constant _REENTRANCY_SLOT = 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
@@ -27,22 +30,10 @@ pragma solidity ^0.8.24;
2730 * ```
2831 */
2932library TransientSlot {
30- /**
31- * @dev UDVT that represent a slot holding a address.
32- */
33- type AddressSlotType is bytes32 ;
34-
35- /**
36- * @dev Cast an arbitrary slot to a AddressSlotType.
37- */
38- function asAddressSlot (bytes32 slot ) internal pure returns (AddressSlotType) {
39- return AddressSlotType.wrap (slot);
40- }
41-
4233 /**
4334 * @dev Load the value held at location `slot` in transient storage.
4435 */
45- function tload (AddressSlotType slot ) internal view returns (address value ) {
36+ function tload (TypedSlot. AddressSlotType slot ) internal view returns (address value ) {
4637 /// @solidity memory-safe-assembly
4738 assembly {
4839 value := tload (slot)
@@ -52,29 +43,17 @@ library TransientSlot {
5243 /**
5344 * @dev Store `value` at location `slot` in transient storage.
5445 */
55- function tstore (AddressSlotType slot , address value ) internal {
46+ function tstore (TypedSlot. AddressSlotType slot , address value ) internal {
5647 /// @solidity memory-safe-assembly
5748 assembly {
5849 tstore (slot, value)
5950 }
6051 }
6152
62- /**
63- * @dev UDVT that represent a slot holding a bool.
64- */
65- type BooleanSlotType is bytes32 ;
66-
67- /**
68- * @dev Cast an arbitrary slot to a BooleanSlotType.
69- */
70- function asBooleanSlot (bytes32 slot ) internal pure returns (BooleanSlotType) {
71- return BooleanSlotType.wrap (slot);
72- }
73-
7453 /**
7554 * @dev Load the value held at location `slot` in transient storage.
7655 */
77- function tload (BooleanSlotType slot ) internal view returns (bool value ) {
56+ function tload (TypedSlot. BooleanSlotType slot ) internal view returns (bool value ) {
7857 /// @solidity memory-safe-assembly
7958 assembly {
8059 value := tload (slot)
@@ -84,29 +63,17 @@ library TransientSlot {
8463 /**
8564 * @dev Store `value` at location `slot` in transient storage.
8665 */
87- function tstore (BooleanSlotType slot , bool value ) internal {
66+ function tstore (TypedSlot. BooleanSlotType slot , bool value ) internal {
8867 /// @solidity memory-safe-assembly
8968 assembly {
9069 tstore (slot, value)
9170 }
9271 }
9372
94- /**
95- * @dev UDVT that represent a slot holding a bytes32.
96- */
97- type Bytes32SlotType is bytes32 ;
98-
99- /**
100- * @dev Cast an arbitrary slot to a Bytes32SlotType.
101- */
102- function asBytes32Slot (bytes32 slot ) internal pure returns (Bytes32SlotType) {
103- return Bytes32SlotType.wrap (slot);
104- }
105-
10673 /**
10774 * @dev Load the value held at location `slot` in transient storage.
10875 */
109- function tload (Bytes32SlotType slot ) internal view returns (bytes32 value ) {
76+ function tload (TypedSlot. Bytes32SlotType slot ) internal view returns (bytes32 value ) {
11077 /// @solidity memory-safe-assembly
11178 assembly {
11279 value := tload (slot)
@@ -116,29 +83,17 @@ library TransientSlot {
11683 /**
11784 * @dev Store `value` at location `slot` in transient storage.
11885 */
119- function tstore (Bytes32SlotType slot , bytes32 value ) internal {
86+ function tstore (TypedSlot. Bytes32SlotType slot , bytes32 value ) internal {
12087 /// @solidity memory-safe-assembly
12188 assembly {
12289 tstore (slot, value)
12390 }
12491 }
12592
126- /**
127- * @dev UDVT that represent a slot holding a uint256.
128- */
129- type Uint256SlotType is bytes32 ;
130-
131- /**
132- * @dev Cast an arbitrary slot to a Uint256SlotType.
133- */
134- function asUint256Slot (bytes32 slot ) internal pure returns (Uint256SlotType) {
135- return Uint256SlotType.wrap (slot);
136- }
137-
13893 /**
13994 * @dev Load the value held at location `slot` in transient storage.
14095 */
141- function tload (Uint256SlotType slot ) internal view returns (uint256 value ) {
96+ function tload (TypedSlot. Uint256SlotType slot ) internal view returns (uint256 value ) {
14297 /// @solidity memory-safe-assembly
14398 assembly {
14499 value := tload (slot)
@@ -148,29 +103,17 @@ library TransientSlot {
148103 /**
149104 * @dev Store `value` at location `slot` in transient storage.
150105 */
151- function tstore (Uint256SlotType slot , uint256 value ) internal {
106+ function tstore (TypedSlot. Uint256SlotType slot , uint256 value ) internal {
152107 /// @solidity memory-safe-assembly
153108 assembly {
154109 tstore (slot, value)
155110 }
156111 }
157112
158- /**
159- * @dev UDVT that represent a slot holding a int256.
160- */
161- type Int256SlotType is bytes32 ;
162-
163- /**
164- * @dev Cast an arbitrary slot to a Int256SlotType.
165- */
166- function asInt256Slot (bytes32 slot ) internal pure returns (Int256SlotType) {
167- return Int256SlotType.wrap (slot);
168- }
169-
170113 /**
171114 * @dev Load the value held at location `slot` in transient storage.
172115 */
173- function tload (Int256SlotType slot ) internal view returns (int256 value ) {
116+ function tload (TypedSlot. Int256SlotType slot ) internal view returns (int256 value ) {
174117 /// @solidity memory-safe-assembly
175118 assembly {
176119 value := tload (slot)
@@ -180,7 +123,7 @@ library TransientSlot {
180123 /**
181124 * @dev Store `value` at location `slot` in transient storage.
182125 */
183- function tstore (Int256SlotType slot , int256 value ) internal {
126+ function tstore (TypedSlot. Int256SlotType slot , int256 value ) internal {
184127 /// @solidity memory-safe-assembly
185128 assembly {
186129 tstore (slot, value)
0 commit comments