pub struct Gas(/* private fields */);Expand description
Wrapper type over near_gas::NearGas, which itself is a wrapper type over u64.
This wrapper exists to maintain JSON RPC compatibility. While NearGas
serializes to a JSON string for precision, we need to continue serializing
Gas values to JSON numbers for backward compatibility with existing clients.
Note: NearGas deserialization already handles both JSON numbers and JSON
strings, so we don’t need to redefine deserialization behavior here.
Implementations§
Source§impl Gas
impl Gas
Sourcepub const fn from_teragas(inner: u64) -> Self
pub const fn from_teragas(inner: u64) -> Self
Creates a new Gas from the specified number of whole tera Gas.
§Examples
use near_primitives_core::gas::Gas;
let tera_gas = Gas::from_teragas(5);
assert_eq!(tera_gas.as_gas(), 5 * 1_000_000_000_000);Sourcepub const fn from_gigagas(inner: u64) -> Self
pub const fn from_gigagas(inner: u64) -> Self
Creates a new Gas from the specified number of whole giga Gas.
§Examples
use near_primitives_core::gas::Gas;
let giga_gas = Gas::from_gigagas(5);
assert_eq!(giga_gas.as_gas(), 5 * 1_000_000_000);Sourcepub const fn from_gas(inner: u64) -> Self
pub const fn from_gas(inner: u64) -> Self
Creates a new Gas from the specified number of whole Gas.
§Examples
use near_primitives_core::gas::Gas;
let gas = Gas::from_gas(5 * 1_000_000_000_000);
assert_eq!(gas.as_teragas(), 5);Sourcepub const fn as_gas(self) -> u64
pub const fn as_gas(self) -> u64
Returns the total number of whole Gas contained by this Gas.
§Examples
use near_primitives_core::gas::Gas;
let gas = Gas::from_gas(12345);
assert_eq!(gas.as_gas(), 12345);Sourcepub const fn as_gigagas(self) -> u64
pub const fn as_gigagas(self) -> u64
Returns the total number of a whole part of giga Gas contained by this Gas.
§Examples
use near_primitives_core::gas::Gas;
let gas = Gas::from_gigagas(1);
assert_eq!(gas.as_gigagas(), 1);Sourcepub const fn as_teragas(self) -> u64
pub const fn as_teragas(self) -> u64
Returns the total number of a whole part of tera Gas contained by this Gas.
§Examples
use near_primitives_core::gas::Gas;
let gas = Gas::from_gas(1 * 1_000_000_000_000);
assert_eq!(gas.as_teragas(), 1);Sourcepub const fn checked_add(self, rhs: Gas) -> Option<Self>
pub const fn checked_add(self, rhs: Gas) -> Option<Self>
Checked integer addition. Computes self + rhs, returning None if overflow occurred.
§Examples
use near_primitives_core::gas::Gas;
assert_eq!(Gas::from_gas(u64::MAX -2).checked_add(Gas::from_gas(2)), Some(Gas::MAX));
assert_eq!(Gas::from_gas(u64::MAX -2).checked_add(Gas::from_gas(3)), None);pub fn checked_add_result(self, rhs: Gas) -> Result<Self, IntegerOverflowError>
Sourcepub const fn checked_sub(self, rhs: Gas) -> Option<Self>
pub const fn checked_sub(self, rhs: Gas) -> Option<Self>
Checked integer subtraction. Computes self - rhs, returning None if overflow occurred.
§Examples
use near_primitives_core::gas::Gas;
assert_eq!(Gas::from_gas(2).checked_sub(Gas::from_gas(2)), Some(Gas::ZERO));
assert_eq!(Gas::from_gas(2).checked_sub(Gas::from_gas(3)), None);Sourcepub const fn checked_mul(self, rhs: u64) -> Option<Self>
pub const fn checked_mul(self, rhs: u64) -> Option<Self>
Checked integer multiplication. Computes self * rhs, returning None if overflow occurred.
§Examples
use near_primitives_core::gas::Gas;
use std::u64;
assert_eq!(Gas::from_gas(2).checked_mul(2), Some(Gas::from_gas(4)));
assert_eq!(Gas::MAX.checked_mul(2), None)Sourcepub const fn checked_div(self, rhs: u64) -> Option<Self>
pub const fn checked_div(self, rhs: u64) -> Option<Self>
Checked integer division. Computes self / rhs, returning None if rhs == 0.
§Examples
use near_primitives_core::gas::Gas;
assert_eq!(Gas::from_gas(10).checked_div(2), Some(Gas::from_gas(5)));
assert_eq!(Gas::from_gas(2).checked_div(0), None);Sourcepub const fn saturating_add(self, rhs: Gas) -> Gas
pub const fn saturating_add(self, rhs: Gas) -> Gas
Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead of overflowing.
§Examples
use near_primitives_core::gas::Gas;
assert_eq!(Gas::from_gas(5).saturating_add(Gas::from_gas(5)), Gas::from_gas(10));
assert_eq!(Gas::MAX.saturating_add(Gas::from_gas(1)), Gas::MAX);Sourcepub const fn saturating_sub(self, rhs: Gas) -> Gas
pub const fn saturating_sub(self, rhs: Gas) -> Gas
Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds instead of overflowing.
§Examples
use near_primitives_core::gas::Gas;
assert_eq!(Gas::from_gas(5).saturating_sub(Gas::from_gas(2)), Gas::from_gas(3));
assert_eq!(Gas::from_gas(1).saturating_sub(Gas::from_gas(2)), Gas::ZERO);Sourcepub const fn saturating_mul(self, rhs: u64) -> Gas
pub const fn saturating_mul(self, rhs: u64) -> Gas
Saturating integer multiplication. Computes self * rhs, saturating at the numeric bounds instead of overflowing.
§Examples
use near_primitives_core::gas::Gas;
use std::u64;
assert_eq!(Gas::from_gas(2).saturating_mul(5), Gas::from_gas(10));
assert_eq!(Gas::MAX.saturating_mul(2), Gas::MAX);Sourcepub const fn saturating_div(self, rhs: u64) -> Gas
pub const fn saturating_div(self, rhs: u64) -> Gas
Saturating integer division. Computes self / rhs, saturating at the numeric bounds instead of overflowing.
§Examples
use near_primitives_core::gas::Gas;
assert_eq!(Gas::from_gas(10).saturating_div(2), Gas::from_gas(5));
assert_eq!(Gas::from_gas(10).saturating_div(0), Gas::ZERO)Trait Implementations§
Source§impl BorshDeserialize for Gas
impl BorshDeserialize for Gas
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl BorshSerialize for Gas
impl BorshSerialize for Gas
Source§impl DecType for Gas
impl DecType for Gas
Source§fn try_from_str(value: &str) -> Result<Self, ParseIntError>
fn try_from_str(value: &str) -> Result<Self, ParseIntError>
Source§fn try_from_unit() -> Result<Self, ParseUnitError>
fn try_from_unit() -> Result<Self, ParseUnitError>
null value. Returns error if this type
does not accept null values.