Skip to content

Commit

Permalink
split pop into pop and popGet
Browse files Browse the repository at this point in the history
  • Loading branch information
ZumZoom committed Mar 21, 2024
1 parent b62d227 commit acba39c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion contracts/libraries/AddressArray.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,29 @@ library AddressArray {
}

/// @dev Array pop back operation for storage `self`.
function pop(Data storage self) internal returns(address res) {
function pop(Data storage self) internal {
bytes4 err = PopFromEmptyArray.selector;
/// @solidity memory-safe-assembly
assembly { // solhint-disable-line no-inline-assembly
let lengthAndFirst := sload(self.slot)
let len := shr(_LENGTH_OFFSET, and(lengthAndFirst, _LENGTH_MASK))

switch len
case 0 {
mstore(0, err)
revert(0, 4)
}
case 1 {
sstore(self.slot, _ZERO_ADDRESS)
}
default {
sstore(self.slot, sub(lengthAndFirst, _ONE_LENGTH))
}
}
}

/// @dev Array pop back operation for storage `self` that returns popped element.
function popGet(Data storage self) internal returns(address res) {
bytes4 err = PopFromEmptyArray.selector;
/// @solidity memory-safe-assembly
assembly { // solhint-disable-line no-inline-assembly
Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/AddressSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ library AddressSet {
return false;
}

address lastItem = s.items.pop();
address lastItem = s.items.popGet();
if (index < s.items.length()) {
unchecked {
s.items.set(index - 1, lastItem);
Expand Down

0 comments on commit acba39c

Please sign in to comment.