Skip to content

Commit

Permalink
✨ Add safeCastTo24
Browse files Browse the repository at this point in the history
  • Loading branch information
transmissions11 committed Aug 3, 2022
1 parent 47348f6 commit 478f9d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
13 changes: 7 additions & 6 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,20 @@ SSTORE2Test:testWriteReadFullBoundedRead() (gas: 53672)
SSTORE2Test:testWriteReadFullStartBound() (gas: 34764)
SafeCastLibTest:testFailSafeCastTo128() (gas: 321)
SafeCastLibTest:testFailSafeCastTo160() (gas: 342)
SafeCastLibTest:testFailSafeCastTo192() (gas: 344)
SafeCastLibTest:testFailSafeCastTo192() (gas: 322)
SafeCastLibTest:testFailSafeCastTo224() (gas: 343)
SafeCastLibTest:testFailSafeCastTo248() (gas: 365)
SafeCastLibTest:testFailSafeCastTo32() (gas: 364)
SafeCastLibTest:testFailSafeCastTo64() (gas: 321)
SafeCastLibTest:testFailSafeCastTo8() (gas: 296)
SafeCastLibTest:testFailSafeCastTo8() (gas: 341)
SafeCastLibTest:testFailSafeCastTo96() (gas: 321)
SafeCastLibTest:testSafeCastTo128() (gas: 449)
SafeCastLibTest:testSafeCastTo128() (gas: 472)
SafeCastLibTest:testSafeCastTo160() (gas: 470)
SafeCastLibTest:testSafeCastTo192() (gas: 471)
SafeCastLibTest:testSafeCastTo192() (gas: 449)
SafeCastLibTest:testSafeCastTo224() (gas: 491)
SafeCastLibTest:testSafeCastTo248() (gas: 427)
SafeCastLibTest:testSafeCastTo32() (gas: 471)
SafeCastLibTest:testSafeCastTo24() (gas: 470)
SafeCastLibTest:testSafeCastTo248() (gas: 450)
SafeCastLibTest:testSafeCastTo32() (gas: 449)
SafeCastLibTest:testSafeCastTo64() (gas: 470)
SafeCastLibTest:testSafeCastTo8() (gas: 469)
SafeCastLibTest:testSafeCastTo96() (gas: 469)
Expand Down
11 changes: 11 additions & 0 deletions src/test/SafeCastLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ contract SafeCastLibTest is DSTestPlus {
assertEq(SafeCastLib.safeCastTo32(2.5e7), 2.5e7);
}

function testSafeCastTo24() public {
assertEq(SafeCastLib.safeCastTo24(2.5e4), 2.5e4);
assertEq(SafeCastLib.safeCastTo24(2.5e3), 2.5e3);
}

function testSafeCastTo8() public {
assertEq(SafeCastLib.safeCastTo8(100), 100);
assertEq(SafeCastLib.safeCastTo8(250), 250);
Expand Down Expand Up @@ -189,6 +194,12 @@ contract SafeCastLibTest is DSTestPlus {
SafeCastLib.safeCastTo32(x);
}

function testFailSafeCastTo24(uint256 x) public {
x = bound(x, type(uint24).max + 1, type(uint256).max);

SafeCastLib.safeCastTo24(x);
}

function testFailSafeCastTo8(uint256 x) public {
x = bound(x, type(uint8).max + 1, type(uint256).max);

Expand Down
6 changes: 6 additions & 0 deletions src/utils/SafeCastLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ library SafeCastLib {
y = uint32(x);
}

function safeCastTo24(uint256 x) internal pure returns (uint24 y) {
require(x < 1 << 24);

y = uint24(x);
}

function safeCastTo8(uint256 x) internal pure returns (uint8 y) {
require(x < 1 << 8);

Expand Down

0 comments on commit 478f9d0

Please sign in to comment.