Skip to content
Closed
Show file tree
Hide file tree
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
58 changes: 51 additions & 7 deletions contracts/src/utils/structs/enumerable_set/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
//! Smart contract for managing sets.
//!
//! 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).
Comment on lines +3 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move these docs above the EnumerableSet struct.

Looking at it from dev perspective, hovering over the EnumerableSet struct I'm about to use, I'd expect it to contain docs explaining how to use it. There's a high chance devs would not know they need to hover over the module to get an example usage.

//!
//! [`EnumerableSet`] provides a generic implementation of sets that can store
//! various data types including [`alloy_primitives::Address`],
//! [`alloy_primitives::B256`], [`alloy_primitives::U8`],
//! [`alloy_primitives::U16`], [`alloy_primitives::U32`],
//! [`alloy_primitives::U64`], [`alloy_primitives::U128`], and [`U256`].
//!
//! # Usage Example
//!
//! ```rust,ignore
//! use alloy_primitives::Address;
//! use openzeppelin_stylus::utils::structs::enumerable_set::EnumerableSet;
//! use stylus_sdk::prelude::*;
//!
//! #[storage]
//! pub struct MyContract {
//! members: EnumerableSet<Address>,
//! }
//!
//! impl MyContract {
Comment on lines +23 to +28
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
//! #[storage]
//! pub struct MyContract {
//! members: EnumerableSet<Address>,
//! }
//!
//! impl MyContract {
//! #[storage]
//! #[entrypoint]
//! pub struct MyContract {
//! members: EnumerableSet<Address>,
//! }
//!
//! #[public]
//! impl MyContract {

Let's have a complete deployable contract

//! pub fn add_member(&mut self, member: Address) -> bool {
//! self.members.add(member)
//! }
//!
//! pub fn remove_member(&mut self, member: Address) -> bool {
//! self.members.remove(member)
//! }
//!
//! pub fn is_member(&self, member: Address) -> bool {
//! self.members.contains(member)
//! }
//! }
//! ```
//!
//! # Custom Storage Types
//!
//! You can implement [`EnumerableSet`] for your own storage types by
//! implementing the [`Element`] and [`Accessor`] traits (see [`element`]
//! module). This allows you to create sets of custom data structures that
//! integrate seamlessly with Stylus storage.
//!
//! **Note**: [`stylus_sdk::storage::StorageBytes`] and
//! [`stylus_sdk::storage::StorageString`] cannot currently be implemented due
//! to current Stylus SDK limitations, but this might change in the future.

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 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[Custom EnumerableSet Implementation]
Loading