Skip to content

Commit 126f2a1

Browse files
ma2bdafck
andauthored
rename Amount::saturating_div into saturating_ratio (#4972)
## Motivation The `Amount` can be multiplied by `u128` using `saturating_mul` but we don't have an inverse operation. The existing `saturating_div` compute a `u128` ratio between two amounts. ## Proposal * rename `Amount::saturating_div` into `saturating_ratio` * create a new `Amount::saturating_div` that takes a u128 similar to `saturating_mul` ## Test Plan CI ## Release Plan - These changes should be backported to the latest `testnet` branch, then - be released in a new SDK, --------- Signed-off-by: Mathieu Baudet <[email protected]> Co-authored-by: Andreas Fackler <[email protected]>
1 parent a92cfc0 commit 126f2a1

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

linera-base/src/data_types.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ macro_rules! impl_wrapped_number {
463463
Ok(())
464464
}
465465

466+
/// Saturating division.
467+
pub fn saturating_div(&self, other: $wrapped) -> Self {
468+
Self(self.0.checked_div(other).unwrap_or($wrapped::MAX))
469+
}
470+
466471
/// Saturating multiplication.
467472
pub const fn saturating_mul(&self, other: $wrapped) -> Self {
468473
Self(self.0.saturating_mul(other))
@@ -727,7 +732,7 @@ impl Amount {
727732
}
728733

729734
/// Divides this by the other amount. If the other is 0, it returns `u128::MAX`.
730-
pub fn saturating_div(self, other: Amount) -> u128 {
735+
pub fn saturating_ratio(self, other: Amount) -> u128 {
731736
self.0.checked_div(other.0).unwrap_or(u128::MAX)
732737
}
733738

linera-execution/src/policy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl ResourceControlPolicy {
407407
/// Returns how much fuel can be paid with the given balance.
408408
pub(crate) fn remaining_fuel(&self, balance: Amount, vm_runtime: VmRuntime) -> u64 {
409409
let fuel_unit = self.fuel_unit_price(vm_runtime);
410-
u64::try_from(balance.saturating_div(fuel_unit)).unwrap_or(u64::MAX)
410+
u64::try_from(balance.saturating_ratio(fuel_unit)).unwrap_or(u64::MAX)
411411
}
412412

413413
pub fn check_blob_size(&self, content: &BlobContent) -> Result<(), ExecutionError> {

0 commit comments

Comments
 (0)