From bbe422822354b4b06e592571cc19b4c70029ed81 Mon Sep 17 00:00:00 2001 From: Alisander Qoshqosh Date: Tue, 10 Dec 2024 16:21:38 +0400 Subject: [PATCH 1/2] drop SubAssignUnchecked / AddAssignUnchecked for storage guard --- contracts/src/utils/math/storage.rs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/contracts/src/utils/math/storage.rs b/contracts/src/utils/math/storage.rs index fb1e295ef..444a888eb 100644 --- a/contracts/src/utils/math/storage.rs +++ b/contracts/src/utils/math/storage.rs @@ -1,20 +1,11 @@ //! Simple math operations missing in `stylus_sdk::storage`. use alloy_primitives::Uint; -use stylus_sdk::storage::{StorageGuardMut, StorageUint}; +use stylus_sdk::storage::StorageUint; pub(crate) trait AddAssignUnchecked { fn add_assign_unchecked(&mut self, rhs: T); } -impl AddAssignUnchecked> - for StorageGuardMut<'_, StorageUint> -{ - fn add_assign_unchecked(&mut self, rhs: Uint) { - let new_balance = self.get() + rhs; - self.set(new_balance); - } -} - impl AddAssignUnchecked> for StorageUint { @@ -28,15 +19,6 @@ pub(crate) trait SubAssignUnchecked { fn sub_assign_unchecked(&mut self, rhs: T); } -impl SubAssignUnchecked> - for StorageGuardMut<'_, StorageUint> -{ - fn sub_assign_unchecked(&mut self, rhs: Uint) { - let new_balance = self.get() - rhs; - self.set(new_balance); - } -} - impl SubAssignUnchecked> for StorageUint { From e2a5b7b00e15300a2c293a8c5d6621b324fe5959 Mon Sep 17 00:00:00 2001 From: Alisander Qoshqosh Date: Tue, 10 Dec 2024 16:58:55 +0400 Subject: [PATCH 2/2] add docs --- contracts/src/utils/math/storage.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contracts/src/utils/math/storage.rs b/contracts/src/utils/math/storage.rs index 444a888eb..a0a762e0b 100644 --- a/contracts/src/utils/math/storage.rs +++ b/contracts/src/utils/math/storage.rs @@ -2,7 +2,9 @@ use alloy_primitives::Uint; use stylus_sdk::storage::StorageUint; +/// Adds value and assign the result to `self`, ignoring overflow. pub(crate) trait AddAssignUnchecked { + /// Adds `rhs` and assign the result to `self`, ignoring overflow. fn add_assign_unchecked(&mut self, rhs: T); } @@ -15,7 +17,9 @@ impl AddAssignUnchecked> } } +/// Subtract value and assign the result to `self`, ignoring overflow. pub(crate) trait SubAssignUnchecked { + /// Subtract `rhs` and assign the result to `self`, ignoring overflow. fn sub_assign_unchecked(&mut self, rhs: T); }