ckb_occupied_capacity_core/
units.rsuse serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
)]
pub struct Capacity(u64);
#[derive(Clone, PartialEq, Debug, Eq, Copy, Deserialize, Serialize)]
pub struct Ratio {
numer: u64,
denom: u64,
}
impl Ratio {
pub const fn new(numer: u64, denom: u64) -> Self {
Self { numer, denom }
}
pub fn numer(&self) -> u64 {
self.numer
}
pub fn denom(&self) -> u64 {
self.denom
}
}
pub trait IntoCapacity {
fn into_capacity(self) -> Capacity;
}
impl IntoCapacity for Capacity {
fn into_capacity(self) -> Capacity {
self
}
}
impl IntoCapacity for u64 {
fn into_capacity(self) -> Capacity {
Capacity::shannons(self)
}
}
impl IntoCapacity for u32 {
fn into_capacity(self) -> Capacity {
Capacity::shannons(u64::from(self))
}
}
impl IntoCapacity for u16 {
fn into_capacity(self) -> Capacity {
Capacity::shannons(u64::from(self))
}
}
impl IntoCapacity for u8 {
fn into_capacity(self) -> Capacity {
Capacity::shannons(u64::from(self))
}
}
const BYTE_SHANNONS: u64 = 100_000_000;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Overflow,
}
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "OccupiedCapacity: overflow")
}
}
impl ::std::error::Error for Error {}
pub type Result<T> = ::std::result::Result<T, Error>;
impl Capacity {
pub const fn zero() -> Self {
Capacity(0)
}
pub const fn one() -> Self {
Capacity(1)
}
pub const fn shannons(val: u64) -> Self {
Capacity(val)
}
pub fn bytes(val: usize) -> Result<Self> {
(val as u64)
.checked_mul(BYTE_SHANNONS)
.map(Capacity::shannons)
.ok_or(Error::Overflow)
}
pub fn as_u64(self) -> u64 {
self.0
}
pub fn safe_add<C: IntoCapacity>(self, rhs: C) -> Result<Self> {
self.0
.checked_add(rhs.into_capacity().0)
.map(Capacity::shannons)
.ok_or(Error::Overflow)
}
pub fn safe_sub<C: IntoCapacity>(self, rhs: C) -> Result<Self> {
self.0
.checked_sub(rhs.into_capacity().0)
.map(Capacity::shannons)
.ok_or(Error::Overflow)
}
pub fn safe_mul<C: IntoCapacity>(self, rhs: C) -> Result<Self> {
self.0
.checked_mul(rhs.into_capacity().0)
.map(Capacity::shannons)
.ok_or(Error::Overflow)
}
pub fn safe_mul_ratio(self, ratio: Ratio) -> Result<Self> {
self.0
.checked_mul(ratio.numer())
.and_then(|ret| ret.checked_div(ratio.denom()))
.map(Capacity::shannons)
.ok_or(Error::Overflow)
}
}
impl ::std::str::FromStr for Capacity {
type Err = ::std::num::ParseIntError;
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
Ok(Capacity(s.parse::<u64>()?))
}
}
impl ::std::fmt::Display for Capacity {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.0.fmt(f)
}
}
impl ::std::fmt::LowerHex for Capacity {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.0.fmt(f)
}
}