Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: drop math for storage guard #446

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading