Skip to content

Commit

Permalink
ref: drop math for storage guard (#446)
Browse files Browse the repository at this point in the history
Drop `add_assign_unchecked` and `sub_assign_unchecked` functions for
`StorageGuardMut`, since guard already implements `Deref` and `DerefMut`
to wrapped types.
  • Loading branch information
qalisander authored Dec 10, 2024
1 parent 091d06d commit 49ff333
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions contracts/src/utils/math/storage.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
//! Simple math operations missing in `stylus_sdk::storage`.
use alloy_primitives::Uint;
use stylus_sdk::storage::{StorageGuardMut, StorageUint};
use stylus_sdk::storage::StorageUint;

/// Adds value and assign the result to `self`, ignoring overflow.
pub(crate) trait AddAssignUnchecked<T> {
/// Adds `rhs` and assign the result to `self`, ignoring overflow.
fn add_assign_unchecked(&mut self, rhs: T);
}

impl<const B: usize, const L: usize> AddAssignUnchecked<Uint<B, L>>
for StorageGuardMut<'_, StorageUint<B, L>>
{
fn add_assign_unchecked(&mut self, rhs: Uint<B, L>) {
let new_balance = self.get() + rhs;
self.set(new_balance);
}
}

impl<const B: usize, const L: usize> AddAssignUnchecked<Uint<B, L>>
for StorageUint<B, L>
{
Expand All @@ -24,19 +17,12 @@ impl<const B: usize, const L: usize> AddAssignUnchecked<Uint<B, L>>
}
}

/// Subtract value and assign the result to `self`, ignoring overflow.
pub(crate) trait SubAssignUnchecked<T> {
/// Subtract `rhs` and assign the result to `self`, ignoring overflow.
fn sub_assign_unchecked(&mut self, rhs: T);
}

impl<const B: usize, const L: usize> SubAssignUnchecked<Uint<B, L>>
for StorageGuardMut<'_, StorageUint<B, L>>
{
fn sub_assign_unchecked(&mut self, rhs: Uint<B, L>) {
let new_balance = self.get() - rhs;
self.set(new_balance);
}
}

impl<const B: usize, const L: usize> SubAssignUnchecked<Uint<B, L>>
for StorageUint<B, L>
{
Expand Down

0 comments on commit 49ff333

Please sign in to comment.