Skip to content

Commit

Permalink
reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
qalisander committed Oct 5, 2024
1 parent 111aa36 commit 565861a
Show file tree
Hide file tree
Showing 4 changed files with 596 additions and 589 deletions.
11 changes: 7 additions & 4 deletions contracts/src/token/erc721/extensions/consecutive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ use crate::{
},
utils::{
math::storage::{AddAssignUnchecked, SubAssignUnchecked},
structs,
structs::{
bitmap::BitMap,
checkpoints,
checkpoints::{trace::Trace, Size, S160},
checkpoints::{
generic_size::{Size, S160},
Trace,
},
},
},
};
Expand Down Expand Up @@ -115,8 +118,8 @@ sol! {
pub enum Error {
/// Error type from [`Erc721`] contract [`erc721::Error`].
Erc721(erc721::Error),
/// Error type from checkpoint contract [`checkpoints::trace::Error`].
Checkpoints(checkpoints::trace::Error),
/// Error type from checkpoint contract [`structs::checkpoints::Error`].
Checkpoints(structs::checkpoints::Error),
/// Batch mint is restricted to the constructor.
/// Any batch mint not emitting the [`Transfer`] event outside of
/// the constructor is non ERC-721 compliant.
Expand Down
89 changes: 89 additions & 0 deletions contracts/src/utils/structs/checkpoints/generic_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Contains generic size utilities for checkpoint storage contract.

use std::ops::{Add, Div, Mul, Sub};

Check failure on line 3 in contracts/src/utils/structs/checkpoints/generic_size.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/src/utils/structs/checkpoints/generic_size.rs#L3

error[E0433]: failed to resolve: use of undeclared crate or module `std` --> contracts/src/utils/structs/checkpoints/generic_size.rs:3:5 | 3 | use std::ops::{Add, Div, Mul, Sub}; | ^^^ use of undeclared crate or module `std`
Raw output
contracts/src/utils/structs/checkpoints/generic_size.rs:3:5:e:error[E0433]: failed to resolve: use of undeclared crate or module `std`
 --> contracts/src/utils/structs/checkpoints/generic_size.rs:3:5
  |
3 | use std::ops::{Add, Div, Mul, Sub};
  |     ^^^ use of undeclared crate or module `std`


__END__

use alloy_primitives::Uint;
use stylus_sdk::prelude::*;

/// Trait that associates types of specific size for checkpoints key and value.
pub trait Size {
/// Type of the key in abi.
type Key: Num;

/// Type of the key in storage.
type KeyStorage: for<'a> StorageType<Wraps<'a> = Self::Key>
+ Accessor<Wraps = Self::Key>;

/// Type of the value in abi.
type Value: Num;

/// Type of the value in storage.
type ValueStorage: for<'a> StorageType<Wraps<'a> = Self::Value>
+ Accessor<Wraps = Self::Value>;
}

/// Size of checkpoint storage contract with 96-bit key and 160-bit value.
pub struct S160;

impl Size for S160 {
type Key = <Self::KeyStorage as Accessor>::Wraps;
type KeyStorage = stylus_sdk::storage::StorageUint<96, 2>;
type Value = <Self::ValueStorage as Accessor>::Wraps;
type ValueStorage = stylus_sdk::storage::StorageUint<160, 3>;
}

/// Size of checkpoint storage contract with 32-bit key and 224-bit value.
pub struct S224;

impl Size for S224 {
type Key = <Self::KeyStorage as Accessor>::Wraps;
type KeyStorage = stylus_sdk::storage::StorageUint<32, 1>;
type Value = <Self::ValueStorage as Accessor>::Wraps;
type ValueStorage = stylus_sdk::storage::StorageUint<224, 4>;
}

/// Size of checkpoint storage contract with 48-bit key and 208-bit value.
pub struct S208;

impl Size for S208 {
type Key = <Self::KeyStorage as Accessor>::Wraps;
type KeyStorage = stylus_sdk::storage::StorageUint<48, 1>;
type Value = <Self::ValueStorage as Accessor>::Wraps;
type ValueStorage = stylus_sdk::storage::StorageUint<208, 4>;
}

/// Abstracts number inside the checkpoint contract.
pub trait Num: Add + Sub + Mul + Div + Ord + Sized + Copy {
/// Zero value of the number.
const ZERO: Self;
}

impl<const B: usize, const L: usize> Num for Uint<B, L> {
const ZERO: Self = Self::ZERO;
}

/// Abstracts accessor inside the checkpoint contract.
pub trait Accessor {
/// Type of the number associated with the storage type.
type Wraps: Num;

/// Gets underlying element [`Self::Wraps`] from persistent storage.
fn get(&self) -> Self::Wraps;

/// Sets underlying element [`Self::Wraps`] in persistent storage.
fn set(&mut self, value: Self::Wraps);
}

impl<const B: usize, const L: usize> Accessor
for stylus_sdk::storage::StorageUint<B, L>
{
type Wraps = Uint<B, L>;

fn get(&self) -> Self::Wraps {
self.get()
}

fn set(&mut self, value: Self::Wraps) {
self.set(value);
}
}
Loading

0 comments on commit 565861a

Please sign in to comment.