Skip to content

Commit 17bc2da

Browse files
authored
Procedurally generate EnumerableSet and EnumerableMap (#3429)
1 parent c797195 commit 17bc2da

9 files changed

+708
-15
lines changed

contracts/mocks/EnumerableMapMock.sol

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ contract AddressToUintMapMock {
9090
}
9191
}
9292

93+
// Bytes32ToBytes32Map
9394
contract Bytes32ToBytes32MapMock {
9495
using EnumerableMap for EnumerableMap.Bytes32ToBytes32Map;
9596

contracts/utils/structs/EnumerableMap.sol

+11-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import "./EnumerableSet.sol";
3030
*
3131
* - `uint256 -> address` (`UintToAddressMap`) since v3.0.0
3232
* - `address -> uint256` (`AddressToUintMap`) since v4.6.0
33-
* - `bytes32 -> bytes32` (`Bytes32ToBytes32`) since v4.6.0
33+
* - `bytes32 -> bytes32` (`Bytes32ToBytes32Map`) since v4.6.0
3434
* - `uint256 -> uint256` (`UintToUintMap`) since v4.7.0
3535
* - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
3636
*
@@ -116,7 +116,7 @@ library EnumerableMap {
116116
}
117117

118118
/**
119-
* @dev Tries to returns the value associated with `key`. O(1).
119+
* @dev Tries to returns the value associated with `key`. O(1).
120120
* Does not revert if `key` is not in the map.
121121
*/
122122
function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool, bytes32) {
@@ -129,7 +129,7 @@ library EnumerableMap {
129129
}
130130

131131
/**
132-
* @dev Returns the value associated with `key`. O(1).
132+
* @dev Returns the value associated with `key`. O(1).
133133
*
134134
* Requirements:
135135
*
@@ -216,7 +216,7 @@ library EnumerableMap {
216216
}
217217

218218
/**
219-
* @dev Tries to returns the value associated with `key`. O(1).
219+
* @dev Tries to returns the value associated with `key`. O(1).
220220
* Does not revert if `key` is not in the map.
221221
*/
222222
function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
@@ -225,7 +225,7 @@ library EnumerableMap {
225225
}
226226

227227
/**
228-
* @dev Returns the value associated with `key`. O(1).
228+
* @dev Returns the value associated with `key`. O(1).
229229
*
230230
* Requirements:
231231
*
@@ -308,18 +308,16 @@ library EnumerableMap {
308308
}
309309

310310
/**
311-
* @dev Tries to returns the value associated with `key`. O(1).
311+
* @dev Tries to returns the value associated with `key`. O(1).
312312
* Does not revert if `key` is not in the map.
313-
*
314-
* _Available since v3.4._
315313
*/
316314
function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
317315
(bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
318316
return (success, address(uint160(uint256(value))));
319317
}
320318

321319
/**
322-
* @dev Returns the value associated with `key`. O(1).
320+
* @dev Returns the value associated with `key`. O(1).
323321
*
324322
* Requirements:
325323
*
@@ -402,7 +400,7 @@ library EnumerableMap {
402400
}
403401

404402
/**
405-
* @dev Tries to returns the value associated with `key`. O(1).
403+
* @dev Tries to returns the value associated with `key`. O(1).
406404
* Does not revert if `key` is not in the map.
407405
*/
408406
function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
@@ -411,7 +409,7 @@ library EnumerableMap {
411409
}
412410

413411
/**
414-
* @dev Returns the value associated with `key`. O(1).
412+
* @dev Returns the value associated with `key`. O(1).
415413
*
416414
* Requirements:
417415
*
@@ -494,7 +492,7 @@ library EnumerableMap {
494492
}
495493

496494
/**
497-
* @dev Tries to returns the value associated with `key`. O(1).
495+
* @dev Tries to returns the value associated with `key`. O(1).
498496
* Does not revert if `key` is not in the map.
499497
*/
500498
function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
@@ -503,7 +501,7 @@ library EnumerableMap {
503501
}
504502

505503
/**
506-
* @dev Returns the value associated with `key`. O(1).
504+
* @dev Returns the value associated with `key`. O(1).
507505
*
508506
* Requirements:
509507
*

contracts/utils/structs/EnumerableSet.sol

+10-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,15 @@ library EnumerableSet {
214214
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
215215
*/
216216
function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
217-
return _values(set._inner);
217+
bytes32[] memory store = _values(set._inner);
218+
bytes32[] memory result;
219+
220+
/// @solidity memory-safe-assembly
221+
assembly {
222+
result := store
223+
}
224+
225+
return result;
218226
}
219227

220228
// AddressSet
@@ -325,7 +333,7 @@ library EnumerableSet {
325333
}
326334

327335
/**
328-
* @dev Returns the number of values on the set. O(1).
336+
* @dev Returns the number of values in the set. O(1).
329337
*/
330338
function length(UintSet storage set) internal view returns (uint256) {
331339
return _length(set._inner);

scripts/generate/run.js

+7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ function getVersion (path) {
1414
}
1515

1616
for (const [ file, template ] of Object.entries({
17+
// SafeCast
1718
'utils/math/SafeCast.sol': './templates/SafeCast',
1819
'mocks/SafeCastMock.sol': './templates/SafeCastMock',
20+
// EnumerableSet
21+
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet',
22+
'mocks/EnumerableSetMock.sol': './templates/EnumerableSetMock',
23+
// EnumerableMap
24+
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap',
25+
'mocks/EnumerableMapMock.sol': './templates/EnumerableMapMock',
1926
})) {
2027
const path = `./contracts/${file}`;
2128
const version = getVersion(path);

0 commit comments

Comments
 (0)