-
Notifications
You must be signed in to change notification settings - Fork 71
docs: Add comprehensive documentation for custom EnumerableSet #797
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
Closed
sumitvekariya
wants to merge
3
commits into
OpenZeppelin:main
from
sumitvekariya:docs/enumerable-set-custom-implementation
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||||||||||||||||||||||||||||||
//! | ||||||||||||||||||||||||||||||
//! [`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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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}; | ||||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.