12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- pragma solidity ^0.5.0;
- /**
- * @dev Wrappers over Solidity's uintXX casting operators with added overflow
- * checks.
- *
- * Downcasting from uint256 in Solidity does not revert on overflow. This can
- * easily result in undesired exploitation or bugs, since developers usually
- * assume that overflows raise errors. `SafeCast` restores this intuition by
- * reverting the transaction when such an operation overflows.
- *
- * Using this library instead of the unchecked operations eliminates an entire
- * class of bugs, so it's recommended to use it always.
- *
- * Can be combined with {SafeMath} to extend it to smaller types, by performing
- * all math on `uint256` and then downcasting.
- *
- * _Available since v2.5.0._
- */
- library SafeCast {
- /**
- * @dev Returns the downcasted uint128 from uint256, reverting on
- * overflow (when the input is greater than largest uint128).
- *
- * Counterpart to Solidity's `uint128` operator.
- *
- * Requirements:
- *
- * - input must fit into 128 bits
- */
- function toUint128(uint256 value) internal pure returns (uint128) {
- require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits");
- return uint128(value);
- }
- /**
- * @dev Returns the downcasted uint64 from uint256, reverting on
- * overflow (when the input is greater than largest uint64).
- *
- * Counterpart to Solidity's `uint64` operator.
- *
- * Requirements:
- *
- * - input must fit into 64 bits
- */
- function toUint64(uint256 value) internal pure returns (uint64) {
- require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits");
- return uint64(value);
- }
- /**
- * @dev Returns the downcasted uint32 from uint256, reverting on
- * overflow (when the input is greater than largest uint32).
- *
- * Counterpart to Solidity's `uint32` operator.
- *
- * Requirements:
- *
- * - input must fit into 32 bits
- */
- function toUint32(uint256 value) internal pure returns (uint32) {
- require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits");
- return uint32(value);
- }
- /**
- * @dev Returns the downcasted uint16 from uint256, reverting on
- * overflow (when the input is greater than largest uint16).
- *
- * Counterpart to Solidity's `uint16` operator.
- *
- * Requirements:
- *
- * - input must fit into 16 bits
- */
- function toUint16(uint256 value) internal pure returns (uint16) {
- require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits");
- return uint16(value);
- }
- /**
- * @dev Returns the downcasted uint8 from uint256, reverting on
- * overflow (when the input is greater than largest uint8).
- *
- * Counterpart to Solidity's `uint8` operator.
- *
- * Requirements:
- *
- * - input must fit into 8 bits.
- */
- function toUint8(uint256 value) internal pure returns (uint8) {
- require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits");
- return uint8(value);
- }
- }
|