Skip to content
66 changes: 58 additions & 8 deletions contracts/src/utils/structs/enumerable_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the mod-level doc to:

//! Storage type for managing [sets] of primitive types.
//!
//! [sets]: https://en.wikipedia.org/wiki/Set_(abstract_data_type)

pub mod element;

/// Sets have the following properties:
///
/// * Elements are added, removed, and checked for existence in constant
/// time (O(1)).
/// * Elements are enumerated in O(n). No guarantees are made on the
/// ordering.
/// * Set can be cleared (all elements removed) in O(n).
use alloc::{vec, vec::Vec};

use alloy_primitives::{uint, U256};
Expand All @@ -18,7 +11,63 @@
storage::{StorageMap, StorageType, StorageU256, StorageVec},
};

/// State of an [`EnumerableSet`] contract.
/// Sets have the following properties:
///
/// * Elements are added, removed, and checked for existence in constant time
/// (O(1)).
/// * Elements are enumerated in O(n). No guarantees are made on the ordering.
/// * Set can be cleared (all elements removed) in O(n).
///
/// ## Usage
Comment on lines +14 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Sets have the following properties:
///
/// * Elements are added, removed, and checked for existence in constant time
/// (O(1)).
/// * Elements are enumerated in O(n). No guarantees are made on the ordering.
/// * Set can be cleared (all elements removed) in O(n).
///
/// ## Usage
/// Storage type for managing [sets] of primitive types.
///
/// Sets have the following properties:
///
/// * Elements are added, removed, and checked for existence in constant time
/// (O(1)).
/// * Elements are enumerated in O(n). No guarantees are made on the ordering.
/// * Set can be cleared (all elements removed) in O(n).
///
/// [sets]: https://en.wikipedia.org/wiki/Set_(abstract_data_type)
///
/// ## Usage

///
/// `EnumerableSet` works with the following primitive types out of the box:
///
/// * [`alloy_primitives::Address`] - Ethereum addresses
/// * [`alloy_primitives::B256`] - 256-bit byte arrays
/// * [`alloy_primitives::U8`] - 8-bit unsigned integers
/// * [`alloy_primitives::U16`] - 16-bit unsigned integers
/// * [`alloy_primitives::U32`] - 32-bit unsigned integers
/// * [`alloy_primitives::U64`] - 64-bit unsigned integers
/// * [`alloy_primitives::U128`] - 128-bit unsigned integers
/// * [`alloy_primitives::U256`] - 256-bit unsigned integers
///
/// ```rust
/// extern crate alloc;
///
/// use alloy_primitives::{Address, U256};
/// use stylus_sdk::prelude::*;
/// use openzeppelin_stylus::utils::structs::enumerable_set::EnumerableSet;
///
/// #[storage]
/// struct MyContract {
/// whitelist: EnumerableSet<Address>,
/// }
///
/// #[public]
/// impl MyContract {
/// fn add_to_whitelist(&mut self, address: Address) -> bool {
/// self.whitelist.add(address)
/// }
///
/// fn remove_from_whitelist(&mut self, address: Address) -> bool {
/// self.whitelist.remove(address)
/// }
///
/// fn is_whitelisted(&self, address: Address) -> bool {
/// self.whitelist.contains(address)
/// }
///
/// fn get_whitelist_size(&self) -> U256 {
/// self.whitelist.length()
/// }
/// }
/// ```
///
/// ## Custom Storage Types
///
/// You can implement `EnumerableSet` for your own storage types by implementing
/// the `Element` and `Accessor` traits. See [`element.rs`] for trait

Check failure on line 69 in contracts/src/utils/structs/enumerable_set/mod.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unresolved link to `element.rs`
/// definitions and the documentation for comprehensive examples.
#[storage]
pub struct EnumerableSet<T: Element> {
/// Values in the set.
Expand Down Expand Up @@ -188,6 +237,7 @@
use stylus_sdk::prelude::TopLevelStorage;
use alloy_primitives::private::proptest::{prop_assert, prop_assert_eq, proptest};


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't seem to find this one, on lines provided it is not after use alloy_primitives::private::proptest::{prop_assert, prop_assert_eq, proptest}; anymore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you didn't push local changes?

unsafe impl TopLevelStorage for $set_type {}

#[public]
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
** xref:uups-proxy.adoc[UUPS Proxy]

* xref:utilities.adoc[Utilities]
** xref:enumerable-set-custom.adoc[EnumerableSet Implementation for Custom Storage Types]
Loading
Loading